重新分配二维字符数组
我有以下代码,
int wordLenght = 256, arrayLength = 2, i = 0, counter = 0;
char **stringArray = NULL;
stringArray = calloc(arrayLength, sizeof(*stringArray));
for(counter; counter<wordLenght; counter++)
stringArray[counter] = calloc(wordLenght, sizeof(stringArray));
while(1)
{
printf("Input: ");
fgets(stringArray[i], wordLenght, stdin);
printf("stringArray[%d]: %s\n", i, stringArray[i]);
if(i == arrayLength)
{
printf("Reallocation !!!\n");
arrayLength *= 2;
stringArray = realloc(stringArray, arrayLength*sizeof(*stringArray));
}
i++;
}
但出现此重新分配错误:
*** glibc detected *** ./stringArray: realloc(): invalid next size: 0x0000000000b49010 ***
======= Backtrace: =========
/lib/libc.so.6(+0x775b6)[0x7f4dd12565b6]
/lib/libc.so.6(+0x7dd66)[0x7f4dd125cd66]
/lib/libc.so.6(realloc+0xf0)[0x7f4dd125d080]
./stringArray[0x4007f9]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f4dd11fdc4d]
./stringArray[0x400629]
我的问题是什么???
谢谢,问候
I have following code
int wordLenght = 256, arrayLength = 2, i = 0, counter = 0;
char **stringArray = NULL;
stringArray = calloc(arrayLength, sizeof(*stringArray));
for(counter; counter<wordLenght; counter++)
stringArray[counter] = calloc(wordLenght, sizeof(stringArray));
while(1)
{
printf("Input: ");
fgets(stringArray[i], wordLenght, stdin);
printf("stringArray[%d]: %s\n", i, stringArray[i]);
if(i == arrayLength)
{
printf("Reallocation !!!\n");
arrayLength *= 2;
stringArray = realloc(stringArray, arrayLength*sizeof(*stringArray));
}
i++;
}
I get this reallocation error:
*** glibc detected *** ./stringArray: realloc(): invalid next size: 0x0000000000b49010 ***
======= Backtrace: =========
/lib/libc.so.6(+0x775b6)[0x7f4dd12565b6]
/lib/libc.so.6(+0x7dd66)[0x7f4dd125cd66]
/lib/libc.so.6(realloc+0xf0)[0x7f4dd125d080]
./stringArray[0x4007f9]
/lib/libc.so.6(__libc_start_main+0xfd)[0x7f4dd11fdc4d]
./stringArray[0x400629]
What's my problem here ???
Thanks, greets
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里你可能想使用 sizeof(char*)
这里你循环了 256 次 (wordLenght) 但你应该只循环 2 次 (arrayLength)。此外,您可能想使用 sizeof(char) 而不是 sizeof(stringArray)。
这个检查应该在调用 fgets 之前完成,因为现在您首先使用内存,然后再分配它们。
此外,在重新分配 stringArray 之后,您需要使用类似的方法分配其余字符串
,最后您需要在退出应用程序之前释放所有分配的内存。
Here you probably wanted to use sizeof(char*)
Here you are looping 256 times (wordLenght) but you should only 2 times (arrayLength). Additionally you probably wanted to use sizeof(char) instead of sizeof(stringArray).
This check should be done before you call fgets, because right now you are firstly using memory and later allocate them.
Additionally after you reallocate stringArray you need to allocate rest of strings using something like this
And finally you need to free all allocated memory before you exit application.
您可能不是指
sizeof(*stringArray)
事实上,我相信您可能也想重新查看
calloc
调用,我认为您正在分配指针的大小那里(字长乘以)。You probably didn't mean
sizeof(*stringArray)
In fact I believe you may want to relook at the
calloc
call too, I think you are allocating the size of the pointer there (word length times).第一次执行此行后:
那么 stringArray[arrayLength/2] 将是一个垃圾值 - 您尚未将其设置为指向该单词的存储。
这部分应该使用 use sizeof(**stringArray),或者 1 因为 **stringArray 是 char,并且计数器应该只达到 arrayLength:
而不是在一个块中分配:
目前,后面可能有一些空格stringArray,当您调用它们时,将(wordLength-arrayLength)额外指针存储到其中,并且 realloc 不会移动 stringArray。
很可能 0xb49010 是您调用的指针之一,并且您覆盖了 malloc 保留其块大小的内存。
但是由于您正在注销 stringArray 的末尾,因此您无论如何都会陷入未定义的行为。
After the first time this line executes:
then stringArray[arrayLength/2] will be a garbage value - you haven't set it to point to storage for the word.
This part should use either use sizeof(**stringArray), or 1 as **stringArray is char, and the counter should only go up to arrayLength:
Instead allocate in one block:
At the moment, it's possible that there is some space after stringArray, into which you are storing the (wordLength-arrayLength) extra pointers when you calloc them, and realloc doesn't move stringArray.
It's quite probable that 0xb49010 is one of the pointers you calloc'd, and you're overwritten the memory where malloc keeps its block size..
But since you're writing off the end of stringArray, you're into undefined behaviour anyway.
好的,这是整个解决方案:
释放空间的最佳方法是什么?!
Ok here is the whole solution:
How is the best way to free the space ?!