【问题标题】:C++ program's assertion failure due to deleting a pointer [duplicate]由于删除指针而导致C ++程序断言失败[重复]
【发布时间】:2014-07-12 05:37:16
【问题描述】:

我已经创建了一个 int 变量。

然后我将它分配给一个指针。

在那之后,我cout-ed它,然后最终我删除了。

会发生什么?控制台弹出,但随即出现一个错误对话框,显示错误断言失败。谁能解释为什么我收到错误?代码如下:

int main()
{
    int mainNum = 10;
    int *numPt;
    numPt = &mainNum;

    cout << &numPt; 

    delete(numPt);

    cout << endl << endl << endl; // this is to secure last newline char to be outputted.
    cout << "---------------------------" << endl;
    return 0;
}

【问题讨论】:

    标签: c++ memory-management


    【解决方案1】:

    您不能删除堆栈上的内容。您只能删除堆上的东西(由new 创建)。否则各种事情都会发生

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-09
      • 2014-03-14
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多