Wednesday, 15 June 2011

c++ - Does `->` implies unreferencing? -



c++ - Does `->` implies unreferencing? -

i'm using code below find alignment properties of attributes. know storing null pointer defined behaviour , pointer operations defined behaviour , unreferencing null (and other invalid values) pointer invokes undefined behaviour. questions simple: -> implies unreferencing pointer (thus causing undefined behaviour in code below)?

class="lang-cpp prettyprint-override">#include <iostream> void f(void *p) { std::cout << p << std::endl; } struct x { int a; int b; }; struct y { int a[2]; int b; }; int main() { x *x = null; y *y = null; f(&x->b); f(&y->b); }

the -> operator combination of * , . operators.

x *x = (struct *)malloc(sizeof(struct)); f(&x->b);

it perform indirection on x locate construction points to, selects b fellow member of structure. similar phone call

f( &(*x).b ); // same f( & ( (*x).b ) );

since in case x null pointer, dereferencing null pointer invokes undefined behavior: c++11: 8.3.2 references (p4):

note: in particular, null reference cannot exist in well-defined program, because way create such reference bind “object” obtained dereferencing null pointer, causes undefined behavior.

c++ pointers alignment

No comments:

Post a Comment