'Error when deleting an object with a pointer
I was curious how to delete an object, but got some problems on the way there.
#include <iostream>
using namespace std;
class test{
int x;
public:
void izpis(){ cout << x; }
test(){ x = 1; }
~test(){ cout << "brisem"; }
};
int main()
{
test a;
test *pa = &a;
pa->izpis();
delete pa;
}
Then I get this error: munmap_chunk(): invalid pointer
Solution 1:[1]
Calls to new and delete must match, calls to new[] and delete[] must match and calls for malloc and free must match. If they are not exactly paired bad things happen.
In your case you are trying to freeing memory in the middle of the stack.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Goswin von Brederlow |
