Reputation: 2019
I'm testing malloc to understand it behavior, and I have found something strange after some manipulation.
I have made 3 different tests with the same var.
First, I have alloc and free my 3 var one by one :
char * a = malloc(10 * sizeof(char));
printf("\na = %p\n", a);
free(a);
char * b = malloc(10 * sizeof(char));
printf("b = %p\n", b);
free(b);
char * c = malloc(10 * sizeof(char));
printf("c = %p\n\n", c);
free(c);
this display :
a = 0xd34010
b = 0xd34010
c = 0xd34010
Logical, it display the same address, memory space is recycled.
Then, I have malloc my 3 var, and then free :
printf("Alloc 3 times and then free :\n");
a = malloc(10 * sizeof(char));
printf("\na = %p\n", a);
b = malloc(10 * sizeof(char));
printf("b = %p\n", b);
c = malloc(10 * sizeof(char));
printf("c = %p\n\n", c);
free(a); free(b); free(c);
It display me :
a = 0xd34010
b = 0xd34030
c = 0xd34050
a
is logical because the last c
variable had been free'ed, so the memory space is re-used. Addresses of b
and c
are logical too because a
and b
are not free'ed so addresses increase.
And now, It become really strange. I malloc a, then b, free a, malloc c and free b and c :
printf("Alloc, free, alloc :\n\n");
a = malloc(10 * sizeof(char));
printf("a = %p\n", a);
b = malloc(10 * sizeof(char));
printf("b = %p\n", b);
free(a);
c = malloc(10 * sizeof(char));
printf("c = %p\n", c);
free(b); free(c);
the result is :
a = 0xd34050
b = 0xd34030
c = 0xd34050
My questions are : Why is a
equal to 0xd34050 and not to 0xd34010 ? And why is the address of b
lower than the address of a
?
Upvotes: 0
Views: 340
Reputation: 222362
I suspect you are performing these tests in one program and the malloc
implementation you are using maintains a stack of some recently-freed blocks.
Thus, in the first test:
malloc
, needs new space, so 0xd34010 is given.malloc
is satisfied by providing the most recently freed block, 0xd34010.malloc
is satisfied by providing the most recently freed block, 0xd34010.Then, in the second test:
malloc
is satisfied by providing the most recently freed block, 0xd34010.malloc
needs new space, so 0xd34030 is given.malloc
needs new space, so 0xd34050 is given.Then, in the third test:
malloc
is satisfied by providing the most recently freed block, 0xd34050.malloc
is satisfied by providing the most recently freed (and still free) block, 0xd34030.malloc
is satisfied by providing the most recently freed block, 0xd34050.Of course, such behavior is not guaranteed by the C standard. This is merely a potential explanation of what you happen to observe in this particular instance.
Upvotes: 3
Reputation: 4584
It is machine dependent. Your Compiler's book keeping routine does it that way. It may be due to memory constraints given to it.
Since you used malloc, it uses Heap memory
Upvotes: 0
Reputation: 1196
The reason might be
Why is a equal to 0xd34050 and not to 0xd34010 ?
Some other process in your machine already occupied the address 0xd34010, so your compiler gives you the next free memory.
And so far I know, depends on the compiler the memory may allocated in the ascending order or descending order. it is not necessarily to be in the ascending order.
Upvotes: 0
Reputation: 22542
You do know that printf
also calls malloc
to allocate stream buffers? And those buffers are not necessarily cleared until you close stdout
.
Try saving all your pointers separately before printing them.
Upvotes: 0
Reputation: 49803
Because that's how memory allocation is implemented in the particular environment you are using. That may sound flip, but the whole point of having such routines is so that they can take care of the bookkeeping for you; you've got more important things to do!
Upvotes: 4