This assignment asks us to allocate two int-type variables using malloc()
(named var1
and var2
), print the addresses of each variable (the address of the pointer on the stack and the address on the heap), then use free()
to deallocate var1
, print the addresses again, then allocate another space in the heap for var1
and print the addresses a third time. I believe that the instructor is trying to show us that the heap address for var1
is supposed to change, but it always stays the same... unless I remove free(var1)
from the code. The instructor did a similar demonstration, but did not use free()
to deallocate any variables, so we never saw how this was supposed to work.
Here is my code:
#include <stdio.h>
#include <stdlib.h>
void main()
{
int *var1 = (int*)malloc(sizeof(int));
*var1 = 1000;
int *var2 = (int*)malloc(sizeof(int));
*var2 = 2000;
printf("Addresses of var1\n");
printf("Pointer on stack: %p / Heap: %p\n\n", &var1, var1);
printf("Addresses of var2\n");
printf("Pointer on stack: %p / Heap: %p\n\n", &var2, var2);
free(var1);
printf("AFTER DEALLOCATING var1 FROM THE HEAP\n");
printf("Addresses of var1\n");
printf("Pointer on stack: %p / Heap: %p\n\n", &var1, var1);
printf("Addresses of var2\n");
printf("Pointer on stack: %p / Heap: %p\n\n", &var2, var2);
var1 = (int*) malloc(sizeof(int));
*var1 = 1500;
printf("NEW MEMORY ADDRESS ALLOCATED FOR var1\n");
printf("Addresses of var1\n");
printf("Pointer on stack: %p / Heap: %p\n\n", &var1, var1);
printf("Addresses of var2\n");
printf("Pointer on stack: %p / Heap: %p\n\n", &var2, var2);
}
This code results in this output:
Addresses of var1
Pointer on stack: 0xffffcbf8 / Heap: 0x600000390
Addresses of var2
Pointer on stack: 0xffffcbf0 / Heap: 0x6000003b0
AFTER DEALLOCATING var1 FROM THE HEAP
Addresses of var1
Pointer on stack: 0xffffcbf8 / Heap: 0x600000390
Addresses of var2
Pointer on stack: 0xffffcbf0 / Heap: 0x6000003b0
NEW MEMORY ADDRESS ALLOCATED FOR var1
Addresses of var1
Pointer on stack: 0xffffcbf8 / Heap: 0x600000390
Addresses of var2
Pointer on stack: 0xffffcbf0 / Heap: 0x6000003b0
As you can see, the heap address does not change for var1
when I deallocate it, and it doesn't change when I allocate memory space for var1
again. However, if I simply remove the free(var1)
line from the program, it simply assigns a second memory space for var1
and points to that on the heap, which DOES have a different memory address:
Addresses of var1
Pointer on stack: 0xffffcbf8 / Heap: 0x600000390
Addresses of var2
Pointer on stack: 0xffffcbf0 / Heap: 0x6000003b0
AFTER DEALLOCATING var1 FROM THE HEAP
Addresses of var1
Pointer on stack: 0xffffcbf8 / Heap: 0x600000390
Addresses of var2
Pointer on stack: 0xffffcbf0 / Heap: 0x6000003b0
NEW MEMORY ADDRESS ALLOCATED FOR var1
Addresses of var1
Pointer on stack: 0xffffcbf8 / Heap: 0x600000420
Addresses of var2
Pointer on stack: 0xffffcbf0 / Heap: 0x6000003b0
(Just to be clear, all I did was remove free(var1)
from the previous code, so the "AFTER DEALLOCATING var1" section now shows the exact same heap address as the previous set, but it DOES change the heap address of var1 in the third section.)
Can anybody tell me what is happening here? The only logical explanation I can come up with is that when I'm using free()
to deallocate var1
and then printing the address, it's simply printing the LAST address that it pointed to, and then when I'm allocating memory for var1
the second time, it's simply "backfilling" the previous address with the new value of var1
. Does this make sense? Do I have errors in my code, or is this just how C behaves when deallocating memory for a variable and then reallocating it?
The C standard defines the behavior of the free function: 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.
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 will not automatically be changed to NULL or anything else.
Calling free() on a null pointer results in no action being taken by free() . Setting message to NULL after it is freed eliminates the possibility that the message pointer can be used to free the same memory more than once.
Free memory, which is memory available to the operating system, is defined as free and cache pages. The remainder is active memory, which is memory currently in use by the operating system. The Disk And Swap Space Utilization page, shown in Figure 9.2, shows system resources use, including disk and swap space use.
It is perfectly normal that malloc
might return the same addresses when memory is freed and then reallocated. It would also be normal for it to return different addresses.
If you change the malloc
calls to request different sizes than the original allocations, you might get different addresses, since the old blocks malloc
had prepared might not be enough for the new requests. But they might be enough, so the addresses might not change.
Incidentally:
void main()
is incorrect. It should be int main(void)
.malloc
is freed with free
, its lifetime ends.The only logical explanation I can come up with is that when I'm using free() to deallocate var1 and then printing the address, it's simply printing the LAST address that it pointed to
Kind of right. Freeing a pointer does not affect the content of the pointer at all. Or to be more precise, it's value is indeterminate after being freed. In fact, according to the standard you cannot even trust the pointer to contain the address it did before invoking free
. As far as I know, it will in most cases, but you cannot trust it.
The standard says:
C 2018 6.2.4 2: “The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.”
A common mistake is to use the test if(ptr == NULL)
as a check if you have properly freed the pointer. This will not work.
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