2

私はWindows 7 - 64bitで、VS2010を使用しています。次のコードはWin32で問題なく構築され、期待される結果(8×8行列の2つの行列で、すべての要素が1の値を持ち、3番目の8×8行列がメモリアドレスを示します)を生成します。なぜCコードはWin32ビルドとして動作しますが、x64ビルドは失敗しますか?

'TEST.EXE':ロード「C:私のコード\テスト\ x64の\ x64のビルドをしようとしたときしかし、私は、出力ウィンドウに次のエラーメッセージを提示しています

#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; 
} 

\ Debug \ test.exe '、Symbolsが読み込まれました。

'TEST.EXE':ロード 'C:\ WINDOWS \ SYSTEM32 \ ntdll.dllの'、PDBファイルを検索したり、開くことができません

'TEST.EXE':「ロードC:\ WINDOWS \ System32に\ KERNEL32.DLLは '見つけるか、PDBが

ファイルを開くことができません 'TEST.EXE':ロード 'C:\ WINDOWS \ System32に\ KernelBase.dll' を見つけるか、PDBが

ファイルを開くことができません' のテストを。 exe ':ロードされた' C:\ Windows \ System32 \ msvcr100d.dll '、シンボルがロードされました。

重大エラーが検出されました。c0000374 Windowsがtest.exeでブレークポイントを起動しました。

これは、ヒープが破損している可能性があります。これは、test.exeまたはロードしたDLLにバグがあることを示しています。

これは、test.exeにフォーカスがある間にユーザーがF12を押したことが原因である可能性もあります。

もし私がWin32用にメモリを正しく割り当て、使用して解放したのであれば、それはなぜx64では違うのですか?

答えて

4

おそらく、に十分な領域を割り当てていない可能性があります。 sizeof(int *)の代わりにsizeof(int)を使用しています。 64ビットでは、私はintが32ビットだと推測していますが、ポインタは64ビットです。

さらに、array_of_pointersの要素を出力するときは、書式指定子として%dの代わりに%pを使用する必要があります。

+0

Win64では、intは実際には32ビットですが、ポインタは64ビットです。 –

5

ポインタの配列のサイズが間違っています。その配列はint*ですが、intのスペースのみを割り当てています。 sizeof(int) != sizeof(int*)

3

問題はここにある:

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); 
    } 
} 
を:彼らはWin64の

ではないint型は、これを試してみてくださいとポインタが同じサイズであるかのようにポインタの

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); 
    } 
} 

あなたの配列は大きさで

+0

単なる秒: – antlersoft

関連する問題