tags:

views:

185

answers:

5

What is the meaning of

*(int *)0 = 0;

It does compile successfully

+1  A: 

Its a compilation error. You cant modify a non-lvalue.

Visage
actually it is *(int*)0=0
sameer karjatkar
Then thats a runtime error - you are trying to dereference a pointer to the memory location 0.
Visage
Actually, it is undefined behaviour.
anon
It's not the location 0 as some other posts also say. It's a location where no objects must be located. It *may* be location 0, but it may aswell be some other location.
Johannes Schaub - litb
A: 

there is no valid lvalue in that operation so it shouldn't compile.

the left hand side of an assignment must be... err... assignable

Luke Schafer
actually it is *(int*)0=0
sameer karjatkar
+4  A: 

It has no meaning. That's an error. It's parsed as this

(((int)0) = 0)

Thus, trying to assign to an rvalue. In this case, the right side is a cast of 0 to int (it's an int already, anyway). The result of a cast to something not a reference is always an rvalue. And you try to assign 0 to that. What Rvalues miss is an object identity. The following would work:

int a;
(int&)a = 0;

Of course, you could equally well write it as the following

int a = 0;

Update: Question was badly formatted. The actual code was this

*(int*)0 = 0

Well, now it is an lvalue. But a fundamental invariant is broken. The Standard says

An lvalue refers to an object or function

The lvalue you assign to is neither an object nor a function. The Standard even explicitly says that dereferencing a null-pointer ((int*)0 creates such a null pointer) is undefined behavior. A program usually will crash on an attempt to write to such a dereferenced "object". "Usually", because the act of dereferencing is already declared undefined by C++.


Also, note that the above is not the same as the below:

int n = 0;
*(int*)n = 0;

While the above writes to something where certainly no object is located, this one will write to something that results from reinterpreting n to a pointer. The mapping to the pointer value is implementation defined, but most compilers will just create a pointer referring to address zero here. Some systems may keep data on that location, so this one may have more chances to stay alive - depending on your system. This one is not undefined behavior necessarily, but depends on the compiler and runtime-environment it is invoked in.

If you understand the difference between the above dereference of a null pointer (only constant expressions valued 0 converted to pointers yield null pointers!) and the below dereference of a reinterpreted zero value integer, i think you have learned something important.

Johannes Schaub - litb
actually it is *(int*)0=0
sameer karjatkar
Just to pile on here, yeah it'll compile, but the runtime behavior is undefined: it might actually assign 00 00 00 00 to the first bytes in the address space or it might bork your process, your mileage may vary by platform, etc.
Jegschemesch
+3  A: 

It will usually cause an access violation at runtime. The following is done: first 0 is cast to an int * and that yields a null pointer. Then a value 0 is written to that address (null address) - that causes undefined behaviour, usually an access violation.

Effectively it is this code:

int* address = reinterpret_cast<int*>( 0 );
*address = 0;
sharptooth
Whether it causes an access violation or not depends on the hardware and/or OS the executable is running on. The most you can say from the question is that it will cause undefined behaviour.
anon
Yeah that's why he said "usually" :)
Mladen Jankovic
I said "usually" in responce to Neil's comment. He's right that undefined behaviour can be just anything.
sharptooth
+1  A: 

It puts a zero on address zero. On some systems you can do this. Most MMU-based systems will not allow this in run-time. I once saw an embedded OS writing to address 0 when performing time(NULL).

stefaanv