gpt4 book ai didi

c++ - 无法删除指针,已触发断点

转载 作者:行者123 更新时间:2023-11-28 01:27:43 37 4
gpt4 key购买 nike

我在在线类(class)中学习了 C++,但在 Visual Studio 中遇到“删除”语句的问题。执行 delete 时,它​​会打开一个文件 delete_scalar.cpp 并显示:

ConsoleApplication.exe has triggered a breakpoint. exception thrown

我已经将 SymbolCache 文件夹从 temp 中移出,包括 wntdll.pdb 等。

#include "pch.h"
#include <iostream>
using namespace std;

int main()
{
int *pInt = new int;
*pInt = 3;
cout << *pInt << endl;
cout << pInt << endl;

delete[] pInt; //error -- UPDATED: still get error with `delete pInt;`

return 0;
}

这里是输出信息,

'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Symbols loaded. 'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Symbols loaded. 'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Symbols loaded. 'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. Symbols loaded. 'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Symbols loaded. 'ConsoleApplication11.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Symbols loaded. The thread 0x2fd0 has exited with code 0 (0x0). HEAP[ConsoleApplication11.exe]: Invalid address specified to RtlValidateHeap( 00930000, 009359E8 ) ConsoleApplication11.exe has triggered a breakpoint.

从视觉本身删除_scalar.cpp,

//
// delete_scalar.cpp
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Defines the scalar operator delete.
//
#include <crtdbg.h>
#include <malloc.h>
#include <vcruntime_new.h>
#include <vcstartup_internal.h>

_CRT_SECURITYCRITICAL_ATTRIBUTE
void __CRTDECL operator delete(void* const block) noexcept
{
#ifdef _DEBUG
_free_dbg(block, _UNKNOWN_BLOCK); // the circle X symbol showed up there
#else
free(block);
#endif
}

我已经做了断点的事情,比如启用和删除,但断点仍然在 delete_scalar.cpp 上触发,并显示圆圈 X 符号。

最佳答案

如果你用new请求内存,你需要用delete删除它。

只有在使用 new [ ] 时才使用 delete [ ]。两者不匹配会导致未定义的行为。

关于c++ - 无法删除指针,已触发断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53057590/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com