For various reasons, I have some structs I want to force to be specific sizes (in this case 64 bytes and 512 bytes). Both however, are below the somewhat below the sizes I want them to be.
Is there anyway for me to tell the compiler to set them to these specific sizes and pad with zeros, or would I be best off just declaring an array inside the struct that makes up the excess space so that it aligns on the size I want?
You can use a union.
This will ensure the union is 512 bytes or more. Then, you can ensure that it is no more than 512 bytes using a static assertion somewhere in your code:
If you are using C11, there is a better way to do this. I don’t know anybody who uses C11 yet. The standard was published a matter of weeks ago.
Note that the only way to pad with zeroes is to put the zeroes there manually (
callocormemset). The compiler ignores padding bytes.