0

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;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ashok Khandhar
  • 55
  • 1
  • 2
  • 6
  • 1
    `delete` what you `new`, and `delete[]` what you `new[]`. If you haven't used `new` then you shouldn't do `delete`. – Some programmer dude Jan 26 '22 at 07:22
  • 4
    No, but returning a pointer to local variable can only lead to undefined behavior. – selbie Jan 26 '22 at 07:22
  • 1
    Compile with warnings, and the compiler will tell you something about above code. – Ulrich Eckhardt Jan 26 '22 at 07:22
  • 4
    On another note, remember that the variable `a` is a *local* variable inside the `create` function. The life-time of `a` will end as the function ends. Any pointers to it will become invalid as soon as the function ends. Attempting to dereference the pointer will lead to *undefined behavior*. – Some programmer dude Jan 26 '22 at 07:23
  • 1
    No, you must not 'delete' the pointer 'ptr', because you have not 'new' it from the heap. – Zongru Zhan Jan 26 '22 at 07:28
  • While we usually speak about "deleting pointers", that's not what we actually do - we delete the objects that they point to. So what you need to think about is whether the object that your pointer points to was created with `new` or not. – molbdnilo Jan 26 '22 at 08:36

4 Answers4

2

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.

Karol T.
  • 543
  • 2
  • 13
1

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.

Shafa95
  • 201
  • 2
  • 14
0

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.

Lukas-T
  • 11,133
  • 3
  • 20
  • 30
0

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326