I am bothered by the fact that, in C, struct
types can be arbitrarily large. We have no control over their memory layout except for the fact that the first member is always at the beginning of the memory and the elements are arranged in the order we defined them in. A 'conforming' but never to be seen in practice C implementation may handle a struct
as follows:
struct S { char x; // 1 byte // 999999999 bytes padding char y; // 1 byte // 999999999 bytes padding short z; // 2 bytes // 999999998 bytes padding}; // Total size: 3 GB
To solve such problems, I am considering implementing a packed
keyword to my programming language. packed struct
would require using the least amount of space with no padding, and that all elements immediately follow each other. packed enum
would use the smallest type that can hold all enumerators instead of some default type such as int
.
What are the implications to implementing such a feature? Are there reasons, performance, compatibility, or otherwise, to refrain?