Hello, I'm new here
I have a question about this statement:
*(int *)0 = 0;
Could anybody explain what this snippets exactly does? And why it compiles but gives a segfault when executing?
Thanks a lot in advance!
Hello, I'm new here
I have a question about this statement:
*(int *)0 = 0;
Could anybody explain what this snippets exactly does? And why it compiles but gives a segfault when executing?
Thanks a lot in advance!
it is equivalent to the following code
Could you see why it segfaults?Code:int * pTemp = NULL; *pTemp = 0;
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Becouse you are trying so set the value 0 in an unknown memory address?
But I've never seen that snippet before so i thought it was malformed too..but C compiler get it right :/ I thought it wouldn't either compile, why that statement is "syntatically" correct by the way?
EDIT:
Oh...0 is seen as NULL by C compilers?! (i'm using gcc)
0 is a null pointer constant.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
It is correct but it means the address stored is the NULL address. If you want to actually set the value then you allocate and dreference. But yes, what you wrote is logically equivalent.
Next time post your code in [code]/*your program*/[/code].
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson
Code - functions and small libraries I use
It’s 2014 and I still use printf() for debugging.
"Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson