Reputation: 3
I was trying to understand better how the memory in the linked list is allocated so I made a simple program in order to see where the addresses are stored.
#include <stdio.h>
struct node {
int data;
struct node* next;
};
int main()
{
struct node first;
struct node second;
struct node third;
struct node *aux; //pointer to go through list
first.data = 1;
second.data = 2;
third.data = 3;
first.next = &second;
second.next = &third;
third.next = NULL;
aux = &first;
while (aux)
{
printf("%p\n", aux); //printing each address
aux = aux->next;
}
return 0;
}
And we get the output:
0x7fff14fabac0
0x7fff14fabad0
0x7fff14fabae0
So there is 1 byte difference between nodes.
So basically first = second - 1. How come there are spaces in memory left for integers since sizeof(int) equals 4 bytes, and we advance only by 1 byte?
Upvotes: 0
Views: 72
Reputation: 450
You are ignoring the last digit, the difference is 16 bytes. The 16 bytes are most likely a result of 8 byte alignment of your system.
Upvotes: 1