代码可以在 Windows 中运行,但不能在 Linux 中运行!为什么? 【简单的指针问题】

发布于 2024-11-09 00:43:57 字数 3135 浏览 0 评论 0原文

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

# 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 技术交流群。

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

发布评论

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

评论(3

殊姿 2024-11-16 00:43:57

可能发生的情况是,不同操作系统上的类型具有不同的大小。事实可能是,在 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.

爱你不解释 2024-11-16 00:43:57

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

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

如果您使用的是具有 32 位 int 的 32 位平台。那不再成立了。

Depends on the size of int versus the size of a TRAN*.

If you're "lucky" to compile on a 64bit platform with 32bit ints, and that doesn't have padding in struct TRAN, then sizeof(TRAN*) == sizeof(TRAN).

If you're on a 32bit platform with 32bit ints. That doesn't hold anymore.

蘑菇王子 2024-11-16 00:43:57

答案已更改

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

ANSWER CHANGED

if you look at your code you have not used the alloc component. Now when you allocate the structure it takes 2n bytes where n is the size of integer. You only access the cost component. Also because you have allocated the TRAN * instead of TRAN when doing array[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 two cost 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 the alloc and cost components then you would have only the value get stored which you stored the latest for each i, j.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文