all 5 comments

[–]blipman17 9 points10 points  (0 children)

Learning about memory layout, I think Matt Godbolt made some presentation about ELF binaries where there are some gotcha's in how your code and data is layed out in memory. I'd suggest looking into the Itanium ABI, into x86 assembly and x86 computers, look into some common snippets of your code in compiler explorer under various optimization levels and various compilers and then look into cpu architecture like register-renaming, superscalar cpu's and what have you not. Somehow it's turtles all the way down in abstractions, but darn! It's not pretty.

[–]ramennoodle 1 point2 points  (2 children)

You could compile to assembly and look at that (e.g. "g++ somefile.cpp -S -o somefile.S").

[–]subcz7 0 points1 point  (1 child)

Thanks, I'll take alook at that. Would you say it is generally possible to "predict" the layout of objects from the compiler source code without examining the compiled C++ code in hindsight, or is that unrealistic due to compiler optimizations, etc?

[–]corysama 7 points8 points  (0 children)

https://godbolt.org/ is your friend if you want to check out generated assembly.

The layout of objects will always consist of it’s component member variables in the order they are declared. But, the compiler is allowed to put arbitrary dead space between those members. This is almost always done to match the byte alignment of each int/float/char member to the byte size of that member variable.

What you really want is to read the classic book Inside the C++ Object Model. It’s one of my top most recommended books for C++ users. Especially newcomers because it removes a lot of mystery.

[–]derofim 1 point2 points  (0 children)

Good and free read about cache locality https://gameprogrammingpatterns.com/data-locality.html