using new and delete causing program to crash?

Why is this code causing my program to crash? Also, if anyone could clarify how do I use new keyword with functions that return pointers? I am trying to understand this since a pointer is supposed to point at the memory address but how could it point at something that no longer exists due to scope.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include <iostream>
  #include "Character.h"

using namespace std;
double * test();
int main()
{
    double * ptr = new double;
    ptr = test();
    cout << ptr << endl ;
    cout << *ptr << endl ;
    delete ptr ; //why does my program crash when I insert this line?

    return 0;
}
double * test()
{
    double salary = 56.88;
    return &salary;
}


Another quick question that is relevant to this, how could I return a pointer after using the new keyword in a function? Does that mean I would have to use delete on the return value ? Since functions do not read past the return keyword.

Basically what I'm asking is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
double * test()
{
double * ptr = new double;
*ptr = 450;
return ptr; //after this where could I delete this??
delete ptr; // can't do it here right cause it won't even read it.
}

int main()
{
test();
delete ptr; // but if I do it here how will main know what ptr is?
}
Last edited on
For the first part:
Your test() function is trying to return the address of a local variable. This is essentially junk after the function is over, and you should not do this.

When you first allocated ptr to hold the double, this allocates memory. You then reassign ptr to point to a static object (not dynamically allocated). As soon as you do this, the link to your dynamically allocated memory is lost, AKA a memory leak.

When you now call delete ptr, ptr is currently pointing to some statically-allocated memory, and you can't delete that. What you were trying to delete was already lost on line 9.


Second question part:

Perhaps you should read into what the scope of a variable is. the variable "ptr" only exists in your test function. That being said, the memory it points to is dynamically allocated and therefore still exists after the test() function ends.

In your main function, when you call test(), you do nothing with its return value. This is another memory leak. Instead, you'd want to assign it to something (it's returning a pointer to a double, right?).

To properly return and delete something, you'd do

1
2
3
4
5
6
7
8
9
10
11
double* test()
{
    double * ptr = new double(42);
    return ptr;
}

int main()
{
    double * dptr = test();
    delete dptr; // no memory leak now!
}


_______________________________
The downside to this is that now the user of the the test function has to know what's inside it, and has to remember to manually, explicitly delete it. Not sure how much C++ you know, but one of the amazing things about classes are destructors, where written calls to delete will be done automatically once an object goes out of scope.
Last edited on
the variable "ptr" only exists in your test function. That being said, the memory it points to is dynamically allocated and therefore still exists after the test() function ends.
OMG thank you, this sentence just made it all click in my head.


however, for the first part:
after I messed around with it a bit I decided to do delete &ptr; and it worked and the program stopped crashing, does that mean that there was no longer any memory leak or was the leak still there?

Thank you, the second part makes 100% sense, I had that little question in my mind for a while now and It had not been properly addressed until now.
You're welcome. &ptr is the address of the pointer variable itself (the address of the pointer, which is also an address, if that makes any sense), I can't think of a reason when this would be done, off the top of my head.

So no, there is still a memory leak. If I understand correctly, ptr is at this point still pointing to a statically allocated memory block, and the pointer itself is statically allocated, so delete &ptr would still be trying to delete something that shouldn't be deleted.

When I compiled the program to delete &ptr, it still crashed for me, so it is in fact undefined behavior.
Last edited on
Okay my friend, thank you so much :)
Topic archived. No new replies allowed.