-1

我在重新分配char数组时遇到了一些问题。当我尝试使用 GDB 调试它时,我得到了realloc(): invalid old size: 0x00000000006042f0 error. 这是代码:

int wordCount = 1;
int charCount = 1;
char** words = (char**)malloc(wordCount*sizeof(char*));
char* words[0] = (char*)malloc(charCount*sizeof(char));
char c = getNextChar(file);//it will read the content of the file character by character
while(c!='\0')//read
{
    words[wordCount-1] = (char*)realloc(words[wordCount-1],(charCount+1)*sizeof(char));
    charCount++;
    c = getNextChar(file);
    if(c=='\n' || c==' ')
    {
         words = (char**)realloc(words, (countWord+1)*sizeof(char*)); //this is where I got the error
         wordCount++;
         c = getNextChar(file);
    }
}

有什么建议吗?谢谢

4

1 回答 1

1
char* words[0] = (char*)malloc(charCount*sizeof(char));

如果这实际上在您的代码中(对我来说是错误的),请尝试删除第一个char*.

于 2013-07-02T05:56:56.217 回答