2013-07-02 2 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

'# include'는''입니까? 또한 : C에서'malloc()'/'realloc()'의 결과를 캐스팅 할 아무런 이유가 없으며, 에러를 숨기는 경향이 있습니다. – alk

답변

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

이 코드에서 사실 인 경우 (그 errors out for me), 최초의 char*를 제거하려고합니다.