不明原因的分段故障(核心转储)

时间:2015-08-10 14:22:52

标签: c

我正在学习c编程。我已将此代码编写为赋值的一部分并继续获取分段错误。 原型:

#define ALLOC_ERROR -1

typedef struct monom
{
  int coefficient;    
  int power;        
} Monom;

int insertNewMonom(Monom** polynom, int base, int power, unsigned int size);
bool findPlace(Monom** polynom, int power, unsigned int size, int* place);
void printPolyMul(Monom* polynom1, unsigned int polynom1Size, Monom* polynom2, unsigned int polynom2Size);

以下是相关代码:

void printPolyMul(Monom* polynom1, unsigned int polynom1Size, Monom* polynom2, unsigned int polynom2Size)
{
    Monom *mulPolynom = NULL, *temp = NULL;
    unsigned int  count = 0;
    int i, j, size=0, base, power;

    for(i=0; i<polynom1Size; i++){
      for(j=0; j<polynom2Size; j++){ 
        if(count==size){
            size = (size*2)+1;
            temp = realloc (sumPolynom, size * sizeof *sumPolynom);
            if(!temp){
                fprintf(stderr, "Allocation error\n");
                exit(ALLOC_ERROR);
            }
            else
                sumPolynom = temp;
        }                       
        base = (polynom1[i].coefficient)*(polynom2[j].coefficient);
        power = (polynom1[i].power)+(polynom2[j].power);
        count += insertNewMonom(&mulPolynom, base, power, count);
    }
}

temp =  realloc (mulPolynom, count * sizeof *mulPolynom);
if(!temp){
    fprintf(stderr, "Allocation Error\n");
    exit(ALLOC_ERROR);
}
else 
    mulPolynom = temp;

printPolynom(mulPolynom, count);            
free(mulPolynom);
}
int insertNewMonom(Monom** polynom, int base, int power, unsigned int size)
{
    int i, place;
    bool new_flag;
    Monom tempMonom;

    tempMonom.coefficient = base;
    tempMonom.power = power;    

    if(!base)
        return 0;
    new_flag = findPlace(polynom, power, size, &place);
    if(new_flag){
        if(place==size){
            (*polynom)[place] = tempMonom;
            return 1;
        }
        else{
            for(i=size; i>place; i--)
                (*polynom)[i] = (*polynom)[i-1];
            (*polynom)[place] = tempMonom;
            return 1;
        }
    }
    else{
        polynom[place]->coefficient += base;
        return 0;
    }   
}

bool findPlace(Monom** polynom, int power, unsigned int size, int* place)
{
    int curr;   
    for(curr=0; curr<size; curr++){
        if(polynom[curr]->power==power){
            *place = curr;
            return false;
        } 
        if(polynom[curr]->power<power){
            if(curr)        
                *place = curr-1;
            else
                *place = curr;
            return true;
        }
    }
    *place = size; 
    return true;
}

我设法找到代码崩溃的确切位置。当检查bool findPlace(Monom** polynom, int power, unsigned int size, int* place)时,第三次调用curr=1,第二次调用迭代(polynom[curr]->power)。 我会提到先前使用insertNewPolynom()来接收polynom1polynom2的程序并且工作得很好。

2 个答案:

答案 0 :(得分:1)

分配的尺寸太小。

考虑size = polynom1Size*polynom2Size; realloc (mulPolynom, size * sizeof *mulPolynom); ... if(place==size){ (*polynom)[place] = tempMonom;

代码正在尝试修改元素[size],但只有元素0,1,2, ... size-1的空间。

如果不深入挖掘,则需要realloc (mulPolynom, (size + 1) * sizeof *mulPolynom);

答案 1 :(得分:0)

在insertNewMonom中你有语句

 (*polynom)[place] = tempMonom;

tempMonom是一个局部变量,在函数返回时不再存在。稍后对它的任何引用都会产生垃圾或崩溃。