dimanche 26 juin 2016

Does default-initializing a non-const reference function parameter from a dynamically allocated dereferenced pointer create a memory leak?

I'm in front of this and must write valid c++0x code :

type1 myfunc( type2& var = /*some value of type "type2"*/ )
{
   // Some code
}

I tried this :

type1 myfunc( type2& var = *(new type2) )
{
   // Some code
}

And of course it works, but I'm not sure wether this creates or not a memory leak. What does this code exacty do in the computer memory ? If I can't do this, do I have any other solutions than to overload my function?

Aucun commentaire:

Enregistrer un commentaire