Structures in C: From Basics to Memory Alignment
I&#x27;ll try not to be too pedantic.<p>&gt; If we declare a structure variable without initializing it, like any other variable in C, it will be uninitialized at first and may contain random values.<p>Really, it&#x27;s important to stress that as far as you are concerned, uninitialized things don&#x27;t contain values, they only contain undefined behavior.<p>&gt; But in complex programs, a structure can easily have 20 members and more.<p>Mostly in poorly designed programs.<p>&gt; The only reason not to use it is if you are forced to work with a C89 compiler and can’t upgrade.<p>Or if you have a Vector2D and don&#x27;t need to be constantly reminded what comes after x.<p>&gt; But what if we want a struct to feel like a real C type?<p>It&#x27;s already a real type. The reason people typedef structs is to save typing 7 characters.<p><pre><code>    #include &lt;stdlib.h&gt;
    
    ...
    
    struct Vector2D *vec;
    vec = malloc(sizeof(struct Vector2D));
    if (sec == NULL) {
        &#x2F;&#x2F; handle allocation failure
    }
</code></pre>
I recommend using sizeof *vec here, instead of sizeof (struct Vector2D), it&#x27;s much harder to screw up and mix up types in my experience.<p>&gt; The memory is uninitialized so it is a good idea to initialize it to zero bytes. We can do this by calling the memset function with the pointer to our new struct, the initialization value 0, and the size of our structure:<p>Don&#x27;t do this by calling memset. There&#x27;s no guarantee that memsetting a pointer, float or double to all bytes zero will actually produce things which equal zero. On implementations where null pointers are not all bits zero (rare but they do exist) you will not get a null pointer.<p>&gt; The great thing about this is that it allows us to omit to allocate structs with malloc on the heap.<p>Nothing stops you from using pointers to struct typed objects with automatic storage duration when calling functions:<p><pre><code>    struct Vector2D vec;
    foo(&amp;vec);
 
</code></pre>
&gt; This macro is useful to check if the compiler added any padding in between the members of the structure.<p>I would say this is hardly a use, more of a curiosity, really you shouldn&#x27;t write code which relies on the presence or absence or width of padding, you can&#x27;t even reliably store information in padding (the value is free to change due to you writing to an unrelated member). The only situation where this makes any sense is when using packed structs, and those almost never make sense except for some very specific circumstances.<p>&gt; It can also be used to get the memory address of the structure if you only have the address of one of its members and know what type of struct it is a member of.<p>I haven&#x27;t been able to gather agreement on whether this is something actually allowed by the C standard. There are two main schools of thought about this and one allows it, the other prohibits it, it&#x27;s an extreme example of a total grey area in the C standard.<p>&gt; This is used in some advanced code e.g. the OOP implementation of the Linux Kernel.<p>I think calling what Linux does OOP is misleading. The kernel just has lots of vtables, I disagree that OOP is just about vtables (or the effect they give).<p>&gt; The most general reason is that one of the design goals of C was to be a language that can be implemented on as many hardware platforms as possible. Therefore the standard needs to be flexible to allow compilers to adapt the actual implementation to the specialties and quirks of their hardware respective platforms.<p>I mean, it&#x27;s actually more about ABI than hardware. You can have two ABIs with different padding requirements on the same hardware platform. It just so happens that ABIs are themselves usually designed with hardware in mind.<p>&gt; And here we already see the solution. A structure has to get enough trailing padding to align with its biggest data type.<p>While this is not a bad way of thinking about it, again, really, it&#x27;s important to stress that while developing code in C, you should NOT be relying on this for anything other than performance optimisations for a particular platform.<p>&gt; (e.g. because you want to map a file format or some hardware registers exposed in memory)<p>While using struct packing to deal with hardware registers is forgivable (although, rare, given that hardware registers will often likely be aligned the same as in the ABI), you really shouldn&#x27;t use it for any file format you want to be portable outside a single machine. With modern compilers there&#x27;s effectively no penalty to doing this properly (i.e. defining functions like uint_least32_t read32le(void *p) which read byte by byte and de-serialize the number using shifts and ORs). Yes I have tested this. Not only will your code not be cryptic and broken the moment you find yourself on a big endian machine, it also won&#x27;t be unnecessarily portable for no good reason.<p>&gt; Thankfully, C supports so-called bitfields.<p>You make it sound like an array&#x2F;struct of bools or a bitfield are the only two options.