c++ - “.exe”已触发断点
问题描述
我继承了在 32 位中完美运行的代码,但是当我将编译器更改为 64 位时,如果我运行该代码,则会出现错误。如果我单步执行代码,它(几乎总是)会在以后失败。代码中有很多指针,C++ 不是我的强项。是否有人可以了解错误发生的原因?
我需要了解的是:是否可以先创建 BYTE***,然后再创建 BYTE**,并且永远不会分配 BYTE*。我不明白这是如何工作的。我认为首先分配 BYTE*,然后是 BYTE**,然后是 BYTE***。为什么这适用于 x86 而不是 x64?
void* AllocMemory(SIZE_T dwBYTEs)
{
void* result;
if (dwBYTEs > 0)
{
result = malloc(dwBYTEs);
if (result != NULL)
return result;
}
return 0;
}
BYTE** CreateBytes(int arg_0, int arg_4, int arg_8)
{
BYTE** hedi = (BYTE**)AllocMemory(arg_0 * 4);
if (!hedi)return 0;
hedi[0] = (BYTE*)AllocMemory(arg_0 * arg_8 * arg_4);
if (!hedi[0]) {
delete(hedi);
return 0;
}
for (int i = arg_0 - 1; i > 0; i--) {
hedi[arg_0 - 1 - i + 1] = &hedi[0][arg_4 * arg_8 * (arg_0 - 1 - i + 1)];
}
return hedi;
}
BYTE*** CreateItem(int arg_0, int arg_4, int arg_8)
{
BYTE*** hebp = (BYTE***)AllocMemory(arg_8 * 4);
hebp[0] = (BYTE**)CreateBytes(arg_0, arg_4 * arg_8, 1);
if (!hebp[0])
{
delete(hebp);
return 0;
}
for (int i = 1; i < arg_8; i++) {
hebp[i] = (BYTE**)AllocMemory(arg_0 * 4);
if (hebp[i]) {
for (int j = 0; j < arg_0; j++) {//eax
hebp[i][j] = &hebp[0][j][i];
}
}
else {
for (int j = i - 1; j > 0; j--) {//esi
delete(hebp[j - i]);
}
delete(hebp[0]);
delete(hebp);
return 0;
}
}
return hebp;
}
解决方案
这是你的问题:AllocMemory(arg_0 * 4)
在 32 位系统上,指针的大小是 32 位,即四个字节。在 64 位系统上,指针是 64 位,8个字节。使用正确的尺寸(使用sizeof
运算符),它应该可以正常工作。
例如,在CreateBytes
您拥有的功能中
BYTE** hedi = (BYTE**)AllocMemory(arg_0 * 4);
将其替换为
BYTE** hedi = (BYTE**)AllocMemory(arg_0 * sizeof *hedi);
还有许多其他问题,例如代码分配内存malloc
和释放内存delete
。这会导致未定义的行为。
而且我什至不会提及变量的命名或缺少注释...
推荐阅读
- python - 我的代码在超过 15500 次迭代时突然停止编写
- amazon-web-services - 如何将 AWS EC2 负载均衡器添加到与我的静态前端相同的域
- julia - 具有邻接矩阵的 Dijkstra 算法
- php - 我想复制 $friendname=$_POST['friendname']; 当 if 语句有效并在此处回显时,值到下一页
- xamarin - 在 Xamarin Forms 中使用邮件服务时,我们如何获取 SmtpServer NetworkCredentials?
- ruby - 无法安装 vagrant-hostsupdater
- reactjs - 在 cPanel 上部署 React 应用程序后,Web 目录可见
- python - .replace() 不替换元素
- javascript - WebRTC onicecandidate 永远不会在 chrome 中触发
- ios - 单击另一个按钮 swift 2.2 更改声音按钮图像