You use the value of r1 at lines 8, 10, 11 and 12 (and 9 if you uncomment it) before initialising or assigning it (which you do at line 13).
Using an automatic scope variable without initialising it is undefined behaviour. There is no need for further explaination undefined behaviour means anything could happen.
You should always initialise automatic scope variables to some sensible value before using them.
Banfa
Practically a Master Poster
695 posts since Mar 2010
Reputation Points: 508
Solved Threads: 109
Skill Endorsements: 5
It doesn't matter, its just uninitialized memory. Why do you think its a memory pointer? It could just as easily be a signed/unsigned interger...Memory is not self defining, its whatever we(the program) say it is.
gerard4143
Nearly a Posting Maven
2,295 posts since Jan 2008
Reputation Points: 512
Solved Threads: 397
Skill Endorsements: 0
Any uninitialized variable will contain whatever is in that chunk of system memory (automatics are on the stack, so whatever was in that stack space). If you were to run a function in a loop, since the stack when the function is run will always be at the same place, if you don't initialize the variables, they will always have the same data values. A pointer will happily point to whatever is in that stack frame, which in your case is kernel memory, probably because that stack address had previously been used by something that set it that way.
This is why, using uninitialized automatic or class member variables will result in "undefined" behavior. Caveat Programmer! (Programmer Beware!). If you want to use a language that protects you from such things, then don't use C/C++... :rolleyes:
rubberman
Posting Maven
2,581 posts since Mar 2010
Reputation Points: 365
Solved Threads: 308
Skill Endorsements: 52