为什么 C 代码在 Win32 构建中可以工作,但在 x64 构建中却失败?

发布于 2024-11-24 17:39:23 字数 2110 浏览 1 评论 0原文

我使用的是 Windows 7 - 64 位,使用 VS2010。以下代码在 Win32 中构建,没有任何问题,并产生预期结果(两个 8 x 8 矩阵,所有元素的值为 1,第三个 8 x 8 矩阵显示内存地址)。

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main(void) {
    int rows, cols, i, x, y;
    int **array_of_pointers, *arr2d;
    rows = 8;
    cols = 8;
    arr2d = (int *) malloc(rows * cols * sizeof(int));
    for(i = 0; i < rows*cols; i++) {
        *(arr2d + i) = 1;
    }

    for(y = 0; y < cols; y++) {
        printf("\n");
        for(x = 0; x < rows; x++) {
            printf("%d ",*(arr2d + y * cols + x));
        }
    }

    array_of_pointers = (int **) malloc(rows * cols * sizeof(int));
    //I want each element of this array_of_pointers to point to the corresponding     element of arr2d
    for(y = 0; y < cols; y++) {
        for(x = 0; x < rows; x++) {
            *(array_of_pointers + y * cols + x) = (arr2d + y * cols + x);
        }
    }

    //now try printing from pointer array
    printf("\n");
    for(y = 0; y < cols; y++) {
        printf("\n");
        for(x = 0; x < rows; x++) {
            printf("%d ",**(array_of_pointers + y * cols + x));
        }
    }

    //now try printing addresses from pointer array
    printf("\n");
    for(y = 0; y < cols; y++) {
        printf("\n");
        for(x = 0; x < rows; x++) {
            printf("%d ",*(array_of_pointers + y * cols + x));
        }
    }

    free(arr2d);
    free(array_of_pointers);

    _getch();
    return 0;
}

但是,当尝试 x64 构建时,我在输出窗口中看到以下错误消息:

“test.exe”:已加载“C:\My code\test\x64\Debug\test.exe”,已加载符号。

“test.exe”:已加载“C:\Windows\System32\ntdll.dll”,无法找到或打开 PDB 文件

“test.exe”:已加载“C:\Windows\System32\kernel32.dll”,无法找到或打开 PDB 文件

“test.exe”:已加载“C:\Windows\System32\KernelBase.dll”,无法找到或打开 PDB 文件

“test.exe”:已加载“C:\Windows\System32\msvcr100d.dll”,已加载符号。

检测到严重错误 c0000374 Windows 在 test.exe 中触发了断点。

这可能是由于堆损坏造成的,这表明 test.exe 或其加载的任何 DLL 中存在错误。

这也可能是由于用户在 test.exe 获得焦点时按了 F12。

如果我已经为 Win32 正确分配、使用和释放了内存,为什么 x64 会有所不同?

I am on Windows 7 - 64bit, using VS2010. The following code builds in Win32 with no problems and produces the expected outcome (Two matrices of 8 by 8 with all elements having a value of 1 and a third 8 by 8 matrix showing memory addresses).

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main(void) {
    int rows, cols, i, x, y;
    int **array_of_pointers, *arr2d;
    rows = 8;
    cols = 8;
    arr2d = (int *) malloc(rows * cols * sizeof(int));
    for(i = 0; i < rows*cols; i++) {
        *(arr2d + i) = 1;
    }

    for(y = 0; y < cols; y++) {
        printf("\n");
        for(x = 0; x < rows; x++) {
            printf("%d ",*(arr2d + y * cols + x));
        }
    }

    array_of_pointers = (int **) malloc(rows * cols * sizeof(int));
    //I want each element of this array_of_pointers to point to the corresponding     element of arr2d
    for(y = 0; y < cols; y++) {
        for(x = 0; x < rows; x++) {
            *(array_of_pointers + y * cols + x) = (arr2d + y * cols + x);
        }
    }

    //now try printing from pointer array
    printf("\n");
    for(y = 0; y < cols; y++) {
        printf("\n");
        for(x = 0; x < rows; x++) {
            printf("%d ",**(array_of_pointers + y * cols + x));
        }
    }

    //now try printing addresses from pointer array
    printf("\n");
    for(y = 0; y < cols; y++) {
        printf("\n");
        for(x = 0; x < rows; x++) {
            printf("%d ",*(array_of_pointers + y * cols + x));
        }
    }

    free(arr2d);
    free(array_of_pointers);

    _getch();
    return 0;
}

However, when trying an x64 build I am presented with the following error message in the output window:

'test.exe': Loaded 'C:\My code\test\x64\Debug\test.exe', Symbols loaded.

'test.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file

'test.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file

'test.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file

'test.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.

Critical error detected c0000374
Windows has triggered a breakpoint in test.exe.

This may be due to a corruption of the heap, which indicates a bug in test.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while test.exe has focus.

If I have allocated, used and freed memory correctly for Win32 why would it be different for x64?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

久夏青 2024-12-01 17:39:23

您的指针数组的大小错误。它是一个 int* 数组,但您仅为 int 分配空间。 sizeof(int) != sizeof(int*)

Your array of pointers is of the wrong size. Its an array of int* yet you are only allocating space for an int. sizeof(int) != sizeof(int*)

小伙你站住 2024-12-01 17:39:23

您可能没有为array_of_pointers分配足够的空间。您使用的是 sizeof(int) 而不是 sizeof(int *)。在 64 位上,我猜测 int 是 32 位,但指针是 64 位。

此外,在打印 array_of_pointers 的元素时,您应该使用 %p (而不是 %d)作为格式说明符。

You're probably not be allocating enough space for array_of_pointers. You're using sizeof(int) instead of sizeof(int *). On 64-bit, I'm guessing that int is 32 bits but pointers are 64 bits.

Additionally, you should be using %p (instead of%d) as the format specifier when printing out the elements of array_of_pointers.

凉栀 2024-12-01 17:39:23

问题就在这里:

    array_of_pointers = (int **) malloc(rows * cols * sizeof(int));
//I want each element of this array_of_pointers to point to the corresponding     element of arr2d
for(y = 0; y < cols; y++) {
    for(x = 0; x < rows; x++) {
        *(array_of_pointers + y * cols + x) = (arr2d + y * cols + x);
    }
}

你的指针数组的大小就好像指针与 int 的大小相同,而在 Win64 中则不是这样

试试这个:

array_of_pointers = (int **) malloc(rows * cols * sizeof(int*));
//I want each element of this array_of_pointers to point to the corresponding     element of arr2d
for(y = 0; y < cols; y++) {
    for(x = 0; x < rows; x++) {
        *(array_of_pointers + y * cols + x) = (arr2d + y * cols + x);
    }
}

The problem is here:

    array_of_pointers = (int **) malloc(rows * cols * sizeof(int));
//I want each element of this array_of_pointers to point to the corresponding     element of arr2d
for(y = 0; y < cols; y++) {
    for(x = 0; x < rows; x++) {
        *(array_of_pointers + y * cols + x) = (arr2d + y * cols + x);
    }
}

Your array of pointers is sized as though pointers are the same size as ints, which they are not in Win64

Try this:

array_of_pointers = (int **) malloc(rows * cols * sizeof(int*));
//I want each element of this array_of_pointers to point to the corresponding     element of arr2d
for(y = 0; y < cols; y++) {
    for(x = 0; x < rows; x++) {
        *(array_of_pointers + y * cols + x) = (arr2d + y * cols + x);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文