#include <iostream>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode (int x): val(x), next(NULL) {}
};
int main(int argc, char const *argv[])
{
// ListNode n1(2);
// ListNode dummy(0);
// dummy.next = &n1;
// ListNode *p = &dummy;
// delete p;
int *p = new int;
int t = 10;
p = &t;
delete p;
return 0;
}
如上所示,剛接觸內存分配,百度了好些東西了,不明白為什麼連這個最基本的分配和釋放都會報錯pointer being freed was not allocated,真心求助
因為你改變了指針,你試圖分配堆棧上的t,當然出錯
int *p = new int;
int t = 10;
int *p1=p;
p = &t;
delete p1;