Bug 31670 - Error calculating size of structs
Summary: Error calculating size of structs
Status: RESOLVED INVALID
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 4.1.2
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-04-23 19:26 UTC by Sergio
Modified: 2007-04-23 19:32 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sergio 2007-04-23 19:26:04 UTC
#include <stdio.h>

typedef struct{
	char data[261];
	int n;
} packet;

int main(int argc, char *argv[]){
	packet p;

	//It should print packet=265... it prints packet=268
	printf("packet = %d\n", sizeof(packet)); 
	//It should print p=265... it prints p=268
	printf("p = %d\n", sizeof(p));
	//It should print p.n=4... OK
	printf("p.n = %d\n", sizeof(p.n));
	//It should print p.data=261... OK
	printf("p.data = %d\n", sizeof(p.data));
	return 0;
}
Comment 1 Andrew Pinski 2007-04-23 19:32:56 UTC
Learn about alignment when doing struct layout.  Basically you want to use the attribute packed to get the sizes you want.