why the size of struct in C is not equal the sum of all variables size; -


possible duplicate:
why isn't sizeof struct equal sum of sizeof of each member?

this struct , size of 40 size of variables 34. how can eliminate space of 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; 

that's because compiler free insert padding honour alignment requirement. have couple of options.

the first inherently non-portable many implementations provide like:

#pragma pack 

which attempt pack structures tight possible. aware may slow down code, depending on architecture

the other put most-aligned elements 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 offsets in comments, asuming ulonglong 8 bytes, ulong 4 , ushort 2. won't remove padding make easier compiler minimise it.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -