0

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?

1
  • Are you sure you mean to have an array of arrays? As it is now, heap (for example) is an array with a single entry, and that entry is an array of ARRAYSIZE fractions. This means you can't do e.g. heap[x] if x is larger than zero, as that will be out of bounds! Commented Apr 10, 2013 at 6:37

3 Answers 3

1

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].

Sign up to request clarification or add additional context in comments.

1 Comment

write then instead fraction* tempFrac = &heap[x]; or fraction* tempFrac = heap + x;
1

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};

2 Comments

Of course! Duh! I made this fix and now both arrays are printing out correctly. But regardless, how could this have caused the error I was experiencing?
@user2252004: You wrote past the allocated memory. Technically, this is undefined behaviour, but in practice you were just overwriting the memory that followed (i.e. the other array).
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.