Pregunta

Consider I have the function:

int &create_ptr(){
    int *x = new int;
    *x =5;
    return *x;
}
int main(){
    int y = create_ptr();
}

Will this cause a memory leak or is there somehow to delete it? Sorry if it's a basic question, this has just been bugging me and I couldn't really find the answer.

Thank you everyone, makes sense now!

¿Fue útil?

Solución

To satisfy your curiosity, yes you can delete it safely, but only if you return it by reference (pointer or C++ reference (&)). By reference so as to retain the original address of your newed object. You need that address for you to correctly and safely delete your object.

int& create_ref(){
    int *x = new int;
    *x =5;
    return *x;
}

int* create_ptr(){
    int *x = new int;
    *x =5;
    return x;
}

int create_sane_version() {
    return 5;
}

int main(){
    int& x = create_ref();
    delete &x;   // OK. Get address of x; same as deleting through a pointer pointing to its address
                 // x here refers to the newed object inside create_ref() (*x)
                 // Still begs the question of why you're painstakingly doing this

    int* y = create_ptr();
    delete y;    // OK, but not optimal (the above isn't either)
                 //    * not optimal == a bad way to do this

    int leak = create_ref();
    delete &leak;    // DEFINITELY NOT OK. leak isn't the same as the newed object
                     //    i.e. &leak != &create_ref();
                     // You're actually *copying* the object referred to by the returned
                     //    reference. The returned reference gets lost.

    int best_way_to_do_this = create_sane_version();
}

Otros consejos

Will this cause a memory leak?

Yes, it will. You are allocating dynamic memory with the new statement with no corresponding delete to free it, ergo: you have a memory leak.


Is there somehow to delete it?

Of course there's: don't do dynamic memory allocation with naked pointers. In your case, why do you even need a reference at all?

int create(){
    return 5;
}

int main(int, char*[]){
    int y = create();
}

If you really need dynamic memory you can use std::shared_ptr and std::make_shared like this:

#include <memory>

auto create_ptr() {
    return std::make_shared<int>(5);
}

int main(int, char*[]) {
    std::shared_ptr<int> y = create_ptr();
}

Yes, this will create a memory leak. You allocate space with new, but never free it using delete.

If you want to create an object (or a primitive type) on the free store, you can return a pointer, and make the function caller for deleting the pointed-to object.

// Caller is responsible for deleting pointed-to int.
int* create_ptr()
{ 
   // Hope that operator new does not throw std::bad_alloc
   int* x = new int;
   *x = 5;
   return x;
 }

A better approach would be to return a smart pointer, but that it outside the scope of this question.

Since you create memory on heap int *x = new int;, You need to explicitly delete it.

You can use shared pointers if you want to avoid track of all heap memory and deleting it explicitly.

Shared pointers keep track of all reference to a memory when last reference goes out of scope, memory pointed is deleted.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top