So I have never used smart/auto pointers or seen them used in any production code. One of the big woops about them is that when you copy addresses it nulls the first pointer so you can't dangle it. So I was like man let's see if its a big woop after all.
PHP Code:
#include <stdio.h>
int main(int argc, char* argv[])
{
int *p1 = new int(5);
int *p2 = p1;
delete p1; // recycle heap cell... p2 is now a dangling pointer
*p2 = 420; // segmentation fault imqo
printf("%p = %d\n", p2, *p2); // but wait... 0x14dc010 = 420?????
p1 = 0x0; // set address of p1 to null
printf("%p = %d", p1, *p1); // good. generates segmentation fault error...
return 0;
}
maybe more complex example is needed to throw off the compiler?? I even compiled with optimization off cuz i thought it would be just removing the delete line since we use it right after
Code:
r00t@wutdo:~/dangling$ g++ -g -O0 -fno-inline main.cpp
r00t@wutdo:~/dangling$ ./a.out
0x24d5010 = 420
Segmentation fault
wut da fuk