简体   繁体   中英

If we return address of local variable of function using pointer, do we need to delete the pointer after using that function or not?

If I don't use delete ptr at comment here in below code, then it causes a memory leak?

int *create(){
    int a;
    int *a_ptr{&a};
    return a_ptr;
}
int main(){
    int *ptr = {nullptr};
    ptr = create();
    //here
    return 0;
}

Actually you return pointer to object that is destroyed after create function ends its execution. int a; is created on stack so a_ptr points to some place on stack. During return all objects on stack are destroyed and there is only a_ptr left there as value.

No, you don't have memory leak but ptr in main() function is invalid as points to non-existing object.

In main() scope you initialized a pointer to nullptr without allocating any space (You have not used the new keyword).

In create() function you declared an int without initializing it (it could have any value), then you declared a a_ptr pointing to the reference of a . When the code exits from the create() function scope, the variable a is out of scope and those memory cells will be marked as unused . So, when the returned pointer it's assigned to the ptr in main() scope it will points to unused memory and this will lead to undefined behaviour whenever you use this pointer.

Essentialy, the pointer is already pointing to nullptr and you don't have to delete it, because you haven't allocated any space for the pointer and you have nothing to delete.

No, you must only delete pointers that were created by new . Never delete pointers to local variables. However, your code has undefined behaviour and that should be your primary concern.

When create() returns, the lifetime of it's local variable a will end and any pointers/references to it will become invalid. Accessing such an invalid pointer causes undefined behaviour which often manifests as segmentation fault. Never return pointers or references to local variables from a function.


One option is to actually new the pointer you return:

int *create(){
    return new int{0};
}

int main(){
    int *ptr = create();

    delete ptr;
}

I wouldn't recommend this however. In modern C++ you can and should avoid new / delete / new[] / delete[] and prefer smart pointers or vectors as appropriate.

If I don't use delete ptr at comment here in below code, then it causes a memory leak?

No. Variables with automatic storage duration are destroyed automatically when they go out of scope. There is no memory leak in the example.

However if you do delete the ptr then the behaviour of the program will be undefined. That's because you may delete only what you've allocated with new .

Even if you only indirect through the pointer, will the program be undefined. The returned pointer is always invalid and nothing useful can be done with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM