简体   繁体   English

重新分配2d char数组

[英]Reallocate a 2d char array

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 谢谢,打招呼

You probably didn't mean sizeof(*stringArray) 你可能不是故意的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). 实际上,我相信您可能也想回顾一下calloc调用,我认为您是在此处分配指针的大小(字长乘以)。

 stringArray = calloc(arrayLength, sizeof(*stringArray));

Here you probably wanted to use sizeof(char*) 在这里您可能想使用sizeof(char *)

for(counter; counter<wordLenght; counter++) stringArray[counter] = calloc(wordLenght, sizeof(stringArray));

Here you are looping 256 times (wordLenght) but you should only 2 times (arrayLength). 在这里,您要循环256次(wordLenght),但您应该仅循环2次(arrayLength)。 Additionally you probably wanted to use sizeof(char) instead of sizeof(stringArray). 另外,您可能想使用sizeof(char)而不是sizeof(stringArray)。

if(i == arrayLength) {...}

This check should be done before you call fgets, because right now you are firstly using memory and later allocate them. 应该在调用fgets之前完成此检查,因为现在您首先使用内存,然后再分配它们。

Additionally after you reallocate stringArray you need to allocate rest of strings using something like this 另外,在重新分配stringArray之后,您需要使用类似以下的方法分配其余的字符串

for(counter = i; counter<arrayLength; counter++) stringArray[counter] = (char*)calloc(wordLenght, sizeof(char));

And finally you need to free all allocated memory before you exit application. 最后,您需要在退出应用程序之前释放所有分配的内存。

After the first time this line executes: 第一次执行此行后:

stringArray = realloc(stringArray, arrayLength*sizeof(*stringArray));

then stringArray[arrayLength/2] will be a garbage value - you haven't set it to point to storage for the word. 那么stringArray [arrayLength / 2]将是一个垃圾值-您尚未将其设置为指向该单词的存储。

This part should use either use sizeof(**stringArray), or 1 as **stringArray is char, and the counter should only go up to arrayLength: 这部分应使用sizeof(** stringArray)或1,因为** stringArray是char,并且计数器应仅上升到arrayLength:

 for(counter; counter<wordLenght; counter++) 
     stringArray[counter] = calloc(wordLenght, sizeof(stringArray));

Instead allocate in one block: 而是分配一个块:

 char* block = malloc(wordLength * arrayLength);

 for ( counter; counter < arrayLength; ++counter ) 
     stringArray[counter] = block + ( counter * wordLength );

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. 目前,stringArray后面可能有一些空间,当您调用它们时,您将在其中存储(wordLength-arrayLength)额外的指针,而realloc不会移动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.. 0xb49010很可能是您调用的指针之一,并且您覆盖了malloc保持其块大小的内存。

But since you're writing off the end of stringArray, you're into undefined behaviour anyway. 但是,由于要注销stringArray的末尾,因此无论如何都会遇到未定义的行为。

Ok here is the whole solution: 好的,这是整个解决方案:

int wordLength = 256, arrayLength = 2, i = 0, counter = 0;
    char **stringArray = NULL;
    char buffer[wordLength];

    stringArray = calloc(arrayLength, sizeof(char*));
    for(counter; counter<arrayLength; counter++) stringArray[counter] = (char*)calloc(wordLength, sizeof(char));

    while(1)
    {
        if(i == arrayLength)
        {
            printf("Reallocation !!!\n");
            arrayLength *= 2;

            stringArray = realloc(stringArray, arrayLength*sizeof(char*));
            for(counter = i; counter<arrayLength; counter++) stringArray[counter] = (char*)calloc(wordLength, sizeof(char));
        }   

        printf("Input: ");
        fgets(buffer, wordLength, stdin);

        if(!strcmp(buffer,"q\n")) break; // also free here      
        else stringArray[i] = buffer;

        printf("stringArray[%d]: %s\n", i, stringArray[i]);

        i++;
    }

How is the best way to free the space ?! 释放空间的最佳方法是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM