Thread: 0xdddddddd != NULL?

  1. #1
    Registered User NixPhoeni's Avatar
    Join Date
    Aug 2001
    Posts
    10

    Question 0xdddddddd != NULL?

    I have a user-defined class Character, of which fighting is a member of type Character. Within the destructor, I have the following code:

    Character *dp = new Character;
    while(fighting!=NULL) // erase this linked list
    {
    dp = fighting;
    fighting = fighting->next;
    dp->next = NULL;
    delete dp;
    }

    This works, until fighting gets the hex value of 0xdddddddd (I checked in the MSVC++ debugger). It goes through the loop and tries to dereference a NULL pointer when this occurs. How can I make this so that it doesn't go through the loop when fighting == NULL or 0xdddddddd? I tried the corresponding C++ code, but it says there is no operator that can convert from Character * to int (it may have said unsigned long, but I know it was an int). Any help is appreciated.
    -Joe
    -------------------------------------------
    To understand a program you must become both the machine and the program.
    --Alan Perlis

  2. #2
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    MSVC++ uses a different *representation* of a null pointer
    to be 0xdddddddd to trap memory problems in debug releases.
    I'm not sure of the specifics though you should know the value
    of a null pointer and the representation can be different.

  3. #3
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    I think maybe I got it wrong, sill not very sure though, msvc++ sets a pointer to 0xdddddddd after deleting or freeing it. So you basically have dangling pointers and it would be a major
    bug to check against 0xdddddddd.

  4. #4
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    General recommendation. Use the STL list, rather than having characters a list item themselves. It will save a lot of headaches, and reusing well written code when applicable is always a good idea.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  5. #5
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    actually null pointers in VC always turn out 0xcccccccc for me so are you sure its valid memory?

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    72
    Hi
    You should rewrite slightly your code for destructor when have in mind that when you invoke the delete dp operator the destuctor for that object is invoked too and the fighting pointer is not valid anymore - it points to memory which is freed inside the destructor of of the dp object.

    damyan

  7. #7
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Not to create some misunderstandings here:

    In MSVC, a NULL pointer is ALWAYS 0x00000000 !
    Release build, debug build, hardisk upside down,
    it's ALWAYS 0x00000000. No exceptions. Anything
    else would be a horrible mess.

    What you are expiriencing is an uninitialized pointer.
    When you don't initialize a pointer, or don't set it to
    a new value after you called delete on it, MSVC will
    set it to a value like 0xcdcdcdcd to symbolize it has
    no value. So you can see that a pointer is not
    initialized instead of wondering why you program
    crashes when you try to call a function on your
    pointer 0x139abc87 which in reality is just a bunch
    of random numbers.

    So what you are getting at here is that you have a
    pointer in your list that is either not initialized,
    or has been deleted previously.

    Code:
    Character *dp = new Character; 
    while(fighting!=NULL) // erase this linked list 
    { 
    dp = fighting; 
    fighting = fighting->next; 
    dp->next = NULL; 
    delete dp; 
    }
    Why does dp have to be a new Character, when the
    first thing you do is lose it's value ( dp=fighting; ) ?

    Another thing to check: Is the list always teminated
    by an element that has NULL as a next value ?

    Next Question: Is fighting INITIALIZED to NULL in your
    constructor ? If not, it might fail at any instance in your
    list.

    Suggestion:
    If you want to delete a list of classes, use this code:
    Remember that when you call delete, the destructor
    is called.

    Code:
    CCharacter::~CCharacter()
    {
         // this calls the destructor of the instance 
         // that is stored in fighting, which in turn 
         // will delete it's predecessors in it's own
         // destructor
         if( fighting != NULL ) delete fighting;
         fighting = NULL;
    }
    Another thing that comes to mind: Make sure you have
    each instance only once in this list.

    Example:

    A->B->C will be fine

    A->B->A means trouble
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "Virtual Printer" or "Moving Printjobs"
    By extasic in forum Windows Programming
    Replies: 12
    Last Post: 06-30-2011, 08:33 AM
  2. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  3. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  4. Compiling 3rd party code problem me too
    By siavoshkc in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 05:55 AM
  5. Really Need Help With My Opengl Camera!!!!
    By psychopath in forum Game Programming
    Replies: 13
    Last Post: 05-28-2004, 03:05 PM