Search code examples
clanguage-lawyerfunction-parametermemory-layout

Storage layout of C objects


The storage layout of C objects is mostly not defined. As far as I know only for struct members and array elements, the layout is defined.

Interestingly for function parameters the C11 standard explicitly mentions that the layout is not defined:

The layout of the storage for parameters is unspecified. (C11 § 6.9.1 P 9)

I was wondering if the standard also explicitly defines that for other objects, e.g., objects with automatic storage duration, the layout is undefined. Is someone aware of this? I couldn't find anything about this in the standard.

What about objects with internal or external linkage?


Solution

  • No, I don't think that there is an explicit mention of the fact that we can't know anything about relative object layout. In fact, the C standard is even more radical than that, you are not even allowed to do comparison with the < operator on two variables that are not elements of the same array, nor may you do arithmetic between pointers to objects that are not part of the same array.

    So the whole question of "layout" cannot even be formulate with the terminology that the C standard provides.