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.