代码可以在 Windows 中运行,但不能在 Linux 中运行!为什么? 【简单的指针问题】
这是一个运输问题的工作代码片段(删除了实际功能。这里只有输入和输出功能。顺便说一句,这是不正确的)
# include <stdio.h>
# include <stdlib.h>
typedef struct transport
{
int cost;
int alloc;
}TRAN;
void problem_input (TRAN **, int *, int *, int, int);
void problem_display (TRAN **, int *, int *, int, int);
int main()
{
int n_dest;
int n_org;
int i;
int j;
printf("\n\n\tEnter Number Of Destinations : ");
scanf("%d", &n_dest);
printf("\n\n\tEnter Number Of Origins(Sub-stations) : ");
scanf("%d", &n_org);
TRAN ** array = (TRAN **)calloc(n_org, sizeof(TRAN *));
int * dest = (int *)calloc(n_dest, sizeof(int));
int * origins = (int *)calloc(n_org, sizeof(int));
for(i = 0; i < n_org; i++)
{
array[i] = (TRAN *)calloc(n_dest, sizeof(TRAN *));
}
problem_input (array, dest, origins, n_dest, n_org);
problem_display (array, dest, origins, n_dest, n_org);
printf("\n\n");
return 0;
}
void problem_input (TRAN ** array, int * dest, int * origins, int n_dest, int n_org)
{
int i;
int j;
printf("\n\n\tEnter The Amount Of Supplies Required At The Destinations : ");
for(i = 0; i < n_dest; i++)
{
printf("\n\n\t\tDestination %d : ", (i+1));
scanf("%d", &dest[i]);
}
printf("\n\n\tEnter The Amount Of Supplies Available At The Origins : ");
for(i = 0; i < n_org; i++)
{
printf("\n\n\t\tOrigin %d : ", (i+1));
scanf("%d", &origins[i]);
}
printf("\n\n\tEnter The Cost Matrix : ");
for(i = 0; i < n_org; i++)
{
printf("\n\n\t\tOrigin %d", (i+1));
for(j = 0; j < n_dest; j++)
{
printf("\n\n\t\t\tDestination %d : ", (j+1));
scanf("%d", &array[i][j].cost);
}
}
}
void problem_display (TRAN ** array, int * dest, int * origins, int n_dest, int n_org)
{
int i;
int j;
printf("\n\n\tThe Given Transportation Problem : ");
for(i = 0; i < n_org; i++)
{
printf("\n\n\t");
for(j = 0; j < n_dest; j++)
{
printf("\t%d", array[i][j].cost);
}
printf("\t[%d]", origins[i]);
}
printf("\n\n\t");
for(i = 0; i < n_dest; i++)
{
printf("\t[%d]", dest[i]);
}
}
这在 Windows 中工作正常,但在 Linux 中显示不正确的输出。 (我在家使用 Windows,但在大学使用 Linux。想象一下,当我在教授面前得到错误的输出时,我的感受如何。但她却一无所知。)
例如,我在 TRAN 中输入“cost” ** array 是
1 2 3
4 5 6
7 8 9
,但输出就像
1 2 4
4 5 7
7 8 9
我的错误是在创建结构期间发生的。我创建这样的二维数组(非常标准)
TRAN ** array = (TRAN **)calloc(n_org, sizeof(TRAN *));
for(i = 0; i < n_org; i++)
{
array[i] = (TRAN *)calloc(n_dest, sizeof(TRAN));
}
但是错误地,我在 for 循环中这样做了
for(i = 0; i < n_org; i++)
{
array[i] = (TRAN *)calloc(n_dest, sizeof(TRAN *));
}
,即 sizeof(TRAN *)
而不是 sizeof(TRAN)
所以我的问题是,为什么这个明显的错误没有在 Windows 中显示出来?
This is a working code snippet of a transportation problem (Removed the actual function. Only input and output functions are here. And BTW, it's incorrect)
# include <stdio.h>
# include <stdlib.h>
typedef struct transport
{
int cost;
int alloc;
}TRAN;
void problem_input (TRAN **, int *, int *, int, int);
void problem_display (TRAN **, int *, int *, int, int);
int main()
{
int n_dest;
int n_org;
int i;
int j;
printf("\n\n\tEnter Number Of Destinations : ");
scanf("%d", &n_dest);
printf("\n\n\tEnter Number Of Origins(Sub-stations) : ");
scanf("%d", &n_org);
TRAN ** array = (TRAN **)calloc(n_org, sizeof(TRAN *));
int * dest = (int *)calloc(n_dest, sizeof(int));
int * origins = (int *)calloc(n_org, sizeof(int));
for(i = 0; i < n_org; i++)
{
array[i] = (TRAN *)calloc(n_dest, sizeof(TRAN *));
}
problem_input (array, dest, origins, n_dest, n_org);
problem_display (array, dest, origins, n_dest, n_org);
printf("\n\n");
return 0;
}
void problem_input (TRAN ** array, int * dest, int * origins, int n_dest, int n_org)
{
int i;
int j;
printf("\n\n\tEnter The Amount Of Supplies Required At The Destinations : ");
for(i = 0; i < n_dest; i++)
{
printf("\n\n\t\tDestination %d : ", (i+1));
scanf("%d", &dest[i]);
}
printf("\n\n\tEnter The Amount Of Supplies Available At The Origins : ");
for(i = 0; i < n_org; i++)
{
printf("\n\n\t\tOrigin %d : ", (i+1));
scanf("%d", &origins[i]);
}
printf("\n\n\tEnter The Cost Matrix : ");
for(i = 0; i < n_org; i++)
{
printf("\n\n\t\tOrigin %d", (i+1));
for(j = 0; j < n_dest; j++)
{
printf("\n\n\t\t\tDestination %d : ", (j+1));
scanf("%d", &array[i][j].cost);
}
}
}
void problem_display (TRAN ** array, int * dest, int * origins, int n_dest, int n_org)
{
int i;
int j;
printf("\n\n\tThe Given Transportation Problem : ");
for(i = 0; i < n_org; i++)
{
printf("\n\n\t");
for(j = 0; j < n_dest; j++)
{
printf("\t%d", array[i][j].cost);
}
printf("\t[%d]", origins[i]);
}
printf("\n\n\t");
for(i = 0; i < n_dest; i++)
{
printf("\t[%d]", dest[i]);
}
}
This much was working fine in Windows but displayed incorrect output in Linux. (I use Windows at home but Linux at college. Imagine how I felt when I'm getting a wrong output in front of my professor. But she was none the wiser.)
For example my input for 'cost' in TRAN ** array
was
1 2 3
4 5 6
7 8 9
but the output was coming like
1 2 4
4 5 7
7 8 9
My error was during creation of the structure. I create 2D arrays like this (very standard)
TRAN ** array = (TRAN **)calloc(n_org, sizeof(TRAN *));
for(i = 0; i < n_org; i++)
{
array[i] = (TRAN *)calloc(n_dest, sizeof(TRAN));
}
But by mistake, I did this in the for loop
for(i = 0; i < n_org; i++)
{
array[i] = (TRAN *)calloc(n_dest, sizeof(TRAN *));
}
That is sizeof(TRAN *)
instead of sizeof(TRAN)
So my question is, why didn't this glaring mistake show in Windows?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
可能发生的情况是,不同操作系统上的类型具有不同的大小。事实可能是,在 Windows 上,sizeof(TRAN) == sizeof(TRAN*) (基于 TRAN 和 sizeof(int) 中的元素),而在 Linux 上,情况显然并非如此。
What's likely happening is that types are of different sizes on different operating systems. It might turn out that on Windows, sizeof(TRAN) == sizeof(TRAN*) (based on the elements inside TRAN and sizeof(int)) whereas on linux, this obviously isn't the case.
取决于
int
的大小与TRAN*
的大小。如果您“幸运”在具有 32 位
int
的 64 位平台上进行编译,并且在struct TRAN
中没有填充,则sizeof(TRAN *) == sizeof(TRAN)
。如果您使用的是具有 32 位
int
的 32 位平台。那不再成立了。Depends on the size of
int
versus the size of aTRAN*
.If you're "lucky" to compile on a 64bit platform with 32bit
int
s, and that doesn't have padding instruct TRAN
, thensizeof(TRAN*) == sizeof(TRAN)
.If you're on a 32bit platform with 32bit
int
s. That doesn't hold anymore.答案已更改
如果您查看代码,则表明您没有使用
alloc
组件。现在,当您分配结构时,它需要2n
字节,其中n
是整数的大小。您只能访问cost
组件。另外,因为在执行array[i][j]
时分配了TRAN *
而不是TRAN
,所以数组算术会*( *(array + sizeof (int *)) + sizeof (TRAN *))
但你想要*(*(array + sizeof (int *)) + sizeof (TRAN))
当您访问这两个时实际上是哪种情况它们在相邻位置实际访问的两个结构中的cost
组件。所以内存访问是完全正确的。因为您只访问唯一的一个组件,并在使用相同数组表示法写入的同一位置进行读取,因此您将获得与输入相同的输出。我想如果同时编写alloc
和cost
组件,那么您将只存储为每个i
存储最新值的值,<代码>j。ANSWER CHANGED
if you look at your code you have not used the
alloc
component. Now when you allocate the structure it takes2n
bytes wheren
is the size of integer. You only access thecost
component. Also because you have allocated theTRAN *
instead ofTRAN
when doingarray[i][j]
the array arithmatic does*(*(array + sizeof (int *)) + sizeof (TRAN *))
but you wanted*(*(array + sizeof (int *)) + sizeof (TRAN))
in which case actually when you access the twocost
components in the two structures they are actually accessed in adjacent locations. So the memory access is perfectly right. because you only access the only one component and read at the same location where you have written with the same array notation so you get the same output as you have input . I guess if write both thealloc
andcost
components then you would have only the value get stored which you stored the latest for eachi
,j
.