I am working on a memory manager using the game engine architecture using a book . I'm currently reading about memory alignment (in a book and on the Internet), and I'm not sure how alignment is used correctly for classes. I understand the concept of memory alignment (for example, a 4-byte data block should be located at an address ending in 0x0, 0x4, 0x8 or 0xC), but allocateAligned()there is a comment in the function in the book that says alignment has to be a force of two. If I have a class that has two intand one char, sizeof(classs)tells me that the class has 12 bytes. So, could you pass 32 as alignment? Could this be a waste of memory and could lead to fragmentation?My question is how classes should be aligned correctly, could you explain it to me in more detail (what happens if you want to align larger pieces of data, for example, 121 bytes), and does it make sense for the larger pieces to be aligned (because the processor only extracts 8 bytes per call, if I am correctly informed)?
allocateAligned()
int
char
sizeof(classs)
Type alignment can never exceed type size. This is due to the fact that the array can not be indented between elements.
It is also important that type alignment may be smaller than the type size.
: , 4- 2- . , 4- 4 , 2- - 2 ( ). 4 . 4- , .
: 12 , 10 - . , 10 , .
, . 32-
struct X { int a; int b; char c; };
, sizeof(X) == 12 alignof(X) == 4. , X , , , 0x1000, 0x1004, 0x1008 0x100c - , X.
sizeof(X) == 12
alignof(X) == 4
X
0x1000
0x1004
0x1008
0x100c
, , sizeof, , sizeof , , .
sizeof