Structure size in C

Well, after reading this Structure size with char, double, int and at I still don't get the size of my structure ,:

struct s {
   char c1[3];
   long long k;
   char c2;
   char *pt;
   char c3;
}

      

And sizeof(struct s)

brings me back40

But according to the post I mentioned I thought the memory should look like this:

  0   1   2   3   4   5   6   7   8   9   a   b   c   d   e   f
+-------------+- -+---------------------------+- - - - - - - -+
| c1          |   |k                          |               |
+-------------+- -+---------------------------+- - - - - - - -+
 10  11  12  13  14  15  16  17
+---+- -+- -+- - - - - -+----+
|c2 |   |pt |           | c3 |
+---+- -+- -+- - - - - -+----+

      

And I should get 18

instead 40

... Can someone explain to me what I am doing wrong? Thank you very much!

+3


source to share


3 answers


Assuming 8 byte pointer size and alignment requirement on long long

and on pointers, then:

  • 3 bytes for c1

  • 5-byte pad
  • 8 bytes for k

  • 1 byte for c2

  • Padding 7 bytes
  • 8 bytes for pt

  • 1 byte for c3

  • Padding 7 bytes

This adds up to 40 bytes.

The trailing shim is highlighted so that the structure arrays keep all structure elements correctly aligned.



Note that dimensions, alignment requirements, and hence padding are machine hardware, compiler, and ABI (Application Binary Interface) platform dependent. The rules I have used are general rules: An N-byte type (for N in {1, 2, 4, 8, 16}) must be allocated on an N-byte boundary. Arrays (both within the structure and arrays of the structure) must also be correctly aligned. Sometimes you can change the overlay using directives #pragma

; be careful. It is usually best to lay out a structure with the most strictly aligned objects at the beginning and less line aligned objects at the end.

If you've used:

struct s2 {
   long long k;
   char *pt;
   char c1[3];
   char c2;
   char c3;
};

      

the required size should be only 24 bytes, total 3 bytes of padding pad. The order matters!

+6


source


The size of the structure depends on which compiler is used and which compiler options are included. The C language standard does not make promises about how memory is used when the compiler creates structures, and different architectures (for example, 32-bit WinTel and 64-bit WinTel) cause different layout decisions, even when the same compiler is used.



Essentially, the size of the structure is equal to the sum of the sizes of bytes required by the field elements (which can usually be computed) plus the sum of the padding bytes entered by the compiler (which is usually unknown).

+2


source


It is because of the alignment that gcc has

#pragma pack(push,n)
// declare your struct here
#pragma pack(pop)

      

to change it. Read here as well __attribute__((__packed__))

.

If you declare struct

struct packed
{
   char c1[3];
   long long k;
   char c2;
   char *pt;
   char c3;
} __attribute__((__packed__));

      

then compiling via gcc

, sizeof(packed) = 18

since

    c1: 3
    k : 8
    c2: 1
    pt: 4 // it depends
    c3: 1

      

Apparently the Visual C ++ compiler supports it #pragma pack(push,n)

too.

0


source







All Articles