stressed_geek
stressed_geek

Reputation: 2176

realloc memory for a pointer which has been freed?

When I try to use realloc to allocate memory for a pointer which has been free'd, I get a segmentation fault. Although I don't face this issue if I use malloc instead.

As per my understanding after the variable has been free'd it is equivalent to a NULL pointer, then why is this unexpected behavior? Am I missing something?

Upvotes: 0

Views: 499

Answers (6)

pb2q
pb2q

Reputation: 59667

A pointer that has been free'd is not equivalent to a NULL pointer. After calling free you'll need to set the pointer to NULL yourself.

If you're passing a pointer to realloc that has been free'd, but not explicitly set to NULL, you're probably passing realloc an invalid address, which it will try to use, resulting in undefined behavior, hence your segfault.

Upvotes: 3

Jerry Coffin
Jerry Coffin

Reputation: 490728

The pointer you pass into realloc must be one of two things: either a valid pointer that was previously returned from malloc/calloc/realloc, or else a null pointer.

If you free the memory, you need to set the pointer to NULL before you pass it to realloc again. free will not set the pointer to NULL (nor modify the pointer itself in any other way -- it can't because it receives a copy of the pointer, not the pointer itself).

Upvotes: 0

ChrisH
ChrisH

Reputation: 924

This from man realloc(3) from a Debian Linux box should tell you all you need to know - basically don't free before using realloc()

realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. If ptr is NULL, then the call is equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.

Upvotes: 0

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272802

As per my understanding after the variable has been free'd it is equivalent to a NULL pointer.

A NULL pointer is a pointer whose value is NULL; standard functions like realloc know how to interpret this value.

A pointer to some memory that has been freed is now an invalid pointer; its value doesn't change. realloc doesn't know that it's invalid, and will try and access it, leading to the seg-fault.

Upvotes: 4

Maresh
Maresh

Reputation: 4712

No free() doesn't set the pointer to 0, You must do it yourself.

If the pointer passed to realloc is null, it will malloc it for you.

Upvotes: 3

ecatmur
ecatmur

Reputation: 157484

No, a free'd pointer is not equivalent to a null pointer. free does not modify the pointer passed in, so the pointer continues to point to the same memory location, which is now unallocated memory.

If you try to realloc it then the memory allocator will get confused and corrupt its internal structures, which is why you get a segfault.

Upvotes: 0

Related Questions