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.
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.