Testing object pointer for null

This is a discussion on Testing object pointer for null within the C++ Programming forums, part of the General Programming Boards category; Hello, I have created several objects for my program: State County Municipality Data Layer The objects are setup as follows: ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    Testing object pointer for null

    Hello,

    I have created several objects for my program:

    State
    County
    Municipality
    Data Layer

    The objects are setup as follows:

    The State object has an array of Countie pointers
    The County object has an array of Municipalities pointers
    The Municipality object has an array of Data Layers pointers

    In other words:

    <State> has 1 to many <Counties> has one to many <Municipalities> has 1 to many <Data Layers>.

    In my program I search a directory structure recursively. The first directory is assigned as a county if the first county element of the State object is null.

    However, I seem to have a tough time testing the county arrary for nulls.

    I use a function entitled getCurCounty to return a pointer to the current county pointer of the State object in the following fashion:

    Code:
    //if the county pointer is null
    			if(StateArg->getCurCounty() == 0)
    			{
    				cout << "About to create a county." << endl << endl; 
    				getch();
    
    				//add a county to the state object
    				StateArg->addCounty(new County(fileStruct.name));
    			
    			}	//end if countyPtr is null
    The getCurCounty() function's code is below:

    Code:
    County * State::getCurCounty()
    {
    	return counties[numOfCounties];
    
    }	//end State::getCounty
    How do I properly test the county arrary of pointers in my state object for nulls? The execution seems to bypass the if statement in the first code segment.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    Have you tried replacing the line
    Code:
    if(StateArg->getCurCounty() == 0)
    with:
    Code:
    if(StateArg->getCurCounty() == NULL)
    I *think* (but don't quote me) that some compiliers distinguish between NULL and 0

    fletch
    "Logic is the art of going wrong with confidence."
    Morris Kline

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > return counties[numOfCounties];

    If you did
    County *counties = new County[numOfCounties];
    Then
    1. If you didn't fill it with NULLs to start with, then it won't return NULL here
    2. counties[numOfCounties] is actually outside the array


    > some compiliers distinguish between NULL and 0
    If you've got a pointer in part of the expression, then 0 and NULL should be the same thing in any decent compiler

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    Originally posted by Salem

    If you did
    County *counties = new County[numOfCounties];
    Then
    1. If you didn't fill it with NULLs to start with, then it won't return NULL here
    That was the exact problem. I fixed it just yesterday. Thanks. I didn't realize you need to initialize the array of object pointers to null.

    Do you always need to intialize arrays in this fashion?
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > Do you always need to intialize arrays in this fashion?
    The only arrays which are initialised by default are true arrays which are either global or static

    Arrays which are local to a function, and everything you get from new (or malloc in C), initially contain garbage, and should be initialised by you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  2. Base-class pointer, accessing object from derived class
    By Korhedron in forum C++ Programming
    Replies: 15
    Last Post: 09-28-2008, 05:30 AM
  3. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. trouble with pointer to object
    By drb2k2 in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2003, 03:10 PM

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