0

I Think I am having a problem with reallocing the token character pointer, please help. i googged how to realloc but I am not sure if this is the right way to do it because I am getting MEMORY CORRUPTION error when I run my program.

char* token = (char*)malloc(sizeof(char));
token[0] = '\0';
int c;
do{         
    c = fgetc(fp);
    if(isalnum(c)){ //add to char array
        if(isalpha(c))
            c = tolower(c);
        if(token[0] == '\0'){
            token[0] = (char)c;
            token[1] = '\0';
        }
        else{
            token = (char*)realloc(token, strlen(token)+2);
            int len = strlen(token);
            token[len] = (char)c;
            token[len+1] = '\0';
        }
    }
    else{ //token
        if(token[0] != '\0'){ //add token
            struct token* newtoken = (struct token*)malloc(sizeof(struct token));
            newtoken->token = (char*)malloc(strlen(token)*sizeof(char));
            strcpy(newtoken->token, token);
            newtoken->records = NULL;
            struct record* newrecord = (struct record*)malloc(sizeof(struct record));
            newrecord->fileName = (char*)malloc(strlen(fileName)*sizeof(char));
            strcpy(newrecord->fileName, fileName);
            newrecord->freq = 1;
            tokens = (struct token*)addToken(tokens, newtoken, newrecord);
        }
        token[0] = '\0';
    }
    if(feof(fp))
        break;
}while(1);
user3100209
  • 357
  • 1
  • 4
  • 17
  • Your first malloc only allocates room for 1 char. I think you want at least 2 chars. – 001 Mar 06 '14 at 06:05
  • One obvious problem: you need to allocate `strlen(s) + 1` prior to `strcpy`, since `strlen` does not include the nul-terminator character. You also don't need those messy casts before `malloc`. – Brett Hale Mar 06 '14 at 06:06
  • `char* token = (char*)malloc(sizeof(char));` are you sure this is correct? sizeof(char) is always **1**, so you are going to cause some trouble by accessing `token[1]`... – Naytzyrhc Mar 06 '14 at 06:07
  • @Naytzyrhc: `sizeof char` is defined to be 1, but `token[1]` would be invalid regardless of the size. – Ed S. Mar 06 '14 at 06:08
  • I would suggest you allocate a block of memory first, when that is used up allocate a new block. It is not effective to allocate/realloc a few bytes a time. – AndersK Mar 06 '14 at 06:15

1 Answers1

1

You wrote:

char* token = (char*)malloc(sizeof(char));

More clearly expressed as char *token = malloc(1);, this allocates 1 byte.

But then you go:

token[0] = (char)c;
token[1] = '\0';

which writes 2 bytes into a 1-byte allocation. This is a buffer overflow and may be the cause of your memory corruption. You could fix this by malloc`ing two bytes to start off with.

You also overwrite your buffers later:

newtoken->token = (char*)malloc(strlen(token)*sizeof(char));
strcpy(newtoken->token, token);

Change to:

newtoken->token = malloc( strlen(token) + 1 );
strcpy(newtoken->token, token);

Notice how my version has fewer warts than yours too, so it is easier to read and therefore easier to spot if there are any errors.

The next strcpy after that also has the same problem.

M.M
  • 138,810
  • 21
  • 208
  • 365