开发者

Code is working in Windows but not in Linux! Why? [Simple pointer problem]

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?


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜