Why does the second example find 1100 instead of 0000 after storing the address in the int pointer and then reading the adjacent bits?
Example 1:
char c = 'a';
int val = *reinterpret_cast<int*>(&c);
val = 97 or 0110 0001
Example 2:
char c = 'a';
int* iptr = reinterpret_cast<int*>(&c);
int val = *iptr;
val = -858993567 or 1100 1100 | 1100 1100 | 1100 1100 | 0110 0001
CodePudding user response:
Both have undefined behavior, generally already because accessing a char
through a int
pointer is an aliasing violation.
Even if that was allowed, the size of int
will almost certainly be larger than that of char
and trying to read an adjacent byte of a variable clearly causes undefined behavior.
And even further, the alignment of a char
variable is probably not guaranteed to be large enough for an int
.
All in all, it is wrong to expect any particular output from the programs.
CodePudding user response:
The problem is that you're typecasting a char*
to an int*
and then dereferencing that int*
which leads to undefined behavior.
Undefined behavior means anything1 can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the output of a program that has undefined behavior. The program may just crash.
So the output that you're seeing(maybe seeing) is a result of undefined behavior. And as i said don't rely on the output of a program that has UB. The program may just crash.
So the first step to make the program correct would be to remove UB. Then and only then you can start reasoning about the output of the program.
1For a more technically accurate definition of undefined behavior see this where it is mentioned that: there are no restrictions on the behavior of the program.