我在重新分配一组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);
}
}
有什么建议吗?感谢
答案 0 :(得分:1)
char* words[0] = (char*)malloc(charCount*sizeof(char));
如果这实际上在您的代码中(errors out for me),请尝试删除第一个char*
。