• Welcome to Valhalla Legends Archive.
 

Null Pointer Question

Started by Ender, February 05, 2006, 06:18 PM

Previous topic - Next topic

Ender

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.

Kp

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?
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

Ender

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?

Skywing

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.

Mephisto

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.