我在重新分配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);
}
}
有什么建议吗?谢谢