Quantcast
Viewing all articles
Browse latest Browse all 22018

extra padding between structs in C

I have struct in C:

typedef struct Node {
    int data; // 4 bytes int + 4 bytes for alignment
    struct Node* prev; // 8 bytes pointer
    struct Node* next; // 8 bytes pointer
} Node;

The size of this struct is 24 bytes (8 + 8 + 8). When I use the sizeof(Node), the compiler also shows 24 bytes.

However, when I create two or more structs on the heap (one after another) and look at their memory location, there are 8 byte gaps between each Node struct. For example:

11121344 (the 1st Node address)
11121376 (the 2nd Node address) // 376-344 = 32-24 = 8 extra bytes
11121408 (the 3rd Node address) // 408-376 = 32-24 = 8 extra bytes

Can you explain why compiler separates Node structs by adding 8 bytes between Nodes?


Viewing all articles
Browse latest Browse all 22018

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>