Thread: Question about Stray or Dangling Pointers

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    14

    Question about Stray or Dangling Pointers

    hello there,

    take a look carefully on this code for example :

    Code:
    #include <iostream>using namespace std;
    
    
    typedef unsigned short USHORT;
    
    
    int main() {
        USHORT * pInt = new USHORT;
        *pInt = 20;
        cout << "*pInt = " << *pInt << "\t&pInt = " << &pInt << endl;
        delete pInt;
    //pInt = 0;
        long * pLong = new long;
        *pLong = 90000;
        cout << "*pLong = " << *pLong << "\t&pLong = " << &pLong << endl;
        cout << "now assigning new value to *pInt\n";
        *pInt = 10 ;
        cout << "dont assigning value to *pInt\n";
        cout << "*pLong = " << *pLong << "\t&pLong = " << &pLong << endl;
        cout << "*pInt = " << *pInt << "\t&pInt = " << &pInt << endl;
        return 0;
    }
    Output:
    Code:
    *pInt = 20      &pInt = 0x28ff1c
    *pLong = 90000  &pLong = 0x28ff18
    now assigning new value to *pInt
    dont assigning value to *pInt
    *pLong = 65546  &pLong = 0x28ff18
    *pInt = 10      &pInt = 0x28ff1c

    1. how come the *pLong value has been overwritten? it is not the same memory slot ?? (!!)


    and if I compile and run the same code including line 11
    Code:
    pInt = 0;

    The program crashes on line 16 when i reassign the value 10 to *pInt.


    2. can someone help me understand why the program crashes after a assign NULL to that deleted pointer?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    cout << "*pInt = " << *pInt << "\tpInt = " << pInt << endl;
        delete pInt;
    //pInt = 0;
        long * pLong = new long;
        *pLong = 90000;
        cout << "*pLong = " << *pLong << "\tpLong = " << pLong << endl;
    Perhaps you should print where the allocated memory is, and NOT the address of the pointer.

    Here, you're likely to see that pInt and pLong point to the same bit of memory.
    Which is allowed since you deleted your pInt and now that memory is being used for pLong.

    Then you come along and break all the rules with *pInt = 10 ;
    You broke your "promise", you hadn't finished with pInt at all.

    > pInt = 0;
    > The program crashes on line 16 when i reassign the value 10 to *pInt.
    Good! It keeps you honest.
    You basically have to decide
    a) did I delete the memory too soon because I needed it
    b) the delete was fine, and the assignment is a mistake.
    You edit the code accordingly.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    1. how come the *pLong value has been overwritten? it is not the same memory slot ?? (!!)
    It's not as alarming as you think. You made the mistake. You deleted pInt and kept using it even though it was released. If you hadn't commented out the part that assigned the pInt variable to 0, the program would have at least crashed.

    2. can someone help me understand why the program crashes after a assign NULL to that deleted pointer?
    On modern systems (and systems older than me) dereferencing a NULL pointer raises a segmentation fault. That pretty well causes a crash. And it's a good thing. A crash is a definite sign of a problem, and your debugger kicks in right after a crash too so you can find out what happened. It's neat.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    14
    Quote Originally Posted by Salem View Post
    Code:
    cout << "*pInt = " << *pInt << "\tpInt = " << pInt << endl;
        delete pInt;
    //pInt = 0;
        long * pLong = new long;
        *pLong = 90000;
        cout << "*pLong = " << *pLong << "\tpLong = " << pLong << endl;
    Perhaps you should print where the allocated memory is, and NOT the address of the pointer.

    Here, you're likely to see that pInt and pLong point to the same bit of memory.
    Which is allowed since you deleted your pInt and now that memory is being used for pLong.

    Then you come along and break all the rules with *pInt = 10 ;
    You broke your "promise", you hadn't finished with pInt at all.

    > pInt = 0;
    > The program crashes on line 16 when i reassign the value 10 to *pInt.
    Good! It keeps you honest.
    You basically have to decide
    a) did I delete the memory too soon because I needed it
    b) the delete was fine, and the assignment is a mistake.
    You edit the code accordingly.

    You said printed the allocated memory address, isn't the &pInt is the allocated? If not how do,I print the allocated memory?


    What you explained me is something I understood immediately but how come pLong has been overwritten if &pLong is different from &pInt?

    If they both were the same, this is a sure cleared thing but they aren't.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Jonathan Yaniv
    You said printed the allocated memory address, isn't the &pInt is the allocated? If not how do,I print the allocated memory?
    Refer to Salem's post #2: print pInt, not &pInt. &pInt is the address of the pointer, but you want the value of the pointer, i.e., the address of what the pointer points to.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    *pInt -> what is stored in the memory you allocated
    pInt -> the place where it is stored
    &pInt -> where the pointer itself is stored

    All your local variables will have different addresses.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    14
    Quote Originally Posted by Salem View Post
    *pInt -> what is stored in the memory you allocated
    pInt -> the place where it is stored
    &pInt -> where the pointer itself is stored

    All your local variables will have different addresses.


    You answered my question.
    Thank you all again guys!

    Edit
    ummm some thing is still not clear.

    Lets say pInt == 5
    So *pInt will give me also 5
    And &pInt will print its address.

    *pInt will not give me the allocated memory address.
    Last edited by Jonathan Yaniv; 06-11-2012 at 11:17 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Lets say pInt == 5
    > So *pInt will give me also 5
    You're still not getting it.
    Code:
    &pInt     pInt           *pInt
    0x123456 |0x11223344| -> 42
    pInt is a variable with a value (0x11223344)
    *pInt takes the address 0x11223344 and tells you that 42 is stored there
    &pInt is the address where pInt itself is stored (memory address = 0x123456)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Jonathan Yaniv View Post
    ummm some thing is still not clear.

    Lets say pInt == 5
    So *pInt will give me also 5
    And &pInt will print its address.

    *pInt will not give me the allocated memory address.
    NO
    *pInt will give a garbage value (or possibly a segfault) as it tries to find the value at memory address 5;
    *(&pInt) will give 5.

    [Edit: Is pInt a number or a pointer here ? If a pointer what is ==5 supposed to mean?]
    Last edited by manasij7479; 06-11-2012 at 11:42 AM.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    [Edit: Is pInt a number or a pointer here ? If a pointer what is ==5 supposed to mean?]
    It's pointer comparison.

    pInt == 5 is a Boolean expression,
    so it will warn but probably do this, since the conversion is defined.
    pInt == ((void*)5)
    which implies pInt is supposed to contain the address 5.

    Not really the way I would ask the question, but I am a native speaker of English.

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by whiteflags View Post
    It's pointer comparison.
    I interpreted his question assuming that he meant pInt is the variable itself.
    (as the idea of comparing a memory address to '5' is quite bizzare and wrong in any high level code)

  12. #12
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    257
    Quote Originally Posted by manasij7479 View Post
    (as the idea of comparing a memory address to '5' is quite bizzare and wrong in any high level code)
    Not that uncommon in embedded programming, though.

  13. #13
    Registered User
    Join Date
    May 2012
    Posts
    14
    Quote Originally Posted by Salem View Post
    > Lets say pInt == 5
    > So *pInt will give me also 5
    You're still not getting it.
    Code:
    &pInt     pInt           *pInt
    0x123456 |0x11223344| -> 42
    pInt is a variable with a value (0x11223344)
    *pInt takes the address 0x11223344 and tells you that 42 is stored there
    &pInt is the address where pInt itself is stored (memory address = 0x123456)
    understood now

    thank you all guys !
    if we go back to the code in the main thread and print the value of pInt and pLong it is really the same.

    thank u !

  14. #14
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > if we go back to the code in the main thread and print the value of pInt and pLong it is really the same.

    If we go back to the code in your other thread, it still has the same problems with undefined behavior. That whole situation occurred because you commented out the line that made one of those pointers NULL like you were told in the other thread.

    You can't use a pointer after delete. If you make a pointer NULL after delete, the computer can actually crash. If you just want to avoid possible crashes and release memory wherever you want, then you will have to live with the weird stuff that happens as a consequence.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer dangling or not
    By vaibhav in forum C++ Programming
    Replies: 16
    Last Post: 08-05-2006, 06:39 PM
  2. Dangling pointer
    By sunnypalsingh in forum C Programming
    Replies: 5
    Last Post: 10-20-2005, 02:28 PM
  3. dangling pointers
    By lydiapeter in forum C Programming
    Replies: 5
    Last Post: 08-31-2005, 03:49 AM
  4. Dangling Else error?
    By OttoDestruct in forum C++ Programming
    Replies: 9
    Last Post: 09-01-2004, 06:25 PM
  5. "Stray pointer" question
    By CppNewbie in forum C++ Programming
    Replies: 1
    Last Post: 01-29-2002, 01:39 PM