HeapFree触发了一个断点
HeapFree Has triggered a breakpoint
我一直在摆弄malloc和free,我一直有一个问题,当我调用free visual studio说我的程序触发了一个断点。这是我收到的错误:
HEAP: Free HEAP block 5371d0在537230被释放后被修改
下面是我的代码:
#include <malloc.h>
struct STestStruct
{
STestStruct(int _a, int _b, int _c)
: a(_a), b(_b), c(_c)
{
}
int a;
int b;
int c;
};
int main(int argc, char** argv)
{
void* myMem = malloc(sizeof(STestStruct) * 2);
STestStruct* testStruct = (STestStruct*)myMem;
(*testStruct) = STestStruct(1, 2, 3);
// If I comment this and the next line out, everything is fine
STestStruct* testStruct2 = testStruct + sizeof(STestStruct);
(*testStruct2) = STestStruct(1, 2, 3);
free(myMem);
return 0;
}
让我困惑的是,在调用free之后,我没有修改指针中的任何内容。知道是怎么回事吗?
指针在添加n
时,不(不一定)增加n
字节。它们以sizeof(*p) * n
字节递增。
因此,您将testStruct2
增加sizeof(STestStruct) * sizeof(STestStruct)
字节,太多了。只需要添加1
,即"移动到下一个STestStruct
对象的块。
你想
STestStruct* testStruct2 = testStruct + 1;
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 运行同一解决方案的另一个项目的项目
- 为什么断点显示数组的第二个值是一个大数字?额外学分工作
- 触发的断点(析构函数)与类模板类型是自身的一个版本
- GDB:在 C++ 中的类函数上创建一个断点
- Boost Multi_array Boost_Assert抛出了一个断点
- 如何在共享库中的确切行号上设置一个断点
- 删除char阵列指针触发了一个神秘的断点
- Windows触发了一个断点
- visual C++多数组-Windows触发了一个断点
- Windows在myprogram.exe中触发了一个断点
- 代码触发了一个断点,但我根本没有设置断点
- 另一个进程中的断点处理
- 是否有一种方法来测试我们是否在一个断点上,使用纯gdb脚本
- HeapFree触发了一个断点
- 是否有一种方法可以将一个条件设置为gdb中的所有断点,用于调试Cpp代码
- 如何设置一个断点
- 项目触发了一个断点,后跟_BLOCK_TYPE_IS_VALID(pHeap->nBlockUse)