c++ - Deletion of reference underlying -
what happen reference if underlying variable whom referring got deleted?
for e.g. why below code giving 12 output checked both on gcc , msvc
int *x = new int(1); int& y = *x; delete x; x=null; int z = 12; y=z; cout<<"y = "<<y<<endl;
accessing object memory has been deleted undefined behaviour, regardless of whether directly or through reference.
the fact code outputs 12
on gcc , msvc largely irrelevant: output 13
, or 42
, or potato
, still standard-conformant.
Comments
Post a Comment