Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dereferencing an invalid pointer, then taking the address of the result

Tags:

Consider:

int* ptr = (int*)0xDEADBEEF;
cout << (void*)&*ptr;

How illegal is the *, given that it's used in conjunction with an immediate & and given that there are no overloaded op&/op* in play?


(This has particular ramifications for addressing a past-the-end array element &myArray[n], an expression which is explicitly equivalent to &*(myArray+n). This Q&A addresses the wider case but I don't feel that it ever really satisfied the above question.)

like image 760
Lightness Races in Orbit Avatar asked Sep 08 '11 10:09

Lightness Races in Orbit


People also ask

What happens when we dereference a pointer?

Dereferencing a pointer means getting the value that is stored in the memory location pointed by the pointer. The operator * is used to do this, and is called the dereferencing operator.

How do you dereference a pointer address?

Dereferencing is used to access or manipulate data contained in memory location pointed to by a pointer. *(asterisk) is used with pointer variable when dereferencing the pointer variable, it refers to variable being pointed, so this is called dereferencing of pointers.

What happens if you dereference an uninitialized pointer?

Response:In Hi-Tech compiler if any pointer variable is modified during code execution, the compiler will give a warning "Dereferencing uninitialized pointer" if the pointer variable is not initialised with some address.To overcome this warning , initialize the pointer variable with any address during the pointer ...

What is the invalid about pointer?

An invalid pointer reference occurs when a pointer's value is referenced even though the pointer doesn't point to a valid block. One way to create this error is to say p=q;, when q is uninitialized. The pointer p will then become uninitialized as well, and any reference to *p is an invalid pointer reference.


3 Answers

According to the specification, the effect of dereferencing an invalid pointer itself produces undefined behaviour. It doesn't matter what you do after dereferencing it.

like image 102
Nawaz Avatar answered Sep 30 '22 16:09

Nawaz


Assuming the variable `ptr' does not contain a pointer to a valid object, the undefined behavior occurs if the program necessitates the lvalue-to-rvalue conversion of the expression `*ptr', as specified in [conv.lval] (ISO/IEC 14882:2011, page 82, 4.1 [#1]).

During the evaluation of `&*ptr' the program does not necessitate the lvalue-to-rvalue conversion of the subexpression `*ptr', according to [expr.unary.op] (ISO/IEC 14882:2011, page 109, 5.3.1 [#3])

Hence, it is legal.

like image 33
chill Avatar answered Sep 30 '22 15:09

chill


It is legal. Why wouldn't it be? You're just setting a value to a pointer, and then accessing to it. However, assigning the value by hand must be obviously specified as undefined behavior, but that's the most a general specification can say. Then, you use it in some embedded software controller, and it will give you the correct memory-mapped value for some device...

like image 31
Diego Sevilla Avatar answered Sep 30 '22 16:09

Diego Sevilla