Checking unitialized class pointer

This is a discussion on Checking unitialized class pointer within the C++ Programming forums, part of the General Programming Boards category; Hello. Is there a way to check if a pointer to a class has been initialized or not? For example ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    271

    Checking unitialized class pointer

    Hello.

    Is there a way to check if a pointer to a class has been initialized or not?

    For example with a struct, you can do the following:

    Code:
    struct some_struct* s;
    if(s == NULL)
    {
    ...statement...
    }
    It doesn't seem to work with classes.

    Code:
    class some_class
    {
    ...
    };
    some_class* s;
    if(s == NULL)
    {
    ...
    }
    The execution never stops at the conditional.

    So how do you check if a class pointer has been initialized or not?

    Thanks.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    In both cases, if s is declared globally, it should work (because global pointer variables are initialized to 0 if you don't initialize them explicitly). In both cases, if s is declared local to a class or function, then they may or may not work, since you are using an uninitialized variable.

    There is no difference in these cases between a struct and a class. If you see a difference, it is just lucky (or potentially due to some compiler specific differences between C and C++).

    Also, you don't need the keyword struct in your code in C++.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Then, is there some surefire way to check if it has been initialized?

    Does c++ have some kind of native function like "IsInitialized()"?

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    maybe you just onitted this for brievity, but you should set pointers to 0 or NULL when declared because the compiler will NOT do that for you (unless its a global or static variable)
    Code:
    struct some_struct* s  = 0;
    if(s == NULL)
    {
    ...statement...
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    No, C++ does not keep track of what variables are uninitialized for you. Just remember to initialize them as Ancient Dragon showed. With pointers it is especially easy.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Here's the complete class definition I'm tinkering with:

    Code:
    class node
    {
    	int data;
    	node* left = NULL; // causes error here after I inserted the NULL per dragon's instructions
    	node* right = NULL; // ditto
    public:
    	int lookup(node*, int);
    	void insert(node*, int);
    	void set(int i){data = i;}
    };
    
    int node::lookup(node* n, int target)
    {
    	if(n==NULL) // the execution just bypasses this conditional even when n is unitialized.
    	{
    		return(false);
    	}
    	else
    	{
    		if(n->data == target) return(true);
    		{
    			if(n->data > target) lookup(n->right, target);
    			else lookup(n->left, target);
    		}
    	}
    }
    
    void node::insert(node* n, int input)
    {
    	if(n == NULL)
    	{
    		n = new node;
    		data = input;
    	}
    	if(input > n->data)
    	{
    		insert(n->right, input);
    	}
    	else
    	{
    		insert(n->left, input);
    	}
    }
    This is actually the modification of a bit of code in a tutorial I got on the internet:

    Code:
    struct node {
    int data;
    struct node* left;
    struct node* right;
    };
    
    static int lookup(struct node* n, int target) {
    	// 1. Base case == empty tree
    	// in that case, the target is not found so return false
    	if (n == NULL) {
    		return(false);
    		}
    	else {
    		// 2. see if found here
    		if (target == n->data) return(true);
    		else {
    			// 3. otherwise recur down the correct subtree
    			if (target < n->data) return(lookup(n->left, target));
    			else return(lookup(n->right, target));
    		}
    	}
    }
    
    struct node* NewNode(int data) {
    	struct node* n = new(struct node); // "new" is like "malloc"
    	n->data = data;
    	n->left = NULL;
    	n->right = NULL;
    	return(n);
    }
    
    struct node* insert(struct node* n, int data) {
    	// 1. If the tree is empty, return a new, single node
    	if (n == NULL) {
    		return(NewNode(data));
    	}
    	else {
    		// 2. Otherwise, recur down the tree
    		if (data <= n->data) n->left = insert(n->left, data);
    		else n->right = insert(n->right, data);
    		return(n); // return the (unchanged) node pointer
    	}
    }
    So at the moment, I have no idea whether my modification is fundamentally wrong or not because I can't even get it to work with the conditional. (BTW, I'm using VC++ 2003)

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    Use the constructor to initialize the pointers to null.

  8. #8
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    Ah, that worked.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. My Window Class
    By Epo in forum Game Programming
    Replies: 2
    Last Post: 07-10-2005, 02:33 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM

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