Why would freeing a pointer say p, which is assigned to where another pointer q points to, free q as well?
//for example,
int *p = malloc(sizeof(int));
*p = 10;
int *q = malloc(sizeof(int));
q = p;
free(p);
//This frees q as well.
Pointers q and p have different addresses but only point to the same location (i.e. to say that they store the same address which is of 10. Correct me if I'm mistaken there).
The contradiction most definitely arises in the way I think about pointers or in the way free() works because from all that I know (which is not a lot), since q and p have different addresses, freeing p should not affect q at all, unless I am wrong in understanding pointers, or that free()/memory works in a peculiar way.
I'd like to know why this is so. (I'm aware of the post Free an assigned pointer, but it doesn't explain why or how exactly this happens)
Pointer assignment between two pointers makes them point to the same pointee. So the assignment y = x; makes y point to the same pointee as x . Pointer assignment does not touch the pointees. It just changes one pointer to have the same reference as another pointer.
The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. which means that a later call to malloc (or something else) might re-use the same memory space. As soon as a pointer is passed to free() , the object it pointed to reaches the end of its lifetime.
No! It is perfectly legal, moral, and wholesome to use malloc() and delete in the same program, or to use new and free() in the same program. But it is illegal, immoral, and despicable to call free() with a pointer allocated via new, or to call delete on a pointer allocated via malloc().
When pointer assignment occurs, any previous association between the pointer object and a target is terminated. Pointers can also be assigned for a pointer structure component by execution of a derived-type intrinsic assignment statement or a defined assignment statement.
So the assignment y = x; makes y point to the same pointee as x. Pointer assignment does not touch the pointees. It just changes one pointer to have the same reference as another pointer. After pointer assignment, the two pointers are said to be "sharing" the pointee.
Website Planning and Creation A pointer is a variable whose value is the direct address of a memory region, or the address of another variable. A pointer must be declared before it may be used to hold any variable address, just like any variable or constant. A pointer variable declaration's general form is
Answer: The basic steps are... Allocate two pointers. Allocate two pointees and set the pointers to point to them. Store the numbers 1 and 2 into the pointees. Assign the first pointer to point to the second pointee. This "loses" the reference to the first pointee which is unusual, but that's what the question calls for.
The dereferenceoperation starts at the pointer and follows its arrow over to access its pointee. The goal may be to look at the pointee state or to change the pointee state. The dereference operation on a pointer only works if the pointer has a pointee -- the pointee must be allocated and the pointer must be set to point to it.
After
int *p = malloc(sizeof(int));
x
+----+
p --------> | |
+----+
P
is pointing to some x
memory which holds the some junk data.
*p = 10;
You are putting 10
into x
x
+----+
p --------> | 10 |
+----+
With below
int *q = malloc(sizeof(int));
y
+----+
q -------> | |
+----+
You created one more memory y
to which q
is pointing.
When you assign
q = p;
x
+----+
p --------> | 10 |
q --------> +----+
y
+----+
Memory leak | |
+----+
You made q
to point x
memory as well, losing the only reference to y
memory.
With freeing p
free(p);
p --------> (invalidated)
q -------->
y
+----+
Memory leak | |
+----+
You deleted x
memory thus p
and q
are pointing to freed memory.
Perhaps this would help:
You are not freeing p
or q
themselves. You are freeing the memory block pointed to by them.
After free()
, both p
and q
themselves continue to exist. You can no longer dereference them, but you can continue using them in other ways. For example, you can make them point to another, valid, address (after which it will again become permissible to dereference them).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With