Variable size/Set: Difference between revisions

→‎{{header|C}}: Unions and padding.
(→‎{{header|TXR}}: Notes about anon padding.)
(→‎{{header|C}}: Unions and padding.)
Line 68:
 
Here <var>foo</var> is a signed integer with at least 32 bits. [[wp:stdint.h#Minimum-width integer types|stdint.h]] also defines minimum-width types for at least 8, 16, 32, and 64 bits, as well as unsigned integer types.
 
<lang c>union u {
int i;
long l;
double d;
/* ... */
};</lang>
 
Here the use of <code>union</code> results in a datatype which is at least as large as the largest type. Unions are sometimes exploited to just meet a minimum size:
 
<lang c>union must_be_at_least_512_bytes {
int interesting_datum;
char padding[512];
};</lang>
 
Here, the application will never access <code>padding</code> nor store anything; the padding is there to make the type large enough to meet some requirement. For instance, so that some third party API function which fills in the object, doesn't write past the end of the memory, when the program is only interested in <code>interesting_datum</code>.
 
=={{header|C++}}==
543

edits