0

Possible Duplicate:
Why isn't sizeof for a struct equal to the sum of sizeof of each member?

I have the next code:

http://ideone.com/brmRy

#include <stdio.h>
#include <stdlib.h>
 
typedef struct Test
{
        int a;
        int b;
        char c;
} Test;
 
int main(void)
{
        Test *obj = (Test*)malloc(sizeof(Test));
 
        printf("Size results:\r\n\r\nstruct: %i\r\nint #1: %i\r\nint #2: %i\r\nchar #1: %i\r\n", 
                sizeof(Test), sizeof(obj->a), sizeof(obj->b), sizeof(obj->c));
 
        return 0;
}

The result is:

Size results:

struct: 12

int #1: 4

int #2: 4

char #1: 1

Why DOES struct size 12 bytes??? int - 4 bytes char - 1 byte

2 int + 1 char = 2 * 4 bytes + 1 byte = 9 bytes.

Why does 12 ???

Community
  • 1
  • 1

2 Answers2

2

Memory is commonly aligned on 4-byte boundaries, so even though the char only takes up 1 byte, it's padded by 3 bytes to meet this segmentation requirement. Notably, individual elements of a struct don't have to be aligned, so if you made one of the ints into a short, you could reduce the struct size from 12 to 8 bytes. I believe you'd have to put the short next to the char in the struct declaration to receive this bonus, though.

1''
  • 26,823
  • 32
  • 143
  • 200
  • 1
    The point of the padding is to have the individual members of the struct suitably aligned, so that (here) the `int` members are four-byte-aligned. To achieve that, the structs must have (at least) the same alignment requirements as the member with the largest alignment requirements, so padding is inserted to make the size of the struct a multiple of that requirement. You can force(?) a smaller alignment with a `pack(ed)` attribute or pragma, but there are architectures where misaligned access causes program abortion. – Daniel Fischer Sep 22 '12 at 15:12
  • No, as this example shows (http://stackoverflow.com/questions/8539348/c-struct-sizes-inconsistence?rq=1) if you have a char, char, short, char, it only takes up 8 bytes. The point of padding is to align the overall struct. To get more of a saving, take a look at bitfields (http://msdn.microsoft.com/en-us/library/yszfawxh(v=vs.80).aspx). Their use is rather situational though. – 1'' Sep 22 '12 at 15:40
  • `short`s will typically be two-byte-aligned, so a `struct foo { short bar; char baz; };` will normally be padded to take four bytes. If you have a `short` and a `char` next to each other in a `struct` with an `int`, you'll again need one byte padding to have the `int` properly aligned. But in `struct foo { short bar; int baz; char quux; };` you'll typically get two bytes padding between the `short` and the `int`, and three bytes next to the `char` (before or after, methinks after is more common), so putting the `short` and the `char` next to each other saves (usually) four bytes. – Daniel Fischer Sep 22 '12 at 15:41
1

If you're using gcc you can force "packing". This means that no nalignment is made and the struct entries stand next to each other. Try

typedef struct Test
{
    int a;
    int b;
    char c;
} __attribute__((packed)) Test;
Peter F.
  • 111
  • 1
  • 7