Project 1999

Go Back   Project 1999 > General Community > Off Topic

Closed Thread
 
Thread Tools Display Modes
  #1  
Old 02-24-2012, 03:37 PM
Truth Truth is offline
Banned


Join Date: Nov 2011
Location: h00t
Posts: 842
Send a message via ICQ to Truth Send a message via AIM to Truth Send a message via MSN to Truth Send a message via Yahoo to Truth
Default Dangling Pointer = Fallacy

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 argccharargv[])
{
    
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
Last edited by Truth; 02-24-2012 at 03:57 PM.. Reason: nerdalert
Closed Thread


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 11:48 AM.


Everquest is a registered trademark of Daybreak Game Company LLC.
Project 1999 is not associated or affiliated in any way with Daybreak Game Company LLC.
Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.