#include <stdio.h>
#include <stdlib.h>
#include "frac_heap.h"
#define ARRAYSIZE 10
#define ENDOFARRAY 999
fraction heap[ARRAYSIZE] = {0};
block freeBlocks[ARRAYSIZE] = {0};
int startingBlock = 0;
int nextFree = 0;
fraction* fracPointers[][ARRAYSIZE] = {0};
block* blockPointers[][ARRAYSIZE] = {0};
void init_Heap(){
int x;
for(x = 0; x < ARRAYSIZE; x ++){
block *currBlock = &freeBlocks[x];
currBlock->isFree = 1;
fraction *fractionPointer = &heap[x];
if(x<ARRAYSIZE - 1){
fractionPointer->denominator = x+1;
}
else if(x == ARRAYSIZE - 1){
fractionPointer->denominator = ENDOFARRAY;
}
}
}
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];
if(testFraction->numerator == 0 && testFraction ->denominator==0){
printf("Before return");
return testFraction;
}
}
int main(){
init_Heap();
dump_heap();
fraction *p1;
p1 = new_frac();
p1->sign = -1;
p1->numerator = 2;
p1->denominator = 3;
dump_heap();
}
尝试调用 new_frac() 时出现分段错误。此时我只是测试代码,我意识到 testfraction 不会总是 = &heap[0];。但是,我认为我能够访问我用'->'指向的部分结构?
在对其进行更多编辑之后,它似乎只有在达到 testFraction->denominator 时才会出现段错误。如果我只检查分母,它仍然会出现段错误,但它只适用于分子。