Why does the root pointer get initialized to null always?

797 views Asked by At

I'm very confused about the following code:

class Tree {
protected:
    struct Node {
        Node* leftSibling;
        Node* rightSibling;
        int value;
    };  
private:
    Node* root;
    int value;
.....
public:
    void addElement(int number) {
        if (root == NULL) {
            printf("This is the value of the pointer %lld\n",(long long)root);
            printf("This is the value of the int %d\n",value);
            ...
            return;
        }
        printf("NOT NULL\n");
    }
};


int main() {
    Tree curTree;
    srand(time(0));
    for(int i = 0;i < 40; ++i) {
        curTree.addElement(rand() % 1000);
    }
}

The curTree variable is local to the main function so I expected it to not have its members initialized to 0, but they are both initialized.

3

There are 3 answers

1
Adam Rosenfield On BEST ANSWER

No, it has unspecified contents. Those contents might be random memory garbage, or they could just happen to be 0, depending on whatever data was left in their memory location beforehand.

It might just happen that due to the way the code was compiled, the particular stack location containing root always has 0 (say, because an earlier local variable occupying that same stack location always ended up as 0). But you cannot rely on this behavior -- you must properly initialize anything before reading it back, otherwise you enter the land of Undefined Behavior.

1
Jesko R. On

The actual default value a pointer is implicitly initialized to will depend on the compiler you use. The Visual C compiler (v2012) will initialize it automatically to __nullptr which is equal to NULL. Have a look at the MSDN doc (see the last example).

I would try to check your compiler manual if you want more information.

0
AudioBubble On

You don't initialize root anywhere, proper place to initialize it would be the constructor.