-6

I am experimenting with "new"-"delete" and memory allocation in C++. In the following program :

int* getPtrToFive() {    
    int x =5;
    int *y = new int;
    y = &x;

    return y;
}

int main() {
   int *p = getpoint();
   cout << *p << endl;
   delete p;
}

inside getpoint function, i create memory in heap to make sure that the pointer y which points to the address of the local variable x will return after the execution of the function. Then I want to deallocate the memory of y but I get a error in the main when I delete the pointer p... Could you see what goes wrong here? Thanks in advance.

Garf365
  • 3,619
  • 5
  • 29
  • 41

4 Answers4

3

y = &x; Here you set "y" to point at something else other than the allocated memory. It is not longer pointing at dynamically allocated memory that can be deleted.

In addition, since no pointer now points at the dynamically allocated memory, you also created a memory leak.

Lundin
  • 195,001
  • 40
  • 254
  • 396
0

in your example there's a memory leak: you create y on the heap and without freeing it you assign another address to y (a stack memory of x) so the memory allocated with new has leaked.

the second disaster is: you are returning a local variables(&x). so after the return of your function x will be destructed thus you are facing an undefined behavior.

the third mistake is: calling delete on stack memory which will issue an assertion.

the correct thing to do: make pointer stores only the value of x not the address and return that pointer. in main you can delete the pointer as long as it is on the heap:

int* getPtrToFive() 
{    
    int x = 5;
    int *y = new int(x); // y points to an address on the heap not to x's. we store value of x in pointer

    return y;
}

int main()
{
   int *p = getpoint();
   cout << *p << endl;
   delete p;
}
Raindrop7
  • 3,889
  • 3
  • 16
  • 27
0

With 'new' you dynamically allocate memory on heap and you are responsible to disallocate this memory. You do it with 'delete'. If assign var x to pointer you dont have access to this memory anymore.

Laxynium
  • 3
  • 1
0

The first thing to learn with delete is that it does not delete pointers.

It deletes the object a pointer points to.

Now: what does p points to when delete is called? It points to the local x inside getPtrToFive, because that's what y = &x does, and not anymore to the anonymous it returned by new. Is that what you want (what you really really want)?

No?!? then first learn the different between y = &x and *y = x.

Where is the pointer returned by new?

It was stored in y. Then y (a pointer) received the address of x. The old address has gone. Forgotten. Evaporated. And the int returned by new floats freely the programmer heap space with nothing more able to catch it. You very first memory leak.

What does delete p then? It tries to return to the program heap a piece of memory that does not belong to it: it belongs to the stack. You had been lucky the run-time support caught it.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63