C: Realloc issues with double pointer -


i trying copy char word[50] array char **wordlist double pointer , realloc(wordlist, (numwords+1)*sizeof(char*)) increase size of word list 1 more char* hold next word.

i keep getting *** glibc detected *** ./program: realloc(): invalid next size error

i declaring wordlist = realloc(wordlist, (numwords+1)*sizeof(char*))

void outfile(char *argv[], char **wordlist)   {       if((infileptr = fopen(argv[2], "r")) != null)       {           if((outfileptr = fopen(endoffile, "w")) != null)           {               while((c = fgetc(infileptr)) != eof)               {                   if(!(isspace(c)))                   {                       word[wordindex] = c;                       wordindex++;                       rowlim++;                   }                   if(isspace(c))                   {                       wordindex = 0;                       if(rowlim < limit)                       {                           rowlim++;                           strcat(text[rownum], word);                           strcat(text[rownum], " \0");                           wordlist[numwords] = strdup(word);                           /*strcat(wordlist[numwords], "\n\0");*/                           memset(word, 0, 50);                           prev = rowlim;                           wordlist = realloc(wordlist, (numwords + 1)*sizeof(char*));                           numwords++;                       }                       else                       {                           strcat(text[rownum], "\n\0");                           printf("text[%d] = %s",rownum, text[rownum]);                           fputs(text[rownum], outfileptr);                           rownum++;                           strcat(text[rownum], word);                           strcat(text[rownum], " \0");                           /*strcat(wordlist[numwords], word);                           strcat(wordlist[numwords], "\n\0");                           wordlist = realloc(wordlist, (numwords + 1)*sizeof(char*));*/                          numwords++;                          memset(word, 0, 50);                          rowlim = rowlim - prev;                      }                      wordindex = 0;                  }              }              strcat(text[rownum], "\n\0");              printf("text[%d] = %s",rownum, text[rownum]);              fputs(text[rownum], outfileptr);              fclose(outfileptr);          } 

you're trying use space before have allocated it:

wordlist[numwords] = strdup(word); wordlist = realloc(wordlist, (numwords + 1)*sizeof(char*)); numwords++; 

instead, allocate space first:

wordlist = realloc(wordlist, (numwords + 1)*sizeof(char*)); wordlist[numwords] = strdup(word); numwords++; 

nb. noted iharob, if wish able recover running out of memory, return value of realloc should stored in separate variable until have confirmed not null.

all of \0 redundant. string literals strings: example "\n" means { '\n', '\0' } already.

it's impossible tell code whether cause buffer overflow writing text[rownum]. i'd suggest redesigning code make sure can never overflow.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -