Quantcast
Channel: User CPlus - Programming Language Design and Implementation Stack Exchange
Viewing all articles
Browse latest Browse all 65

Would a structure ever require padding beyond what is required to align the members?

$
0
0

In C a structure can have an arbitrary amount of padding. In theory this implementation conforms to the C standard:

struct X {    int x;    // 1000000000 bytes padding};// sizeof(struct X) == 1000000004

Is there any reason to permit arbitrary (seemingly meaningless) padding? (This will never be found in practice but still a possibility in theory) Is there any reason to not simply:

Structs may add up to the amount of padding required to ensure that the struct and all of the members are properly aligned.

This would put an upper bound on the size of structures, and would ensure consistent memory layouts if a programmer ensures that all members are properly aligned to begin with. I thought purpose of padding was to ensure alignment of the members. If so, why permit padding beyond the bare minimum to ensure the alignment of the members (which may be none if all the members are already aligned)?


Viewing all articles
Browse latest Browse all 65

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>