Dangling pointer

This is a discussion on Dangling pointer within the Tech Board forums, part of the Community Boards category; Can anyone tell me what's a dangling pointer? An example? As a matter, I'm a little confused on the concept ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    16

    Dangling pointer

    Can anyone tell me what's a dangling pointer? An example? As a matter, I'm a little confused on the concept of pointer in C/C++.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    A pointer is a memory location that points to another memory location. A pointer which is not pointing to anything, is dangling...

    int *pVariable;

    ... is dangling, it has random contents. Stylistically, it is always better to declare a pointer thus...

    int *pVariable = 0;

    ... then later you can test to see if the pointer is dangling because no real world pointer would have the value 0. Now...

    pVariable = new int;

    ... is no longer dangling, because you have created a memory location on the heap, (see the other thread!), and the pointer is pointing to it.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,266
    int pVariable;

    Are you sure about that Adrian? pVariable is pointing to something (Usually 0xCCCCCCCC on Win32), you just don't know what it is. If, on the other hand, you had:-

    int *pVariable;

    I'd be inclined to agree.

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Yeah, typo, we're talking about pointers, they were supposed to be pointers!

    Note to self, avoid answering questions whilst drinking breakfast tea.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    16
    Ok, thanks for answering but I'm still a little unclear. You say a dangling pointer is dangling if it doesnt point to anything, but then you also said a pointer always point to something? huh?

  6. #6
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,825
    > a pointer always point to something?

    If you don't tell the pointer what to point to (by initializing it), the value of the pointer is just random garbage from memory, so it's still technically pointing to something, but it's almost definitely nothing of any use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-24-2008, 11:16 AM
  2. Replies: 0
    Last Post: 03-20-2008, 08:59 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  4. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21