In the directive, #pragma pack(n), which values are valid values of n?

78 views Asked by At

I did google and found that n= 1,2,4,8 are only valid arguments to the preprocessor directive #pragma pack(n).

Can someone tell me please what's wrong with the values other than the above-mentioned values? (ex- n=3,5, etc why invalid)?

Are the above-mentioned values the only values which can be taken as an argument?

How the values of n are taken and why?

I am using GCC compiler.

1

There are 1 answers

0
Eric Postpischil On BEST ANSWER

Here are three reasons the alignment must be a small power of two.

  1. The GCC documentation says so: The pack value is “always is required to be a small power of two.”

  2. C 2018 6.2.5 4 says alignment must be a power of two: “Every valid alignment value shall be a nonnegative integral power of two.” There is additional text in the clause about maximum alignment (thus limiting it to small powers of two), but it is flexible.

  3. Memory is addressed and organized using binary. Fetching data from memory uses address bits to activate parts within memory devices and to select bytes within words or other groups of bytes. So the alignment boundaries between groupings are located where low bits change. This means the low n address bits for the first byte of a new group are zeros, so the address is a multiple of 2n.