在我编写的一个小程序中使用64位整数时,我看到了一些非常奇怪的行为。我的程序执行以下操作......
以下是我的程序代码(numWaysXY_iterative.cc)
#include<stdio.h>
#include<iostream>
using namespace std;
void printMap(int X, int Y, long long int **map)
{
printf("printing map(%d, %d)...\n\n", X, Y);
for(long long int i = 0; i < Y; i++)
{
for(long long int j = 0; j < X; j++)
{
printf("(%lld, %lld) value: %lld\t", j, i, map[j][i]);
}
printf("\n");
}
printf("Finished Printing map\n");
}
long long int numWaysXY_iterative(long long int X, long long int Y)
{
long long int **map = new long long int*[X];
long long int value = 0;
for(long long int i = 0; i < X; i++)
{
map[i] = new long long int(Y);
for(long long int j = 0; j < Y; j++)
{
printf("i: %lld, j: %lld, value: %lld\n", i, j, value);
map[i][j] = value;
value++;
}
}
//debugging - print map
printMap(X,Y, map);
//initialize first row and first column entries to 1
for(long long int i = 0; i < X; i++)
{
map[i][0] = 1;
}
for(long long int j = 0; j < Y; j++)
{
map[0][j] = 1;
}
//debugging - print map
printMap(X,Y, map);
/*
for(int i = 0; i < X, i++)
{
for(int j = 0; j > Y, j++)
{
if
}
}
*/
return map[X-1][Y-1];
}
int main()
{
(void) numWaysXY_iterative(5,3);
}
在编译并运行代码后,我得到了这个输出
$ g++ my_solution/numWaysXY_iterative/numWaysXY_iterative.cc -m64 -o executables/numWaysXY_iterative
$
$
$
$ ./executables/numWaysXY_iterative
i: 0, j: 0, value: 0
i: 0, j: 1, value: 1
i: 0, j: 2, value: 2
i: 1, j: 0, value: 3
i: 1, j: 1, value: 4
i: 1, j: 2, value: 5
i: 2, j: 0, value: 6
i: 2, j: 1, value: 7
i: 2, j: 2, value: 8
i: 3, j: 0, value: 9
i: 3, j: 1, value: 10
i: 3, j: 2, value: 11
i: 4, j: 0, value: 12
i: 4, j: 1, value: 13
i: 4, j: 2, value: 14
printing map(5, 3)...
(0, 0) value: 0 (1, 0) value: 3 (2, 0) value: 6 (3, 0) value: 9 (4, 0) value: 12
(0, 1) value: 1 (1, 1) value: 4 (2, 1) value: 7 (3, 1) value: 10 (4, 1) value: 13
(0, 2) value: 3 (1, 2) value: 6 (2, 2) value: 9 (3, 2) value: 12 (4, 2) value: 14
Finished Printing map
printing map(5, 3)...
(0, 0) value: 1 (1, 0) value: 1 (2, 0) value: 1 (3, 0) value: 1 (4, 0) value: 1
(0, 1) value: 1 (1, 1) value: 4 (2, 1) value: 7 (3, 1) value: 10 (4, 1) value: 13
(0, 2) value: 1 (1, 2) value: 1 (2, 2) value: 1 (3, 2) value: 1 (4, 2) value: 14
Finished Printing map
Problems:
1. After initializing the values of the array in lines 26 - 35, even though the value
variable is strictly increasing, and each array indice is supposedly getting a unique
value assigned to it, this is not being reflected in the array. You can see that
map[0][2] == map[1][0] for example.
2. I'm setting the first row and first column array elements to have the value 1 in lines
40 - 48, but again this is not reflected when the array is printed out. Instead it
seems that the first row, first column, and all elements in the last row besides the
last are being set to 1.
在多次查看我的逻辑并且没有看到任何错误之后,我决定制作另一个版本的程序,只需用int替换“long long int”变量。
此代码粘贴在下面(debug_numWaysXY_iterative.cc)
#include<stdio.h>
#include<iostream>
using namespace std;
void printMap(int X, int Y, int **map)
{
printf("printing map(%d, %d)...\n\n", X, Y);
for(int i = 0; i < Y; i++)
{
for(int j = 0; j < X; j++)
{
printf("(%d, %d) value: %d\t", j, i, map[j][i]);
}
printf("\n");
}
printf("Finished Printing map\n");
}
int numWaysXY_iterative(int X, int Y)
{
int **map = new int*[X];
int value = 0;
for(int i = 0; i < X; i++)
{
map[i] = new int(Y);
for(int j = 0; j < Y; j++)
{
printf("i: %d, j: %d, value: %d\n", i, j, value);
map[i][j] = value;
value++;
}
}
//debugging - print map
printMap(X,Y, map);
//initialize first row and first column entries to 1
for(int i = 0; i < X; i++)
{
map[i][0] = 1;
}
for(int j = 0; j < Y; j++)
{
map[0][j] = 1;
}
//debugging - print map
printMap(X,Y, map);
/*
for(int i = 0; i < X, i++)
{
for(int j = 0; j > Y, j++)
{
if
}
}
*/
return map[X-1][Y-1];
}
int main()
{
(void) numWaysXY_iterative(5,3);
}
只是为了证明我在这两个程序之间唯一的区别是用“int”替换“long long int”变量(并相应地更改printf调用),这里是差异 两个源文件之间
$ diff my_solution/numWaysXY_iterative/numWaysXY_iterative.cc my_solution/numWaysXY_iterative/debug_numWaysXY_iterative.cc
6c6
< void printMap(int X, int Y, long long int **map)
---
> void printMap(int X, int Y, int **map)
9c9
< for(long long int i = 0; i < Y; i++)
---
> for(int i = 0; i < Y; i++)
11c11
< for(long long int j = 0; j < X; j++)
---
> for(int j = 0; j < X; j++)
13c13
< printf("(%lld, %lld) value: %lld\t", j, i, map[j][i]);
---
> printf("(%d, %d) value: %d\t", j, i, map[j][i]);
20c20
< long long int numWaysXY_iterative(long long int X, long long int Y)
---
> int numWaysXY_iterative(int X, int Y)
22c22
< long long int **map = new long long int*[X];
---
> int **map = new int*[X];
24c24
< long long int value = 0;
---
> int value = 0;
26c26
< for(long long int i = 0; i < X; i++)
---
> for(int i = 0; i < X; i++)
28,29c28,29
< map[i] = new long long int(Y);
< for(long long int j = 0; j < Y; j++)
---
> map[i] = new int(Y);
> for(int j = 0; j < Y; j++)
31c31
< printf("i: %lld, j: %lld, value: %lld\n", i, j, value);
---
> printf("i: %d, j: %d, value: %d\n", i, j, value);
41c41
< for(long long int i = 0; i < X; i++)
---
> for(int i = 0; i < X; i++)
45c45
< for(long long int j = 0; j < Y; j++)
---
> for(int j = 0; j < Y; j++)
$
然而,当这个版本的代码运行时,我得到了我期望的输出!!
$ g++ my_solution/numWaysXY_iterative/debug_numWaysXY_iterative.cc -m64 -o executables/debug_numWaysXY_iterative
$
$
$
$ ./executables/debug_numWaysXY_iterative
i: 0, j: 0, value: 0
i: 0, j: 1, value: 1
i: 0, j: 2, value: 2
i: 1, j: 0, value: 3
i: 1, j: 1, value: 4
i: 1, j: 2, value: 5
i: 2, j: 0, value: 6
i: 2, j: 1, value: 7
i: 2, j: 2, value: 8
i: 3, j: 0, value: 9
i: 3, j: 1, value: 10
i: 3, j: 2, value: 11
i: 4, j: 0, value: 12
i: 4, j: 1, value: 13
i: 4, j: 2, value: 14
printing map(5, 3)...
(0, 0) value: 0 (1, 0) value: 3 (2, 0) value: 6 (3, 0) value: 9 (4, 0) value: 12
(0, 1) value: 1 (1, 1) value: 4 (2, 1) value: 7 (3, 1) value: 10 (4, 1) value: 13
(0, 2) value: 2 (1, 2) value: 5 (2, 2) value: 8 (3, 2) value: 11 (4, 2) value: 14
Finished Printing map
printing map(5, 3)...
(0, 0) value: 1 (1, 0) value: 1 (2, 0) value: 1 (3, 0) value: 1 (4, 0) value: 1
(0, 1) value: 1 (1, 1) value: 4 (2, 1) value: 7 (3, 1) value: 10 (4, 1) value: 13
(0, 2) value: 1 (1, 2) value: 5 (2, 2) value: 8 (3, 2) value: 11 (4, 2) value: 14
Finished Printing map
$
正如我所示,我用-m64标志编译了两个程序,我的笔记本电脑中有一个i7处理器, 这是一个64位处理器。我也试过我的程序版本,所有“long long int”实例都替换为“long int”,而另一个版本我用“int64_t”替换了所有“long long int”,并且输出结果不正确对于这两个版本也是如此。
任何人都知道我为什么会在这里看到不同的行为?