Structure Alignment?
-
Can any body tell me why structure alignment is necessary in C/C++ and what it may cause? Anil Kumar
Hi Applications should generally align structure members at addresses that are "natural" for the data type and the processor involved. For example, a 4-byte data member should have an address that is a multiple of four. This principle is especially important when you write code for porting to multiple processors. A misaligned 4-byte data member, which is on an address that is not a multiple of four, causes a performance penalty with an 80386 processor and a hardware exception with a MIPSĀ® RISC processor. In the latter case, although the system handles the exception, the performance penalty is significantly greater. /\|-||\/|/\|)
-
Can any body tell me why structure alignment is necessary in C/C++ and what it may cause? Anil Kumar
Or maybe this can help too: All modern CPUs expect that fundamental types like ints, longs and floats will be stored in memory at addresses that are multiples of their length. CPUs are optimized for accessing memory aligned in this way. Some CPUs: allow unaligned access but at a performance penalty; trap unaligned accesses to the operating system where they can either be ignored, simulated or reported as errors; use unaligned addresses as a means of doing special operations during the load or store. When a C compiler processes a structure declaration, it can: add extra bytes between the fields to ensure that all fields requiring alignment are properly aligned; ensure that instances of the structure as a whole are properly aligned. Malloc always returns memory pointers that are aligned for the strictest, fundamental machine type. The specifications for C/C++ state that the existence and nature of these padding bytes are implementation defined. This means that each CPU/OS/Compiler combination is free to use whatever alignment and padding rules are best for their purposes. Programmers however are not supposed to assume that specific padding and alignment rules will be followed. There are no controls defined within the language for indicating special handling of alignment and padding although many compilers like gcc have non-standard extensions to permit this. Summary Structure Alignment Structure alignment may be defined as the choice of rules which determine when and where padding is inserted together with the optimizations which the compiler is able to effect in generated code. /\|-||\/|/\|)
-
Or maybe this can help too: All modern CPUs expect that fundamental types like ints, longs and floats will be stored in memory at addresses that are multiples of their length. CPUs are optimized for accessing memory aligned in this way. Some CPUs: allow unaligned access but at a performance penalty; trap unaligned accesses to the operating system where they can either be ignored, simulated or reported as errors; use unaligned addresses as a means of doing special operations during the load or store. When a C compiler processes a structure declaration, it can: add extra bytes between the fields to ensure that all fields requiring alignment are properly aligned; ensure that instances of the structure as a whole are properly aligned. Malloc always returns memory pointers that are aligned for the strictest, fundamental machine type. The specifications for C/C++ state that the existence and nature of these padding bytes are implementation defined. This means that each CPU/OS/Compiler combination is free to use whatever alignment and padding rules are best for their purposes. Programmers however are not supposed to assume that specific padding and alignment rules will be followed. There are no controls defined within the language for indicating special handling of alignment and padding although many compilers like gcc have non-standard extensions to permit this. Summary Structure Alignment Structure alignment may be defined as the choice of rules which determine when and where padding is inserted together with the optimizations which the compiler is able to effect in generated code. /\|-||\/|/\|)
But as per my understanding the structure members are stored in the contigous memory locations for example struct temp { char a, int b, float c, }; if i declare temp *structPtr = 0x00000000 (Address of element a) then what should be the address of element b if it is 0x00000002 then one byte is waisted. According to you b should have address of multiple of 4. So i can say that there is a trade of CPU performance vs Memory usage Am i right please guide me. Regards Anil Kumar
-
But as per my understanding the structure members are stored in the contigous memory locations for example struct temp { char a, int b, float c, }; if i declare temp *structPtr = 0x00000000 (Address of element a) then what should be the address of element b if it is 0x00000002 then one byte is waisted. According to you b should have address of multiple of 4. So i can say that there is a trade of CPU performance vs Memory usage Am i right please guide me. Regards Anil Kumar
Yes, memory usage versus performance. In Visual Studio you can use the #pragma pack() to control memory alignment on a structure-by-structure basis.
-
Yes, memory usage versus performance. In Visual Studio you can use the #pragma pack() to control memory alignment on a structure-by-structure basis.
Generally the default structure alignment (which you should use unless there is a compelling reason not to) is 8 bytes. This aligns all basic data types (up to doubles) to even multiples of their size. Example (using 8 byte alignment):
struct test
{
char a;
int b;
WORD c;
char d;
double e;
};The offset addresses are: a = 0 b = 4 c = 8 d = 10 e = 16 Total size of the structure: 24 bytes Notice there is some wasted space in there. You can add some variables to this and not increase the size of the structure at all. The following structure is still 24 bytes in size, but is now fully packed.
struct test2
{
char a;
char a2;
WORD a3;
int b;
WORD c;
char d;
char d2;
int d3;
double e;
};