Skip to contentSkip to conversation

realloc

DISCUSSION Chuck Winters 4 participants 4 messages Aug 5, 2002

lore
Share & tools
lore
#1 lore raw
In reply to Chuck Winters
Quoted 10 lines · Chuck Winters

Hello all,
Anyone know why a program would freeze at realloc? I thought
if there was a problem, realloc would just fail.

Thanks,
Chuck
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Hi,

The problem happens just because of specific allocator that you are using. You can avoid it by simply malloc new area, copy the content and free previos one. Just as follow:

#include <sys/mman.h>
ptr1=malloc(size1);
.
.
.
ptr2=realloc(ptr1,size2);

...........................

ptr1=malloc(size1);
.
.
.
ptr2=malloc(size2);
memcpy(ptr2,ptr1,size1);
free(ptr1);

Good Luck,

Mehran

#0 lore raw

Hello all,
Anyone know why a program would freeze at realloc? I thought
if there was a problem, realloc would just fail.

Thanks,
Chuck

#2 lore raw
Quoted 1 line · Chuck Winters

Anyone know why a program would freeze at realloc?

By far the most likely reasons are:

a) heap corruption, and
b) passing an invalid pointer (one which wasn't returned from malloc (or similar), or which has since been freed.

Quoted 1 line

I thought if there was a problem, realloc would just fail.

If there is insufficient memory to satisfy a request, realloc would fail.

OTOH, if the heap is corrupt, or if the pointer is invalid, all bets are off; calling malloc/calloc/realloc/free etc will result in "undefined behaviour".

Signature · 1 line
Glynn Clements <[email protected]>
#3 lore raw
In reply to Chuck Winters
Quoted 4 lines

Hello all,
Anyone know why a program would freeze at realloc? I thought
if there was a problem, realloc would just fail.

Not really. I've had that problem... it's a corrupted heap problem, usually. And the results are undefined.. :(

Ciao!

Jason

Signature · 5 lines
|UUCP  [email protected]                        Who wills, Can.
|VOICE (408) 243-3425                         Who tries, Does.
|LOCAL Hey, Jason!                              Who loves, Lives.          o_.
|Disclaimer:  Not me! I didn't do *THAT!*                                 <|
|Local Disclaimer:  I'm not here!                   A. McCaffrey           4