The C++ compiler may generate extra fields for
classes that contain base classes or virtual functions. Objects
of these types may not conform to the mapping shown below.
| Type | The definition of the
structure variable is preceded by the #pragma
pack directive or the keyword
_Packed in C, or the /Sp compiler
option is used. For example,
the following lines define a packed struct called
mystruct#pragma pack(1);
struct mystruct {
char char1;
short short1;
char char2;
float float1;
char char3;
};
|
||||||||||||||||||||||||||||||||||||||||||||
| Size | The sum of the sizes of each type that makes up the struct. | ||||||||||||||||||||||||||||||||||||||||||||
| Storage Mapping |
When the _Packed keyword,
the #pragma pack(1) directive, or /Sp1 option is used,
the structure mystruct is stored as shown below. High memory is to the right.
When #pragma pack(2) or
the /Sp2 option is used, mystruct is stored as shown
below
|
![]()
Summary
of Compiler Options
Keywords
in C and C++