Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ Pointers: Testing for Truth Value

Options
  • 04-03-2004 3:35pm
    #1
    Closed Accounts Posts: 373 ✭✭


    If I test a C++ pointer for its truth value, am I right in saying the following:

    An initialised pointer will return True.
    An unitialised pointer or one initialised to NULL will return False.

    ??

    I have created a Linked List of a Vectors for use in an OpenGL coded SpaceInvaders that myself and a friend are doing for a college project.
    My Vector class contains 3 floats, and next Vector and previous Vector pointers.

    What I am testing is the truth value of a Vector pointer - i.e. to find out if the next node in the list is valid/has been initialised yet or not.

    The relevant code is below. The BOLD bits are the bits that are directly related to the problem im having.

    Vector.h
    #ifndef VECTOR_H
    #define VECTOR_H
    
    #include <iostream>
    using namespace std;
    
    
    class Vector
    {
    	public:
    		Vector();
    
    		float x;
    		float y;
    		float z;
    		bool bob;
    
    		Vector *next;
    		Vector *previous;
    
    		~Vector();
    };
    
    #endif
    

    Vector.cpp
    #include <stdlib.h>
    #include "Vector.h"
    
    Vector :: Vector()
    {
    	x = 0;
    	y = 0;
    	z = 0;
    
    	bob = false;
    
    	next = NULL;
    	previous= NULL;
    
    }
    
    
    Vector::~Vector()
    {
    }
    

    VectorList.h
    #ifndef VECTORLIST_H
    #define VECTORLIST_H
    
    #include <iostream>
    #include "Vector.h"
    using namespace std;
    
    class VectorList
    {
    	public:
    		VectorList();
    
    		void add(float xx, float yy, float zz);
    
    		[b]Vector* getCurrent();
    		void advanceCurrent();[/b]
    
    		void clear();
    
    		~VectorList();
    
    	//private:
    		Vector *head;
    		Vector *tail;
    		Vector *current;
    };
    
    #endif
    

    VectorList.cpp
    #include <stdlib.h>
    #include "VectorList.h"
    #include "Vector.h"
    
    VectorList::VectorList()
    {
    	head = NULL;
    	tail = NULL;
    	current = NULL;
    }
    
    void VectorList::add(float xx, float yy, float zz)
    {
    	if (head == NULL)
    	{
    		head = new Vector();
    
    		head->x = xx;
    		head->y = yy;
    		head->z = zz;
    		head->bob=true;
    
    		tail = head;
    		current = head;
    	}
    
    	else 
    	{
    		Vector *vect = new Vector();
    		vect->x = xx;
    		vect->y = yy;
    		vect->z = zz;
    		vect->bob = true;
    		
    
    		tail->next = vect;
    		vect->previous = tail;
    		tail = vect;
    
    	}
    
    }
    
    [b]	
    Vector* VectorList::getCurrent()
    	{
    		return current;
    	}
    		
    
    void VectorList::advanceCurrent()
    	{
    		current = (current->next);
    	}[/b]
    
    void VectorList::clear()
    {
    	Vector *walker = head;
    	while(walker != NULL)
    	{
    		Vector *temp = walker;
    		walker = walker-> next;
    		delete temp;
    	}
    	head = tail = NULL;
    
    }
    
    
    VectorList::~VectorList()
    {
    	clear();
    }
    
    


    Main Loop that gets called for every frame to be drawn
    void RenderScene() 
    {
    	int i=0;	
    
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	// Clear The Screen And The Depth Buffer
    	glLoadIdentity();									// Reset The matrix
    	
    		// 	  Position      View	   Up Vector
    	gluLookAt(0, 0, 6,     0, 0, 0,     0, 1, 0);		// This determines where the camera's position and view is
    	
    	
    
    	movingRowAliens(); //Draw the row of "Aliens" moving down the screen.
    	
    	drawSky();			//Draw the stars in background
    	
    	
    	static VectorList *vectList = new VectorList();	 //Create a Linked List of Vectors. 
    						                   //Make it Static so it only gets initialised once.
    	static int counter = 0;
    	
    	if (!counter)
    	{
    		vectList->add(0.0, -2.05f, 0.0);
    	}
    	else 
    		if (!(counter%100))
    	{
    		vectList->add(0.0, -2.05f, 0.0);
    	}
    	
    
    	[b]while(vectList->getCurrent()) //While the current Vector pointer is initialised. 
    	{	
    		fireShot(vectList);
    		vectList->advanceCurrent();
    	}[/b]
    
    	counter++;
    
    	SwapBuffers(g_hDC);									// Swap the backbuffers to the foreground
    }
    

    The fireShot() method above works on the current node of the list using its Vector to draw the shot/fire(s) on the sceen in that frame.


Comments

  • Closed Accounts Posts: 423 ✭✭Dizz


    An initialised pointer will return True.
    True - returns a value > 0 - its memory address
    An unitialised pointer or one initialised to NULL will return False.
    True - look the definition of NULL in your systems headers

    Win32 systems - From windef.h

    [PHP]
    #ifndef NULL
    #ifdef __cplusplus
    #define NULL 0
    #else
    #define NULL ((void *)0)
    #endif
    #endif
    [/PHP]


  • Registered Users Posts: 1,391 ✭✭✭fatherdougalmag


    Never trust an uninitialised pointer - especially local ones. Probably varies from compiler to compiler but global data is typically initialised to 0/NULL. When a local variable (incl. pointer) is declared it can sometimes contain a random value. As a rule of thumb you should initialise all variables when declared.


  • Closed Accounts Posts: 373 ✭✭Faltermyer


    Ok, so why when I test like above or like:
    while( (vectList->getCurrent()) != NULL)
    {
       fireShot(vectList);
       vectList->advanceCurrent();
    }
    


    It does not go into the loop, ever!??
    The first Pointer/node in the list is definitely initialised, with values.


  • Closed Accounts Posts: 373 ✭✭Faltermyer


    Eureeka!!

    Sorry bout this lads,... but it WAS going into the loop, but only once so the result on screen was too quick for the naked eye.

    Have solved it now, I was not reseting the "current" pointer to the beginning of theLinked List each time round!

    feel like an idiot now!

    Ah well, I now understand pointer properly having laboured with it all week!


  • Closed Accounts Posts: 437 ✭✭casper-


    Originally posted by fatherdougalmag
    Never trust an uninitialised pointer - especially local ones. Probably varies from compiler to compiler but global data is typically initialised to 0/NULL. When a local variable (incl. pointer) is declared it can sometimes contain a random value. As a rule of thumb you should initialise all variables when declared.

    I believe you can almost be completely certain that an uninitialised pointer on Windows will definitely _not_ be NULL in the release build (but it will be in the debug one).

    I'm not sure about global pointers--- but why are you using those anyway in C++? :) That's what singletons are for ....


  • Advertisement
Advertisement