Reputation: 2675
Take the following struct and class:
struct TestStruct
{
};
class TestClass
{
public:
TestStruct* testStruct;
};
Do the following in main
:
TestClass testClass;
if (testClass.testStruct == NULL)
cout << "It is NULL." << endl;
else
cout << "It is NOT NULL.";
The output will be: It is NOT NULL.
.
However, if I instead do this:
TestClass testClass;
if (testClass.testStruct == NULL)
cout << "It is NULL." << endl;
else
cout << "It is NOT NULL." << endl << testClass.testStruct;
The output will be: It is NULL.
.
Interestingly enough, if I do this (fundamentally the same as above):
TestClass testClass;
if (testClass.testStruct == NULL)
{
cout << "It is NULL." << endl;
}
else
{
cout << "It is NOT NULL." << endl;
cout << testClass.testStruct;
}
The output will be:
It is NOT NULL.
0x7fffee043580.
What is going on?
Upvotes: 2
Views: 196
Reputation: 14530
It is because you didn't initialized the testStruct
member. You have an Undefined behaviour here. It contains garbage value.
If you want it to be always initialized to NULL
, you can do :
class TestClass
{
public:
TestClass(): testStruct(NULL) {}
TestStruct* testStruct;
};
Or with the c++11 way :
class TestClass
{
public:
TestStruct* testStruct{NULL}; // or TestStruct* testStruct = NULL;
};
All the three examples in live : http://ideone.com/ge25Zr
As it is said in the comment, to complete with the C++11 way, you could use nullptr
:
class TestClass
{
public:
TestStruct* testStruct = nullptr; // or TestStruct* testStruct{nullptr};
};
By the way, it is a better practice to keep member attributes private or at least protected. You should create accessors to retreive them.
Upvotes: 2
Reputation: 1313
For me it displays "It is NOT NULL" in both questions and sometimes you may get it as NULL
The reason for above scenario to occur is that C++ doesn't automatically assign anything to a variable, Therefore it contains an unknown value
so that unknown value may be NULL sometimes but sometimes it may be not
the best way to test this theory is to try it in visual C++ and g++ and any other c++ compiler
Yet another reason for you to get either null or not null is that when you compile the application in different ways the compiler outputs different executables so in one undefined variable scenario the compiler may output an executable which when executed may point the undefined variable to a NULL or NOT NULL
warning don't use this code IT'S BAD (this is a test for undefined variable scenario in two compilers)
code (based on OP) :
#include <iostream>
using namespace std;
struct TestStruct
{
};
class TestClass
{
public:
TestStruct* testStruct;
};
int main(){
TestClass testClass;
if (testClass.testStruct == NULL)
cout << "It is NULL." << endl;
else
cout << "It is NOT NULL." << endl << testClass.testStruct;
}
GNU G++
Visual Studio CL
Upvotes: 4
Reputation: 143
Well, the Pointer doesn't get initialized by default. You'll have to do that in the constructor. It just contains what's in your RAM. On usual 32 bit systems the propability of it being NULL is around 0,2e-9. on 64 bit systems ( in 64 bit assemblies) it will be even lower.
Upvotes: 2
Reputation: 13661
Your pointer is not initialized when you declare testClass
. You experience here an undefined behaviour. The value of the pointer will be the last value that was contain in the memory section where it is stored.
If you wanted it to always be NULL
, you would need to initialize it in the constructor of your class.
class TestClass
{
public:
TestClass(): testStruct(NULL) {}
TestStruct* testStruct;
};
Upvotes: 13
Reputation: 3189
The testStruct
will sometimes be NULL and sometimes not be NULL.
Make sure your constructor clears the pointer. Variables in C++ are not default to NULL/0.
Upvotes: 3