The result of some pointer pointers is described as unspecified. For example, [expr.static.cast] / 13:
A value of type "pointer to cv1 void" can be converted to a prvalue of type "pointer to cv2 T", [...] If the initial value of the pointer represents the address A of the byte in memory and A satisfies the alignment requirement T, then the resulting value of the pointer is that the same address as the original pointer value, that is A. The result of any other such pointer conversion is not specified .
My question is: in the case when the alignment fails, what are the possible results?
For example, the following results are acceptable?
- null pointer
- invalid pointer value (i.e. a pointer that does not point to a dedicated storage of size
T ) - valid pointer to
T in a completely separate piece of memory
Sample code for reference:
#include <iostream> int main(int argc, char **argv) { int *b = (int *)"Hello, world"; // (1) *b = -1; // (2) std::cout << argc << '\n'; }
Line (1) calls my quote from [expr.static.cast] / 13 above because it is reinterpret_cast , which is covered by [expr.reinterpret.cast] / 7, which defines the conversion in terms of static_cast via void * .
If the vague result may be an invalid pointer value, then line (1) may cause a hardware trap. (Ref: N4430 , which clarifies the similar wording that was in C ++ 14 and C ++ 11).
Corollary question: is there a case where line 1 causes undefined behavior? (I do not think that at this stage, since C ++ 14, an invalid reading of a pointer value is determined by the implementation or causes a hardware trap).
It is also interesting that line (2) in most cases would be undefined behavior due to severe violation of aliases (and possibly other reasons), however, if the undefined result could be &argc , then this program could output -1 without triggering undefined behavior !