Hi, from my understanding, if you initialize an object without the "new", it will be only saved in the stack. If the method exits, the object is destroyed. Is that correct? What about this code?

int main() {
	string xxx = getText();
	cout << "from main() " << xxx << endl;
}

string getText() {
	string text("Hello World!");
	cout << "from getText() " << text << endl;
	return text;
}

output is
from getText() Hello World!
from main() Hello World!

In getText() method, I created a string without the "new", so I assumed that the string will be destroyed after the return. But why does xxx in main() equal to the return of the getText() method? Should it be that xxx contain an unknown value since the object it points to was destroyed? Thanks a lot!

Recommended Answers

All 7 Replies

I could see there being a problem if you made xxx a pointer to getText() and some how getText() got destroyed then it would be pointing to an object that no longer exists.

The example you posted is assigning xxx to the value your getText() function returns. So xxx is equal to "Hello World!" and is not equal to getText().

So, if xxx is now equal to "Hello World!", then is it ok if the string in getText() was destroyed? What I'm trying to do is to make the xxx equal to "Hello World!" even if the string in getText() was destroyed.

That is exactly what you have.
If xxx was a pointer to getText() then you would have a problem.

Thanks man problem solved!

>>Hi, from my understanding, if you initialize an object without the "new", it will be only saved in the stack.
Correct, otherwise it would be saved in heap

>>If the method exits, the object is destroyed. Is that correct? What about this code?

Ok lets see,

int main() {
      ///xxx is declared in stack
     //its scope, i.e its lifetime is when the program reaches the 
    //end of this function
	string xxx = getText();  //Look at the function's comment
  
     //thus xxx gets a copy of the string text in getText().
	cout << "from main() " << xxx << endl;
}
 
string getText() {
     //text is declared on stack
    //its lifetime or scope is when the program reaches the 
    //end of this function
	string text("Hello World!"); 
	cout << "from getText() " << text << endl;
	return text; //returns a COPY of text
}

>>In getText() method, I created a string without the "new", so I assumed that the string will be destroyed after the return.
Yep, its destructor is called.

>> But why does xxx in main() equal to the return of the getText() method? Should it be that xxx contain an unknown value since the object it points to was destroyed? Thanks a lot!

What happens with your functions is that getTex() is called first,
the inside that function you create a string object, and do some
stuff and at last you return the string object. When you return the string object, the compiler goes to the memory address of the string object, checks its value, finds another memory to save this temporary value, and deletes the string object. Now the compiler is left with a temporary value stored at a temporary address, this
temporary data is what is returned to the string xxx, not the string text inside your function. When the temporary data is returned,
string xxx invokes the copy constructor and makes a deep copy
of the temporary data returned.

Was that ok to understand?

Thanks man. Good explanation. But is it still the same when returning any object? I believe that it is. I just want to make sure :)

class Person {
private:
    string name;
public:
    void setName(string _name) {
        name = _name;
    }

    string getName() {
        return name;
    }
};

int main() {
    Person per = getPerson();
    cout << per.getName();
}

Person getPerson() {
    Person p;
    p.setName("John");
    return p;
}

Please correct me if I'm wrong.
The person in getPerson() is destroyed after the method. But it returned a COPY of that person and was saved temporarily in the heap. Then "per" in main() method was assigned to it. So the "per" invokes the copy constructor. Am I right?

Yes. Try declaring a constructor that sends a message when its destroyed
to see how it happens.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.