Awareness of the layout and size of data is still very common in game development. Mike Acton did a great talk related to this at CppCon a few years ago : <a href="https://www.youtube.com/watch?v=rX0ItVEVjHc" rel="nofollow">https://www.youtube.com/watch?v=rX0ItVEVjHc</a>
Lost but not forgotten.<p>IDK, most these rules are very trivial and fairly simple to automate. Pretty much boils down to declare the smallest fields first, with a handful of exceptions based on the modulus of size.<p>Ultimately I like Rust’s approach. Without including ‘[repr(C)]/[repr(packed)]’ the compiler is free to layout the structure in the most efficient way for the target platform. And accessing fields with different compiler’s code is UB. You get the speed up, while keeping backwards compatibility.<p>Sadly C/C++ are shackled to this archaic system. Also I believe Go, Nim, and Swift are as well (this is speculation).
clang-tidy has a struct padding check which diagnoses non-optimal struct orders, and a coworker contributed a clang tool to reorder fields: <a href="https://github.com/llvm-mirror/clang-tools-extra/tree/master/clang-reorder-fields" rel="nofollow">https://github.com/llvm-mirror/clang-tools-extra/tree/master...</a>
I tried the example below, saved as `main.c`. It seems she automatically reorders the static mem. Can that behavior be disabled? Can someone please explain?<p><pre><code> #include<stdio.h>
int main(int argc, char** argv)
{
char *p;
char c;
int x;
printf("char p %p\n", &p);
printf("char %p, offset %i\n", &c, (int)((void*)&c-(void*)&p));
printf("integer %p, offset %i\n", &x, (int)((void*)&x-(void*)&p));
return 0;
}
</code></pre>
Output on `gcc main.c; ./a.out`<p><pre><code> char p 0x7ffc5107dff0
char 0x7ffc5107dfeb, offset -5
integer 0x7ffc5107dfec, offset -4</code></pre>
This is going to sound rude but it is not my intention. The reality is that packing DOES NOT matter for most applications these days.<p>I read an article about a person who was the last in the world to speak some obscure language. Some people wanted to start a school to teach others to speak that language, wanted the government to pay for it, etc.<p>The two articles gave me the same combined feel of “I feel a little sorry but not a lot” and “who cares.”<p>The compiler will take care of most.<p>Unfortunately for that person on the other article, he is basically screwed.<p>Don’t let that be you.