Difference between “new Foo()” and “&Foo()” as parameters

  • A+
Category:Languages

I have some questions concerning the difference between the keyword "new" and "&" in a certain context.
Let’s say this is my Code:

class Base {…}; class Foo : public Base {…}; class Storage {     void save(Base * object);     Base * content; }; int main(…) {     Storage s1, s2;     s1.save(new Foo())     s2.save(&Foo()) } 

After the execution of main, s1 will hold a pointer to an object of type Foo. Yet s2 will hold a pointer to an object of type Base.
s2.content will only point towards an object of type Foo until the save method has finished execution.

Please correct me if I am wrong:

As far as I understand "new Foo()" creates a pointer to a new object of type Foo. "&Foo()" on the other hand first creates a new object of type Foo and then points to it.

What exactly is the difference between "new Foo()" and "&Foo()" then? Obviously both give you a pointer to an existing object of type Foo.

Why does the object created by "new Foo()" persist after execution of the save method whereas the object created via "&Foo()" does not?

May it be that "&Foo()" creates a temporary object, which will cease to exist after the execution of save? If yes, how can I prolong the life of the object created via "&Foo()" to make it live (at least) until the destruction of s2?

Edit 1: Thank you very much for the quick answers! I'm simply using Visual Studio, so maybe "&Foo()" compiling is some Microsoft specific stuff...

 


What exactly is the difference between "new Foo()" and "&Foo()" then? Obviously both give you a pointer to an existing object of type Foo.

Why does the object created by "new Foo()" persist after execution of the save method whereas the object created via "&Foo()" does not?


new Foo()

new Foo(); 

This creates a dynamically-allocated Foo object and returns a pointer pointing to that object. Dynamically-allocated objects will persist until they're explicitly deleted by the programmer:

Foo* foo = new Foo(); delete foo; // delete the object. 

&Foo()

Foo(); 

This creates a Foo object using automatic-storage. This means its lifetime, when the object is deleted, is decided by the scope that the object lives in:

{   Foo foo{}; // foo lives in automatic storage. } // end of scope, foo dies 

In your case you are creating a new Foo object and you're passing the address of this anonymous object to Storage::save. This object will be destroyed at the end of the full expression. This basically means after s2.save() returns your object will be destroyed and the pointer pointing to it in s2 will be dangling and dereferencing it will be undefined behaviour.


If yes, how can I prolong the life of the object created via "&Foo()" to make it live (at least) until the destruction of s2?

You can't. You probably want a smart pointer here such as std::unique_ptr.


Note that taking the address of a temporary is non-standard and thus this code is non-compliant to begin with. Your compiler is probably using an extension to allow it. MSVC is known for allowing this.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: