0

I am using code::blocks 12.11 (gcc) on a 32 bit OS. I have the following structure:

struct node 
{
int a;
float b;
char d;
struct node* c;
}
s1;

now individually,

sizeof(int);    sizeof(float);    sizeof(char);

gives output 4 4 1 bytes respectively. so i calculate size of structure as 13 bytes. But the following

sizeof(s1); or sizeof(struct node);

gives output 16bytes.

I am unable to figure out why this is so. Please help me out here. Thanks.

Madeyedexter
  • 1,155
  • 2
  • 17
  • 33
  • 1
    The computer auto aligns memory to a certain bounds, so that an odd number of bytes will still occupy a certain range of memory that is "aligned." For instance, I struct of 1 char, and int, and a char, will probably take 8 bytes. – KrisSodroski Jul 22 '13 at 20:35

1 Answers1

1

The struct rounded to 32-bit (4-byte) chunks. If you'd added 3 more chars, it would be the same size.

Jiminion
  • 5,080
  • 1
  • 31
  • 54