4

Is there a method/function that frees up memory without the possibility of moving it to a new pointer in c?

Thanks!

user2662982
  • 237
  • 1
  • 2
  • 8
  • 1
    What exactly are you trying to accomplish? If you no longer need some part of your allocated memory (and as such certain variables), just free those variables. – JSQuareD Aug 11 '13 at 11:06
  • @JSQuareD: I would guess that the OP wants to avoid the overhead of the copy that a realloc() may incur. – Oliver Charlesworth Aug 11 '13 at 11:23
  • Some implementations won't move the data unless a smaller chunk can accommodate it reasonably (it must be able to grow a little or it wasn't worth moving because it will just get moved back when made larger again). If it doesn't move your data, it will simply add the extra freed bytes to the free list and keep your data intact at the same address. –  Aug 11 '13 at 13:09

2 Answers2

4

According to a strict interpretation of the C99 standard, any call to realloc(), including to reduce the size of an allocated block, may return a pointer different from the argument. In fact, according to a strict interpretation of the standard, you are not allowed to compare the old pointer with the new pointer, because passing the old pointer to realloc() renders all existing copies of it indeterminate.

The realloc function deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size. … (C99 7.20.3.4:2)


However, the man page for realloc() on my system says:

If there is not enough room to enlarge the memory allocation pointed to by ptr, realloc() creates a new allocation, copies as much of the old data pointed to by ptr as will fit to the new allocation, frees the old allocation, and returns a pointer to the allocated memory.

This more or less implies that realloc() can only move data and return a new pointer when used to enlarge a block. This would suggest that on this platform, realloc() can be used to reduce the size of a block obtained from malloc() without risk of moving it.

If I was to use realloc() to reduce the size of an allocated block without moving it, I would use the check below:

uintptr_t save = old;
void * new = realloc(old, …);
assert (save == (uintptr_t) new);

These precautions are not for nothing, see for instance Winner #2 in this informal undefined behavior competition.

2021 EDIT: Years later, however, further efforts to re-interpret and evolve the definition of C as that of a high-level language mean that even of the above three lines are perfectly legal, even after executing them, you cannot use an old copy of old to access the block pointed by new, even though these pointers point to the same place. See the current paper about pointer provenance in C.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
  • The only thing I might add is how pointless it would be to compare the pointers. If `realloc` returns `NULL`, your old pointer didn't get changed. Otherwise, the new pointer is what should be used. An assignment is faster than a branch and possible assignment, so just assign the new pointer to the old pointer after ensuring the new pointer is not `NULL`. If it is NULL, the data is still in the old pointer and can safely be accessed, though it wasn't resized. What you do under such a condition is dependent upon the context and intent of the reallocation. –  Aug 11 '13 at 14:32
  • @ChronoKitsune if (new != old) { costly update numerous copies of old pointer } is an idiom that can sometimes be seen in existing code. I am not taking position on its value, I am just saying it exists and it is technically undefined behavior. – Pascal Cuoq Aug 11 '13 at 14:54
  • If there is another variable somewhere pointing to the same address, that would be your job after realloc. You'd blindly assign all of those pointers to the new pointer because they would be invalid anyway. It shouldn't be costly to do an assignment operation. If it is, I'd argue in favor of one more level of indirection where possible to avoid such an update. That way, assigning the new pointer to the old pointer would update other copies because they would point to the address of the old pointer rather than to the same address that the old pointer once pointed to. –  Aug 11 '13 at 15:43
-1

According to me No,Because you have to copy to new location and then use free() to free previous location.

This is what i understand from you question.

Please give clear question.What i understand is what @JsQuareD also understand from your question.

Madan Ram
  • 880
  • 1
  • 11
  • 18