2013-07-02 90 views
-1

我正面临一些问题,重新分配char数组的数组。当我尝试使用GDB进行调试时,我得到了realloc(): invalid old size: 0x00000000006042f0 error。这里是代码:realloc导致内存映射错误SIGABRT

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

这是真正的密码? KarthikT指出,至少应该给出一些编译器警告。 –

+2

请确定您的文章中的代码是实际的代码。否则很难帮助你。从编辑器或IDE复制并粘贴。不要重新编写它,并避免粘贴后进行编辑。 –

+1

你有没有'#包括'''?另外:在C中没有理由也没有获得'malloc()'/'realloc()'的结果,甚至更多:这样做往往会隐藏错误。 – alk

回答

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

如果这实际上是在你的代码(它errors out for me),尽量去除第一char*