Not understanding some memory corruption

Here's a struct from a .h file

struct aPartition{
int size;
int startAddr;
int isTaken;
};

Here's my function in a .h file

void setEmUp_2(struct aPartition * PARTITIONS_AA, struct aPartition * PARTITIONS_BB, int * howmany_in)
{
    scanf("%d",&(*howmany_in));
    PARTITIONS_BB = malloc(sizeof(PARTITIONS_BB)*(*howmany_in));
    PARTITIONS_AA = malloc(sizeof(PARTITIONS_AA)*(*howmany_in));
    int startVal = (*howmany_in)*3*2;

    int i;
    for(i=0;i<(*howmany_in);i++)
    {
        printf("\n\nFor iteration %d\n",i);
        PARTITIONS_AA[i].size = startVal;
        startVal--;
        PARTITIONS_AA[i].startAddr = startVal;
        startVal--;
        PARTITIONS_AA[i].isTaken = startVal;
        startVal--;

        PARTITIONS_BB[i].size = startVal;
        startVal--;
        PARTITIONS_BB[i].startAddr = startVal;
        startVal--;
        PARTITIONS_BB[i].isTaken= startVal;
        startVal--;

        printf("A's info: %d  %d  %d\n",PARTITIONS_AA[i].size,PARTITIONS_AA[i].startAddr,PARTITIONS_AA[i].isTaken);
        printf("B's info: %d  %d  %d\n",PARTITIONS_BB[i].size,PARTITIONS_BB[i].startAddr,PARTITIONS_BB[i].isTaken);
    }

    printf("\n\nInfo immediately after setting\n\n");
    for(i=0;i<(*howmany_in);i++)
    {
        printf("\n\nFor iteration %d\n",i);
        printf("A's info: %d  %d  %d\n",PARTITIONS_AA[i].size,PARTITIONS_AA[i].startAddr,PARTITIONS_AA[i].isTaken);
        printf("B's info: %d  %d  %d\n",PARTITIONS_BB[i].size,PARTITIONS_BB[i].startAddr,PARTITIONS_BB[i].isTaken);
    }
}

Here's the .c main

#include "DS.h

int main()
{   
    int howmany_into=0;
    struct aPartition * PARTITIONS_ABB=NULL;
    struct aPartition * PARTITIONS_BBB=NULL;

    setEmUp_2(PARTITIONS_ABB,PARTITIONS_BBB,&howmany);

    return 0;
}

Here's my output:

2

For iteration 0
A's info: 12 11 10
B's info: 9 8 7

For iteration 1
A's info: 6 5 4
B's info: 3 2 1

Info immediately after setting

For iteration 0
A's info: 12 11 10
B's info: 9 8 7

For iteration 1
A's info: 6 1866861066 1953046642
B's info: 3 2 1

What I'm wondering: why is A's appear to change between the set and the print?

Also something worth noting, the output seems to change between mallocing AA first instead of BB first

2

For iteration 0
A's info: 12 11 10
B's info: 9 8 7

For iteration 1
A's info: 6 5 4
B's info: 3 544417601 1868983913

Info immediately after setting

For iteration 0
A's info: 12 11 10
B's info: 9 8 7

For iteration 1
A's info: 6 5 4
B's info: 3 544417601 1868983913

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories