Problem with realloc ()

A

anirbid.banerjee

#include <stdlib.h>
#include <stdio.h>
int main(){
char *ptr = "hello";
ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char ));
printf ("\n %s", ptr);
return 0;
}
___________________________________
The above program while execution dumps a stack trace and exits. This
is the detailed error message.

*** glibc detected *** ./try: realloc(): invalid pointer: 0x080484b0
***
======= Backtrace: =========
/lib/libc.so.6(realloc+0x38e)[0x94aa3e]
/lib/libc.so.6[0x94ab81]
/lib/libc.so.6(realloc+0x3c)[0x94a6ec]
../try[0x80483af]
/lib/libc.so.6(__libc_start_main+0xdc)[0x8f6f2c]
../try[0x8048301]
======= Memory map: ========
00295000-00296000 r-xp 00295000 00:00 0 [vdso]
008c0000-008d9000 r-xp 00000000 08:02 163372 /lib/ld-2.5.so
008d9000-008da000 r-xp 00018000 08:02 163372 /lib/ld-2.5.so
008da000-008db000 rwxp 00019000 08:02 163372 /lib/ld-2.5.so
008e1000-00a18000 r-xp 00000000 08:02 163373 /lib/libc-2.5.so
00a18000-00a1a000 r-xp 00137000 08:02 163373 /lib/libc-2.5.so
00a1a000-00a1b000 rwxp 00139000 08:02 163373 /lib/libc-2.5.so
00a1b000-00a1e000 rwxp 00a1b000 00:00 0
00cc7000-00cd2000 r-xp 00000000 08:02 163382 /lib/
libgcc_s-4.1.1-20061011.so.1
00cd2000-00cd3000 rwxp 0000a000 08:02 163382 /lib/
libgcc_s-4.1.1-20061011.so.1
08048000-08049000 r-xp 00000000 08:08 288064 /home/subh/try
08049000-0804a000 rw-p 00000000 08:08 288064 /home/subh/try
08a5c000-08a7d000 rw-p 08a5c000 00:00 0
b7fd4000-b7fd5000 rw-p b7fd4000 00:00 0
b7ff0000-b7ff1000 rw-p b7ff0000 00:00 0
bfd49000-bfd5f000 rw-p bfd49000 00:00 0 [stack]
Aborted
____________________________________________________
I don't know what is going wrong. Please explain.

Thanks
Anirbid
 
C

christian.bau

#include <stdlib.h>
#include <stdio.h>
int main(){
char *ptr = "hello";
ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char ));
printf ("\n %s", ptr);
return 0;}

___________________________________
The above program while execution dumps a stack trace and exits.

And rightfully so.

You must call realloc () and free () only for pointers that have been
previously allocated with a successful call to malloc, calloc or
realloc and that have not been free'd yet, or for null pointers.
 
S

santosh

#include <stdlib.h>
#include <stdio.h>
int main(){

If you're not going to use command line arguments, then int main(void)
might be better.
char *ptr = "hello";
ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char ));

realloc can only resize memory which has previously been allocated by
malloc or calloc. Similarly, free can only release memory obtained
from malloc, calloc or realloc. Specifically, the pointer value that
you pass to realloc or free must be the same value that malloc or
calloc returned or a previous call of realloc returned. In the above
case, you violate these requirements, hence leading to undefined
behaviour.

An exception involves passing a null pointer to realloc or free. In
that case realloc behaves like malloc, while free does nothing.

Also, in C, you don't need to cast the value returned by malloc,
calloc or realloc. sizeof(char) is always one, hence in the statement
above redundant. I would write it as:

ptr = realloc(ptr, 10 * sizeof *ptr);

Notice that I dropped the uneccessary cast to size_t. Also doing
sizeof *ptr keeps the statement correct even when you might happen to
change the type of ptr. If realloc fails it returns a null pointer,
but the original memory whose resizing was requested remains
unchanged. Thus unless you want a memory leak you'll need to backup
the original pointer before passing it to realloc or pass a temporary
to realloc and asign to the original pointer only if successful.
printf ("\n %s", ptr);

Supply a trailing newline to the printf string or call fflush(stdout)
immediatly after the printf, otherwise the output may be buffered and
may fail to appear when expected.

<snip>
 
R

Richard Heathfield

(e-mail address removed) said:
#include <stdlib.h>
#include <stdio.h>
int main(){
char *ptr = "hello";
ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char ));
printf ("\n %s", ptr);
return 0;
}


Christian Bau has already answered your question about the above code,
so I won't repeat what he's said. But I would like to take the
opportunity to add a few comments of my own:

1) never cast the return value of realloc. It's not necessary or
desirable, and can have the unhappy side effect of suppressing a
diagnostic message should you ever forget to #include <stdlib.h>
(which, on this occasion, you did not).

2) don't use the same pointer object to catch realloc's return value.
Since realloc returns NULL if it is unable to resize the memory block,
you run the risk of overwriting your only pointer to the original
block. Here's how to do it properly:

char *ptr = malloc(n * sizeof *ptr);
if(ptr != NULL)
{
/* okay, we have a memory block. Let's pretend we've used it
for a bit, and we discover it's not enough... */

if(not enough memory)
{
char *tmp = realloc(ptr, newcount * sizeof *ptr);
if(tmp != NULL)
{
/* ptr is now indeterminate, but tmp is valid, so we can
do this... */
ptr = tmp;
tmp = NULL;
}
else
{
/* tmp is invalid, but ptr is still a good pointer to the
memory we already had */
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top