I am working on a project that implements the buddy allocation algorithm in C. Using mmap(), I allocated a space of 1GB of continuous memory. It looks like this:
char * base = mmap(NULL, MAX_MEM, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
I have a struct that I am trying to place into that space. The struct looks like this:
typedef struct Node {
unsigned char header;
struct Node *next;
struct Node *prev;
}node;
I am unsure how to get a node to go into that spot. I tried the code below because I didn't want to put the node at the very beginning, but when I print out the pointer they show much different spots
void *ptr = (struct Node *) base + 512;
printf("base: %p\n", base);
printf("ptr: %p\n", ptr);
My terminal window prints out this:
base: 0x102ef1000
ptr: 0x102ef4000
But this doesn't seem right because the pointers are 12288 spaces away from each other in memory instead of just 512. What am I doing wrong?