Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Note the following points about <code>__attribute__((packed))</code>:</p> <ul> <li><p>When <code>packed</code> is used in a structure declaration, it will compress its fields such, such that, sizeof(structure) == sizeof(first_member) + ... + sizeof(last_member).</p></li> <li><p>Here, an array is just <strong>one member</strong> of the struct. Packing the containing structure of an array will not change the array's size. In fact, the size of (any) array is <strong>always</strong> sizeof(element) * number_of_elements.</p></li> <li><p>Similarly, packing a containing structure of an inner structure will not change the size of the inner structure. The size of a structure is <strong>completely determined by its declaration</strong>, and is the same no matter where you use.</p></li> <li><p>Packing a structure will make its required alignment one byte (i.e. it can be placed anywhere in memory).</p></li> <li><p>Packing will introduce alignment issues when accessing the fields of a packed structure. The compiler will account for that when the the fields are accessed directly, but <strong>not when they are accessed via pointers</strong>. Of course, this does not apply to fields with required alignment one (such as char's or other packed structures). See <a href="https://stackoverflow.com/questions/1756811/does-gccs-attribute-packed/7956942#7956942">my answer to a similar question</a>, which includes a program demonstrating the problem with accessing members via pointers.</p></li> </ul> <p>Finally, to answer the question,</p> <blockquote> <p>Is there any way to guarantee that the data_s structure will have absolutely no additional space added to it or any of its sub-structures so I don't have compiler dependent shifts in the memory map?</p> </blockquote> <p>Yes. Declare the structure as packed, and also all structures that it contains, recursively.</p> <p>Also note that the packed attribute applies to a structure declaration, and not to a type. <strong>There's no such thing as packed version of a structure</strong> that is declared non-packed. When you use a structure somewhere, it (its members) will be packed if and only if the structure itself was declared packed. This is kind of implied by the fact that the size of a structure is completely determined by its declaration.</p> <p><strong>UPDATE</strong>: For some reason you're still confused about arrays. The solution I provided (declare all structures packed) <strong>works with arrays too</strong>. For example:</p> <pre><code>struct elem_struct { uint32_t x; } __attribute__((packed)); // packed guarantees that sizeof(struct elem_struct) = sizeof(uint32_t) = 4 struct array_struct { struct elem_struct arr[10]; } __attribute__((packed)); // packed guarantees that sizeof(struct array_struct) = // = sizeof(struct elem_struct[10]) = 10 * sizeof(struct elem_struct) // = 10 * 4 = 40 </code></pre> <p>The two additional points you made about arrays are true - but only when the structures are not packed. Packing forces the fields of the struct to be continuous, and this <em>does</em> create alignment issues which, if no packing was used, would be solved by inserting empty space between members and padding the struct (see the point I already raised about alignment).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload