The problem is that the pointer manualpointer
doesn't point to any int
object and so dereferencing it leads to undefined behavior. Thus when you wrote:
*manualpointer = 52; //this is undefined behavior
The above statement, dereferences manualpointer
and so 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.
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.
For example, here the program doesn't crash but here it crashes.
So the first step to make the program correct would be to remove UB(which you can do by creating an int
object and making manualpointer
point to it in your case). 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.