Pointer and Its memory

M

Mahendra

I have two cases -

1. When I have a pointer A pointing to a heap memory - What happens when
I dereference the pointer A using free(A).

It deallocated the heap memory the pointer was pointing to. Is that
correct ?

2. If I have a pointer A pointing to a heap memory. Now I create another
pointer B which points to same heap memory as pointer A. But now, I do
not want pointer A to still point to heap memory but I want to remove
the pointer A itself.

In this scenario, if I do not want to deallocate the created heap memory
pointed by pointer A (holding some data in the memory allocated) and
also do not want to copy the memory to some other same struct type, then
is there a way to remove pointer A pointing to heap memory but keep
pointer B pointing to the heap memory.

Essentially my question is - can i deallocate the memory holding the
pointer variable itself ? Can i use free()? If so, can i do it with
free(&p) by freeing the memory which holds the pointer variable itself.

Thanks
Mahendra
 
P

Peter Nilsson

Richard Heathfield said:
...When you pass a pointer value to free(), one of three
things will happen.
Either:

(1) it is a value previously handed to you by malloc,
calloc, or realloc and not yet passed to free(), in
which case the memory pointed to will be deallocated; or

Note that realloc can deallocate memory too, if you ask
for zero size (in C89), in which case you have a null
pointer and point (2) applies if you pass it to free().
 
J

James Kuyper

Mahendra said:
I have two cases -

1. When I have a pointer A pointing to a heap memory - What happens when
I dereference the pointer A using free(A).

That is not dereferencing the pointer. That is deallocating the memory
it points at. free() itself may dereference a pointer based upon the
value you pass it, but your code does not dereference A.

Note that heap memory is an implementation detail, and not all systems
even have a heap. The key point is whether A points at the start of a
block of memory that was allocated by calling malloc(), calloc() or
realloc().
It deallocated the heap memory the pointer was pointing to. Is that
correct ?

Yes, it deallocated the memory (heap or not), that the pointer pointed at.
2. If I have a pointer A pointing to a heap memory. Now I create another
pointer B which points to same heap memory as pointer A. But now, I do
not want pointer A to still point to heap memory but I want to remove
the pointer A itself.

In this scenario, if I do not want to deallocate the created heap memory
pointed by pointer A (holding some data in the memory allocated) and
also do not want to copy the memory to some other same struct type, then
is there a way to remove pointer A pointing to heap memory but keep
pointer B pointing to the heap memory.
>
Essentially my question is - can i deallocate the memory holding the
pointer variable itself ?

You can only allocate objects with allocated storage duration, and a
named variable will always have either static or automatic storage
duration. However, when an object with automatic storage duration
reaches the end of its lifetime, the memory set aside to store that
object is no longer used for that purpose, and can be used for some
other purpose. That's similar in concept to deallocation. Would that
meet your needs? Here's an example:

void *B;
if(condition)
{
void *A = malloc(desired_size);
B = A;
}

At this point, the compiler is free to use the memory that used to be
used to store A, for some other purpose.

You could also dynamically allocate the memory used to store the
pointer, but in that case 'A' could not be the name used to refer to
that memory. Richard Heathfield has already given you an example of how
to do this.
Can i use free()?

You can only use free() to deallocate memory with allocated storage
duration.
... If so, can i do it with
free(&p) by freeing the memory which holds the pointer variable itself.

No, passing a pointer at free() which was not returned by one of the
malloc() family members has undefined behavior. It will most likely
cause your program to malfunction, possibly by aborting immediately,
possibly in a much more obscure fashion that you might not even notice
until after it has corrupted every record in a database containing data
that used to be worth millions of dollars.
 
H

Harold Aptroot

(snipped)
Essentially my question is - can i deallocate the memory holding the
pointer variable itself ? Can i use free()? If so, can i do it with
free(&p) by freeing the memory which holds the pointer variable itself.

Thanks
Mahendra

Why do you even want to do this?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top