1

I am trying to use realloc function to store input characters in a dynamically array. Everything goes fine when I use it without calling free method to release the memory after usage. But when I use it with free method runtime error comes. Here is my code snippet.

int main(){  
char *message ;  
int len = 0 ;
char c ;  
while((c=getchar()) != '\n'){  
    message = realloc(message,(len+1)*sizeof(char)) ;  
    message[len++] = c ;  
}     
message = realloc(message, (len+1)* sizeof(char));  
message[len]='\0' ;  
printf("Message is %s\n",message);  
free(message) ;  
return 0 ;  
}  

Can anyone figure out this. As i need to use both method together.. Thanks!!!!

7
  • Please post the error received. Commented Jun 21, 2012 at 23:26
  • I am getting this error *** glibc detected *** ./reallocdemo: realloc(): invalid pointer: 0x00d5cff4 *** ======= Backtrace: ========= /lib/i386-linux-gnu/libc.so.6(+0x6b961)[0xc6b961] /lib/i386-linux-gnu/libc.so.6(realloc+0x2ad)[0xc7073d] /lib/i386-linux-gnu/libc.so.6(realloc+0x2c5)[0xc70755] ./reallocdemo[0x80484b6] /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xe7)[0xc16e37] ./reallocdemo[0x8048401] Commented Jun 21, 2012 at 23:32
  • 1
    message is uninitialised. At least you could set it to null before calling realloc with it. Commented Jun 21, 2012 at 23:35
  • Oh great!!! message initialization solved this. Thanks wildplasser!!!! Commented Jun 21, 2012 at 23:40
  • Ther realloc() call is still not accident-proof. It could return NULL, and than you would leak memory. (not in trivial cases; but in practice most cases are non trivial ;-) Commented Jun 21, 2012 at 23:42

1 Answer 1

1

Though it may not be causing the problem you're seeing, X = realloc(X, newsize); is a timebomb waiting to explode. realloc can return a null pointer and leave your existing data unchanged if it fails to allocate the new chunk you've asked for. When/if it does that, this will overwrite the existing pointer with NULL, leaking the memory you've previously allocated (and failing to allocate more).

Though it's probably not causing the problem either, I'd also recommend (strongly) against using realloc to increase your allocation one character at a time. That's horribly inefficient. I'd start with a block of, say, 32 or 64 characters, and increase the allocation by some factor (say, 1.5) each time you run out of space. This way you can deal with greatly different lengths of input without a huge number of calls to realloc.

Edit: looking at it, the real problem probably that you haven't initialized your pointer properly before the initial call to realloc. If you pass something other than a NULL pointer, it expects what you're passing to be a valid pointer you got from malloc/calloc or a previous call to realloc.

int main(){  
char *message ;  
int len = 0 ;
char c ;  
while((c=getchar()) != '\n'){  
    message = realloc(message,(len+1)*sizeof(char)) ;  
    message[len++] = c ;  
}     
message = realloc(message, (len+1)* sizeof(char));  
message[len]='\0' ;  
printf("Message is %s\n",message);  
free(message) ;  
return 0 ;  
}  

At least for me, this runs without any error messages.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, Jerry!! Yeah sure I will use it to allocate memory by some factor of previously allocated memory. But here i am just checking it for the use of realloc and free together which cause problem..
I think this is not ok. First you give a non-answer that would have belonged into the comments and is not focussing on the real problem. Then you change this into an answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.