2

This is the struct.

//Structure pour communiquer les paramètres de traitement à travers le MMF
struct params_traitement_mmf
{
    int brilliance;
    double contraste;
    char convolution[9];
};

This is my code to display the size of this struct :

    char valeur[10];
    sprintf(valeur, "%d", sizeof(params_traitement_mmf));
    MessageBoxA(NULL, valeur, "rien", MB_OK);

The MessageBox displays 32. Thank you!

toto
  • 880
  • 11
  • 21
  • 1
    Duplicate of [Why isn't sizeof for a struct equal to the sum of sizeof of each member?](http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member) – James McNellis Dec 21 '10 at 03:25
  • 90% duplicate - It would be interesting to hear comments on the padding at the end of the structure. – Keith Dec 21 '10 at 03:29

5 Answers5

6

What packing? And compiled for what platform? Alignment requirement differ between x86, AMD64 and IA64. And packing can wreak havoc in a struct size.

Assuming default packing (8) and AMD64 target (or x86, wouldn't differ) you have 8 bytes for the brilliance (4 bytes size, 4 bytes wasted), 8 bytes for contraste and then 16 bytes for the convolution (9 bytes size, 7 bytes wasted). Total 32, which seems just about what you get.

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • Any particular reason you're looking at struct size, besides curiosity? You should not mess with neither alignment nor packing unless you have very specific reasons... – Remus Rusanu Dec 21 '10 at 03:42
  • 1
    I am using a Memory Mapped File to communicate between 2 processes. I'm using a structure at the beginning of this 1 megabyte FILE to communicate values between my processes and the rest of my file is used to store a frame in rgb values. So I need to know how big the size of my structure is, actually i do not since i'm using sizeof but it's a curiosity yes. – toto Dec 21 '10 at 03:46
  • OK, if you write the structure to 'disk' then you have to make sure you absolutely get it right. Typically structs that end up on disk are `pack`ed to 1 byte and then the code that handle them must be prepared to deal with alignment issues (specially on IA64). – Remus Rusanu Dec 21 '10 at 03:56
2

The biggest(memory wise) member of the struct is the double which takes 8 bytes. The struct will by default align memory on the largest member. Since the char is 9 bytes long, it needs 2x8 bytes to store it and the int even if taking only 4 bytes(on 32bit machine) will use 8 bytes.

4 -> 8 + 8 -> 8 + 9 -> 16 = 32 bytes.

Eric Fortin
  • 7,533
  • 2
  • 25
  • 33
  • I stated memory wise at the beginning of answer, I didn't feel it was needed to repeat it again. Can add it if you think it is. – Eric Fortin Dec 21 '10 at 03:23
  • The total struct size will be probably be aligned to the largest member. But the individual members need not be. – Johan Kotlinski Dec 21 '10 at 03:56
2

It is likely that the payload is 21 bytes (int=4, double=8, chars=9), but that your compiler adds padding between int and double to make the double 8-byte aligned. Also there is some padding at the end, to make sure the double is 8-byte aligned even if the struct is put in an array.

If you instead change definition so that double is moved first:

struct params_traitement_mmf {
    double contraste;
    int brilliance;
    char convolution[9];
};

...it is likely that the need to add padding between int and double disappears, and sizeof(params_traitment_mmf) may go down to 24.

Of course, what happens in practice is platform specific.

Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
1

My Mac lists this as only 24 bytes.

int - 4 bytes
double - 8 bytes
char[9] - 12 bytes (padding)

Windows must be adding extra padding for proper alignment (also depends on your processor).

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
1

You're getting

int - 4 bytes
padding - 4 bytes (So the double is aligned)
double 8 - bytes
char array - 9 bytes
padding - 7 bytes

Why the padding at the end? Not certain, but suppose you had an array of these; you'll need it 8 byte aligned for the double.

Keith
  • 6,756
  • 19
  • 23