Valhalla Legends Forums Archive | C/C++ Programming | Null Pointer Question

AuthorMessageTime
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:
[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;
}
[/code]
[code]
Output: NULL.
[/code]
February 6, 2006, 12:18 AM
Kp
[quote author=Ender link=topic=14158.msg144742#msg144742 date=1139185137]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.[/quote]

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

[quote author=Ender link=topic=14158.msg144742#msg144742 date=1139185137]Is it bad habit to pass null pointers to functions? Some other languages simply don't let you, but C++ does.[/quote]

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

[quote author=Ender link=topic=14158.msg144742#msg144742 date=1139185137][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;
}
[/code][/quote]

The output looks quite correct for what you wrote.  What were you expecting to have happen?
February 6, 2006, 12:25 AM
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?
February 6, 2006, 12:29 AM
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.
February 6, 2006, 4:03 PM
Mephisto
[quote author=Ender link=topic=14158.msg144742#msg144742 date=1139185137]
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:
[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;
}
[/code]
[code]
Output: NULL.
[/code]
[/quote]

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.
[code]test(&tree); // modifies the object declared in main(); not test(); as you had before[/code]

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

Search