Is alignof(T*) is the same for all possible types? What about sizeof(T*)?

194 Views Asked by At

Is alignof(T*) is the same value for all possible types T? What about sizeof(T*)? Please answer based on what is allowed/specified by the standard and not what is the current situation in different compilers.

1

There are 1 best solutions below

0
On

The standard doesn't say much about sizes and alignments of pointers, and thus they are not strictly restricted by the language.

Conversion from one valid pointer to function into another and back is guaranteed to produce the original value. As such, there must be the same minimum number of values that must be representable that is the maximum number of valid addresses for any given pointer to function type which gives lower bound to the size of all pointer to function types.

Conversions between object types have a similar guarantee, which however only applies when the original pointed type has stricter or equal alignment requirement. As a consequence, object pointers to highly aligned types require fewer representable values. If the alignment is high enough, then the type could in theory be smaller.

On systems where conversion between pointer to void and pointer to function is allowed (which is conditionally supported), the minimum number of representable values of pointers to functions and pointers to void must be the same.

But even so, some pointer types could be larger than the number of representable values it needs. Those extra bits would simply necessarily be unused. This would not be very practical.