代码在Windows中运行,但在Linux中不运行!为什么? [简单指针问题]

时间:2011-05-21 10:46:33

标签: c windows linux pointers struct

这是一个运输问题的工作代码片段(删除了实际的功能。这里只有输入和输出功能。顺便说一下,这是不正确的)

# 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 ** array中输入的“费用”是

1 2 3
4 5 6
7 8 9

但是输出就像

1 2 4
4 5 7
7 8 9

我的错误是在创建结构期间。我创建这样的2D数组(非常标准)

    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中没有显示这个明显的错误?

3 个答案:

答案 0 :(得分:1)

可能发生的情况是,不同操作系统上的类型大小不同。可能会发现,在Windows上,sizeof(TRAN)== sizeof(TRAN *)(基于TRAN和sizeof(int)中的元素)而在linux上,显然情况并非如此。

答案 1 :(得分:0)

取决于int的大小与TRAN*的大小。

如果您“幸运”在32位int的64位平台上进行编译,并且在struct TRAN中没有填充,那么sizeof(TRAN*) == sizeof(TRAN)

如果您使用32位int的32位平台。这不再适用了。

答案 2 :(得分:0)

答案已更改

如果您查看代码,则表示您尚未使用alloc组件。现在,当您分配结构时,它需要2n个字节,其中n是整数的大小。您只能访问cost组件。另外,因为您在执行TRAN *时已分配TRAN而不是array[i][j]数组算术*(*(array + sizeof (int *)) + sizeof (TRAN *)),但您希望*(*(array + sizeof (int *)) + sizeof (TRAN))在这种情况下实际访问时cost两个结构中的两个alloc组件实际上是在相邻位置访问的。所以内存访问是完全正确的。因为您只访问唯一的一个组件并在您使用相同数组表示法编写的同一位置读取,因此您可以获得与输入相同的输出。我想如果同时写下costi组件,那么您只会存储值,您存储的是每个j,{{1}}的最新值。