Skip to main content
5 votes
Accepted

When we free a dynamically allocated memory, what happens to the pointer pointing at it?

Yes, when you use a free(px); call, it frees the memory that was malloc'd earlier and pointed to by px. The pointer itself, however, will continue to exist and will still have the same address. It ...
Cliff B's user avatar
  • 69k
2 votes
Accepted

What will realloc() do if the contiguous memory location is already occupied?

The answer of our friend @NullityNull is correct, as long as realloc succeeds and works well. Your question is interesting. Consider the following program fragment char *string; string = (char *)...
MARS's user avatar
  • 5,179
2 votes

What will realloc() do if the contiguous memory location is already occupied?

If realloc can't resize the memory block you pass in, it makes a new one, copies the data, and deallocates the old one. If I were you I'd read up a bit on specification for Realloc and malloc.
NullityNull's user avatar
2 votes
Accepted

Week 4, Section : Dynamic Memory Allocation

In addition to the points made by @MARS and @NullityNull, there are advantages and disadvantages to both methods. Allocating memory on the stack is more convenient because it automatically cleans up ...
hotwebmatter's user avatar
  • 3,050

Only top scored, non community-wiki answers of a minimum length are eligible