0

I was trying to understand how objects create and initialize constructors. I want to create an object with a "person class" with "*name" feature and test if the name pointer is NULL or not once I create a object.

I first test with dynamic allocation "new" to create the object and the pointer is null. However, If I use static to create the object and the pointer is not null.

class person{
public:
    string *name;
person(){
}
~person(){}
};

The above is class definition.

int main(){

person *jack = new person();
cout << jack->name << endl;
if(jack->name == NULL){
    cout << "is null" << endl;
} else{
    cout << "not null" << endl;
}
return 0;
}

The above will output: 0x0 , is null

int main(){
person louis = person();
cout << louis.name << endl;

if(louis.name == NULL){
    cout << "is null" << endl;
} else{
    cout << "not null" << endl;
}
return 0;
}

The above will output: 0x7ffeee80b918, not null

Moreover, if I put the two together:

int main(){

person *jack = new person();
cout << jack->name << endl;
if(jack->name == NULL){
    cout << "is null" << endl;
} else{
    cout << "not null" << endl;
}
person louis = person();
cout << louis.name << endl;
if(louis.name == NULL){
    cout << "is null" << endl;
} else{
    cout << "not null" << endl;
}

return 0;
}

The above will output: 0x0, is null, 0x0, is null.

I was trying to figure out why using "new" or not will have a different result on the "name" pointer?

With "new" the name pointer will be NULL Without "new" the name pointer will not be NULL

Moreover, If I put create two objects, the first one creates with new and the second one creates without new. the result will all become NULL. why the first object will affect the name pointer on the second object?

  • 2
    Unrelated: Why are you using a _pointer_ to a `string` in your class? It's uninitialized in `cout << jack->name << endl;` so you get _undefined behavior_. Why are you using pointers at all for any of this? – Ted Lyngmo Jan 15 '22 at 21:40
  • 2
    You're looking at [undefined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior). The "why" is "because your compiler felt like it". When data is uninitialized, it can have whatever value. It might be 0, it might be whatever was at that position in memory, or it might be some constant sentinel value. – Silvio Mayolo Jan 15 '22 at 21:44
  • 4
    Simply put -- a pointer that is not initialized is not usable until it is initialized. Writing code to see what an uninitialized pointer does is pointless -- you don't learn anything really. The only thing you need to know is -- a pointer must point somewhere valid before you can utilize it -- no excuses, regardless of what output you are getting. – PaulMcKenzie Jan 15 '22 at 22:10

1 Answers1

0

You are dealing with "undefined behavior." So instead of trying to differentiate between the output for both types of initialization, change your declaration of name in your class definition from string *name; to string *name = nullptr; to guarantee that name starts off as NULL

Eitan
  • 101
  • 3