The first program, which is quite simple, shows the natural alignment in memory for two primitive types through std::alignment_of. By compiling (g++ alignedStorage.cpp) and running the program, we have the following output:
This means that each integer will be aligned at 4 bytes of boundary and with floating-point types aligned to 8 bytes.
In the second program, we need an integer that is aligned to 8 bytes. By compiling it and running the executable, the output would be something like this:
You may have noticed that I've compiled with the -g option (to add debug symbols). We did this to show, with the memory dump in GDB, that the memory of the integer is correctly aligned at 8 bytes:
From the debug session, we can see that through the x/20bd iu (x = memory dump) command, we dumped 20 bytes of the memory after the address of the iu variable. We can see something interesting here: both the iu and ju variables are aligned at 8 bytes. Each memory row displays 8 bytes (test it: 0x7ffc57654470 – 0x7ffc57654468 = 8).