Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, this is not a VLA, it's a flexible array member. A VLA can only be a automatic variable (i.e a regular variable on the stack), not the member of a struct. This is because lots of things the compiler does depend on knowing the size of the elements it manipulates.</p> <p>Based on your comment, it looks like you're on a 32-bit platform which has 4-byte alignment requirement for pointers and 2-byte alignment for 16-bit integers. That's why you get a sizeof of 64 bytes. For sizeof purpose, it is like there is no flexible array member in your struct, so we can ignore it for now. The flexible array member is a "fake" member, it does not take any space.</p> <p>The compiler adds 2 bytes of padding after the uint16_t to guarantee that in an array of dummy_struct the pointers have 32-bit alignment. Imagine an array of 2 dummy structs, if there is no padding, the first pointer would start right after the uint16_t and it would not be aligned on a 32-bit boundary.</p> <p>Note that you can usually force the compiler not to pad, but there is no portable way for doing this. With gcc for example, you can use <code>attribute ((packed))</code> on the struct. If you do that, sizeof(dummy_struct) will return 62 bytes.</p> <p>For B, you're basically printing the offset of the flexible array member. The C99 standard says:</p> <p><em>As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.</em></p> <p>So when you use ->, the FAM behaves like it's a regular array with the biggest size it could have without changing the size reported by sizeof (64 bytes). In this case, you can only fit a single 16-bit integer without changing the size. In this imaginary struct, there would be no padding and the offset of the imaginary array of one element would be 62 (right after the uint16_t). So that's why you get 62.</p> <p>If your FAM was a int32_t, you could fit 0 element without changing the size (you'd need the 2 bytes of padding to do the alignment). The standard says that this behaves like an array of size 1. So your FAM will be at offset 64 like what sizeof return.</p> <p>Let me re-iterate: if you simply change <code>int16_t array[]</code> into <code>int array[]</code>, A) will still return 64 but B) will now return 64, not 62.</p> <p>This is also what happens in your second example. Your FAM is now an array of pointers. You're on a 32-bit platform and your struct is 4. You can fit 0 element in the FAM without changing the size. So its offset is 4.</p>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    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