user2252004
user2252004

Reputation: 49

Pointers to array affecting wrong array

I am having an issue where editing array A is affecting array B in C when using pointers. My code is the following:

#include <stdio.h>
#include <stdlib.h>
#include "frac_heap.h"

#define ARRAYSIZE 10

fraction heap[][ARRAYSIZE] = {0};
block freeBlocks[][ARRAYSIZE] = {0};
int startingBlock = 0;

void init_Heap(){
    int x;
    for(x = 0; x < ARRAYSIZE; x ++){    

        block *currBlock = freeBlocks[x];
        currBlock->isFree = 1;  
    }
}



void dump_heap(){
    int x;
    for(x = 0; x < ARRAYSIZE; x ++){
        fraction* tempFrac = heap[x];
        printf("%d\t%d\t%d\n",tempFrac->sign, tempFrac->numerator, tempFrac->denominator);
    }   
}


fraction* new_frac(){
    fraction* testFraction = heap[0];
    return testFraction;
}




int main(){ 
    init_Heap();
    dump_heap();
    fraction *p1;
    p1 = new_frac();
    p1->sign = -1;
    p1->numerator  = 2; 
    p1->denominator = 3;
    dump_heap();    
   }

dump_heap() just prints out the contents of heap along with the fractions sign, numerator, and denominator. However, the output when I run this code is the following:

0   0   0
0   1   0
0   1   0
0   1   0
0   1   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0

-1  2   3
0   1   0
0   1   0
0   1   0
0   1   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0

See the 1s in the numerator place in numerous fractions in the fractions array even though I never told it to put 1s there? This doesnt happen if I edit out the call to init_heap(). If I edit out the call to init_heap the output is:

0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0

-1  2   3
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0
0   0   0

Which is correct. My question is why is init_heap affecting the fractions array even though in init_heap I am only editing and accessing the freeBlocks array?

Upvotes: 0

Views: 96

Answers (3)

Jonathan Leffler
Jonathan Leffler

Reputation: 753705

I think your problem lies in these two definitions:

fraction heap[][ARRAYSIZE] = {0};
block freeBlocks[][ARRAYSIZE] = {0};

Each defines an array with size 1 for the leading dimension, because you only provide 1 initializer.

int x;
for (x = 0; x < ARRAYSIZE; x ++){    

    block *currBlock = freeBlocks[x];
    currBlock->isFree = 1;  
}

This code is indexing through the array of arrays one unit of 10 blocks at a time (and is trampling way out of bounds). freeblocks[1] is beyond the end of the space allocated for freeblocks; freeblocks[9] is even further out of control.

Upvotes: 1

NPE
NPE

Reputation: 500347

From the way you're using them, it seems that heap and freeBlocks are meant to be 1D arrays, not 2D.

If that's the case, the following:

fraction heap[][ARRAYSIZE] = {0};
block freeBlocks[][ARRAYSIZE] = {0};

should become

fraction heap[ARRAYSIZE] = {0};
block freeBlocks[ARRAYSIZE] = {0};

Upvotes: 1

Lundin
Lundin

Reputation: 213809

Don't declare your arrays as heap[][ARRAYSIZE], it doesn't make any sense, especially not in the way you are trying to use them. Instead, declared them as heap[ARRAYSIZE].

Upvotes: 1

Related Questions