项目名称.exe 已触发断点
ProjectName.exe has triggered a breakpoint
当运行程序时,它会正常运行,但总是抛出此错误。 它说错误来自以下行:
int* temp = new int[length];
我不知道为什么会这样。 程序按升序排序返回数组,但随后抛出断点。
void mergeSort(int *a, int low, int high)
{
if (low == high)
return;
int q = (low + high) / 2;
mergeSort(a, low, q);
mergeSort(a, q + 1, high);
merge(a, low, q, high);
}
void merge(int *a, int low, int q, int high)
{
const int length = high - low + 1;
int* temp = new int[length];
int i = low;
int k = low;
int j = q + 1;
while (i <= q && j <= high)
{
if (a[i] <= a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while (i <= q)
temp[k++] = a[i++];
while (j <= high)
temp[k++] = a[j++];
for (i = low; i <= high; i++)
a[i] = temp[i];
}
我认为这是temp
的内存访问违规
int k = low;
在void merge
k
变量是数组索引temp
。如果调用mergeSort(a, q + 1, high)
则merge low
参数q + 1
并且k
超出范围 0 ~ 长度。
如果k
超出范围 0 ~ 长度。temp[k]
发生访问冲突。
我还建议在merge
函数中添加delete[] temp
。
这是我的代码
int _a[] = { 5, 1, 3, 4, 2 }; // Test array!
void merge(int *a, int low, int q, int high)
{
const int length = high - low + 1;
int* temp = new int[length];
int i = low;
int k = 0; // I fixed it(low -> 0)
int j = q + 1;
while (i <= q && j <= high)
{
if (a[i] <= a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while (i <= q)
temp[k++] = a[i++];
while (j <= high)
temp[k++] = a[j++];
for (i = low; i <= high; i++)
a[i] = temp[i];
delete[] temp; // Add Delete
}
int main()
{
mergeSort(_a, 0, 5);
return 0;
}
相关文章:
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 终端不会为C++文件创建.exe文件吗
- 在Windows中以.exe的形式运行c++
- 无法删除指针,已触发断点
- Visual Studio mkl_link_tool.exe链接错误
- 条件断点在不应该触发时触发
- PrintWindow函数在notepad.exe中出错
- VSCode C++ 编译的exe感染了Win32:TrojanX-gen[Trj]
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- 项目名称.exe 已触发断点
- ".exe"触发了断点
- Delete[] 运算符使 *.exe 触发断点和 wntdll.pdb未加载以显示
- 测试项目.exe触发了断点
- _snprintf_s instokes [peceraiton] .exe触发了断点
- C++控制台应用程序 1.exe 已触发断点
- 项目.exe在系统("pause")之后触发了断点
- MyTest1.exe 在 C++ / MFC 程序中触发了删除命令的断点
- 从双重和错误中丢失数据:Windows 在 exe 中触发了断点.这可能是由于堆损坏
- OpenCV-FindContours-Projekt.exe已触发断点
- Windows在myprogram.exe中触发了一个断点