Here is my program:
#include <stdio.h>
#include <stdlib.h>
main(){
char *p1, *p2, *p3, *p4;
p1 = (char*)malloc(10);
p2 = (char*)malloc(10);
p3 = (char*)malloc(16);
p4 = (char*)malloc(32);
printf("p1 points at: %d\n", p1);
printf("p2 points at: %d\n", p2);
printf("p3 points at: %d\n", p3);
printf("p4 points at: %d\n\n", p4);
system("PAUSE");
}
This produces the following output on my PC:
p1 points at: 6492080
p2 points at: 6492104
p3 points at: 6492128
p4 points at: 6492152
So, each memory space that malloc allocates starts 24 bytes further, no matter how many bytes are allocated. Why is that? I appreciate your help!
mallocshall allocate space for an object ofsizesize. Nothing prevents your implementation to add padding bytes for alignment purpose, for instance. Another example: some memory management implementations keep the size of your block before the allocated space.If you want more details, you should precise your implementation.