1

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

this is my struct and the size of it is 40 but the size of all variables is 34. how can I eliminate extra space of this struct?

typedef struct
{
USHORT SequenceNumber;
USHORT LinkCount; 
USHORT AttributeOffset;
USHORT Flags;
ULONG BytesInUse;
ULONG BytesAllocated;
ULONGLONG BaseFileRecord;
USHORT NextAttributeNumber;
USHORT Padding;
ULONG MFTRecordNumber;
USHORT UpdateSeqNum;
} FILE_RECORD_HEADER, *PFILE_RECORD_HEADER;
Community
  • 1
  • 1
micheal
  • 197
  • 1
  • 1
  • 5
  • 2
    http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member Dupe? – Aidan Steele Nov 16 '10 at 06:25

4 Answers4

2

That's because the compiler is free to insert padding to honour alignment requirement. You have a couple of options.

The first is inherently non-portable but many implementations provide something like:

#pragma pack

which will attempt to pack structures as tight as possible. Just be aware that this may slow down your code, depending on the architecture

The other is to put all the most-aligned elements up front such as:

typedef struct {
    ULONGLONG BaseFileRecord;              // 0x00
    ULONG BytesInUse;                      // 0x08
    ULONG BytesAllocated;                  // 0x0c
    ULONG MFTRecordNumber;                 // 0x10
    USHORT NextAttributeNumber;            // 0x14
    USHORT SequenceNumber;                 // 0x16
    USHORT LinkCount;                      // 0x18
    USHORT AttributeOffset;                // 0x1a
    USHORT Flags;                          // 0x1c
    USHORT Padding;                        // 0x1e
    USHORT UpdateSeqNum;                   // 0x20
} FILE_RECORD_HEADER, *PFILE_RECORD_HEADER;

with likely offsets in comments, asuming that ULONGLONG is 8 bytes, ULONG is 4 and USHORT is 2. This won't necessarily remove all padding but it will make it easier for the compiler to minimise it.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

Because of padding. The size of your structure is platform and compiler dependent. Why do you care? Are you treating your struct as binary data?

Ed S.
  • 122,712
  • 22
  • 185
  • 265
0

Alignment + padding.

There are two ways to solve this:

  1. Reduce padding by arranging variables in largest to smallest order.
  2. Use the non-standard packed attribute extension in your compiler (__attribute__((packed)) for GCC). Note that the access time for reading struct members will go down, and code size will go up (though not as bad as on platforms which simply cannot access unaligned words, such as ARM).
Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
0

Yup, you should have a look at:

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

In a nutshell, it has to do with the compiler inserting extra space into the struct so that the members of the struct align better.

Community
  • 1
  • 1
Nico Huysamen
  • 10,217
  • 9
  • 62
  • 88