2

Does free releases the memory in the below, and how can I verify that memory is released ?

int *p = malloc(sizeof(int)); int *q = p; free(q); 

How to access MCB structure for a dynamically allocated memory ?

1
  • 2
    Yes, free will always release the memory space pointed to by p, regardless of whether you have made copies of p. There is no point verifying it as free cannot fail on valid pointers (obtained through *alloc). And you are not supposed to access the MCB. Commented Sep 19, 2013 at 5:12

3 Answers 3

2

Yes, it does as both p and q point to same memory address returned by malloc().

To verify try to access that memory after its freed and check it through valgrind, which will show invalid memory access.

Sign up to request clarification or add additional context in comments.

Comments

2

Def of free()::

void free (void* ptr); 

Deallocate memory block

A block of memory previously allocated by a call to malloc, calloc or realloc is deallocated, making it available again for further allocations.

If ptr does not point to a block of memory allocated with the above functions, it causes undefined behavior.

If ptr is a null pointer, the function does nothing.

Notice that this function does not change the value of ptr itself, hence it still points to the same (now invalid) location.

So, in that case, if you try to access the freed memory through the pointer which still points to the same location, it would cause "undefined behavior". You can verify the same by invoking this undefined behavior( for Experimental pupose only ) or you can use Valgrind to verify. If you are using Visual studio, you can add a watch to the pointer q and see for yourself the memory layout and freeing of the block.

Comments

1

As per your code the memory will be released, But if you change the pointer q other than initial pointer p. It will not work.

For explanation: When you call malloc() you pass the amount or size to allocate memory, But in reality the memory is allocated more than size because memory allocator stores some meta information of this allocation. So when you call free with the initial pointer of the given malloced region the free() function is able to get this info & so memory is deallocated. But if you pass the pointer other than the initial pointer of the allocated region it will not get this meta info & free() will fail.!!!

6 Comments

"how can I verify that" ?
@P0W there is no way to verify this...If ptr does not point to a block of memory allocated with the allocated functions, it causes undefined behavior. and If ptr is a null pointer, the function does nothing. & free does not modify ptr passed to it.
@akp Do you mean free(q) won't work ? What is initial pointer ?
@VivekS As per Abhineet answers u could verify that free(q) will work but result may be undefined if passed pointer "q" is not same as pointer returned by malloc() or calloc() or realloc() functions,whichever u have used.
@akp Off course. We just need to pass a pointer to a memory region allocated by malloc and its friends. Whether it is p, q ...or z does not matter. By pointer we mean the address of the memory location, not the variable. There can be 100 variables pointing to the same location and any one of them can be passed to free.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.