Anup Buchke
Anup Buchke

Reputation: 5520

Data Alignment Issue

struct NodeUsingAttribute
{

char cr;
int data __attribute__((aligned(8)));

};


struct Node
{

char cr;
int data ;

};

The first one gives size as 16 and second on gives size as 8 on my machine. I am not able to figure out why 16?.

Upvotes: 1

Views: 54

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754570

Your aligned attribute requires the data element to be on an 8-byte boundary. To ensure that all elements of an array of the structure are properly aligned, the structure as a whole has to be 8-byte aligned, and that's achieved by making it 16 bytes long.

Upvotes: 4

Related Questions