'c++ reinterpret_cast char to int* / adjacent bits are repeatedly set as 1100

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



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1