Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Ender on February 05, 2006, 06:18 PM

Title: Null Pointer Question
Post by: Ender on February 05, 2006, 06:18 PM
C++ lets you pass null pointers to functions. When I instantiate my pointer in the function, it doesn't instantiate the pointer that I passed to the function, if this makes sense. Why is this? Is it bad habit to pass null pointers to functions? Some other languages simply don't let you, but C++ does. Here's some example code:

#include <iostream>
#include <string>
#include "BSTree.h"
using namespace std;

void test(BSTree*);

int main(int argc, char* argv[])
{
BSTree* tree = NULL;
test(tree);
if (tree == NULL) {
cout << "Output: NULL." << endl;
} else {
cout << "NOT NULL." << endl;
}
return 0;
}

void test(BSTree* t)
{
t = new BSTree;
}


Output: NULL.
Title: Re: Null Pointer Question
Post by: Kp on February 05, 2006, 06:25 PM
Quote from: Ender on February 05, 2006, 06:18 PMC++ lets you pass null pointers to functions. When I instantiate my pointer in the function, it doesn't instantiate the pointer that I passed to the function, if this makes sense.

It doesn't make sense, because it does change the pointer that you passed to the function.

Quote from: Ender on February 05, 2006, 06:18 PMIs it bad habit to pass null pointers to functions? Some other languages simply don't let you, but C++ does.

It's perfectly fine to pass NULL if the called function is expecting it.

Quote from: Ender on February 05, 2006, 06:18 PM
#include <iostream>
#include <string>
#include "BSTree.h"
using namespace std;

void test(BSTree*);

int main(int argc, char* argv[])
{
BSTree* tree = NULL;
test(tree);
if (tree == NULL) {
cout << "Output: NULL." << endl;
} else {
cout << "NOT NULL." << endl;
}
return 0;
}

void test(BSTree* t)
{
t = new BSTree;
}

The output looks quite correct for what you wrote.  What were you expecting to have happen?
Title: Re: Null Pointer Question
Post by: Ender on February 05, 2006, 06:29 PM
Oh, I think I get it. The pointer isn't assigned an address in memory until you use the new operator? So the pointer I passed to the function doesn't point to the same address as the pointer inside the function?
Title: Re: Null Pointer Question
Post by: Skywing on February 06, 2006, 10:03 AM
You passed the pointer by value, so that only the local copy of the pointer on the argument stack was modified.

I think that here, you want to use either a double indirection (**) or a reference to a pointer (*&), or just return the pointer from the function as a return value.
Title: Re: Null Pointer Question
Post by: Mephisto on February 06, 2006, 01:14 PM
Quote from: Ender on February 05, 2006, 06:18 PM
C++ lets you pass null pointers to functions. When I instantiate my pointer in the function, it doesn't instantiate the pointer that I passed to the function, if this makes sense. Why is this? Is it bad habit to pass null pointers to functions? Some other languages simply don't let you, but C++ does. Here's some example code:

#include <iostream>
#include <string>
#include "BSTree.h"
using namespace std;

void test(BSTree*);

int main(int argc, char* argv[])
{
BSTree* tree = NULL;
test(tree);
if (tree == NULL) {
cout << "Output: NULL." << endl;
} else {
cout << "NOT NULL." << endl;
}
return 0;
}

void test(BSTree* t)
{
t = new BSTree;
}


Output: NULL.


Your output is NULL because you're passing the pointer to the function by value, which simply means instead of passing the tree object itself into the function to be modified, you're passing a copy of it, or its value rather, so when the function does modify it, it's not modifying the actual object, just the local object in the function that you created via the parameter list.

Pass a reference to the pointer or more simply, the address of the object.
test(&tree); // modifies the object declared in main(); not test(); as you had before

Edit: Skywing already replied to the problem, but you may want to review my solution as well if you don't understand his.