C程序:段错误

C program: Segmentation Fault

我目前正在尝试解决一个任务,这对我这个 C 初学者来说很难处理,所以我到了这个地步,我不知道该做什么了。

我的任务是用几个函数实现多项式.... 功能我觉得看代码应该就清楚了

我的确切问题是我没有遇到编译器错误,而是遇到了分段错误。我标记了我尝试调试的地方。但我完全不知道我必须改变什么。我希望有人能帮我修复我的代码。

下面是三个代码部分: 第一名:poly.c

#include <stdlib.h> 
#include <stdio.h>
#include <assert.h>
#include "poly.h"

struct poly_t { 
  unsigned degree;
  int *coeffs; 
  }; 


  //constructor: heap
  poly_t *poly_alloc(unsigned degree){

    poly_t *heap_p;
    heap_p = malloc(sizeof(*heap_p)+(degree+1)*sizeof(int)); //or malloc(sizeof(*heap_p)*(degree+1)) furthermore not sure if degree or degree +1

  }

 //free heap 
  void poly_free(poly_t *p){
    int *coeffs = p->coeffs;
    free(coeffs);
    free(p);
  }


  void poly_set_coeff(poly_t *p, unsigned deg, int coeff){
    p->degree = deg;
    p->coeffs += deg;
    p->coeffs[deg] = coeff;

    //does not work Segmentation Fault not sure what to do
    //p->coeffs += deg;
    //*p->coeffs = coeff;
       printf("%d",*p->coeffs);
  }

//different variations
  poly_t *poly_linear(poly_t *p, int a1, int a0){
    p->degree=1;
    *p->coeffs=a1;
    p->coeffs++;
    *p->coeffs=a0;
    p->coeffs--;
  }


  poly_t *poly_quadratic(poly_t *p, int a2, int a1, int a0){
    p->degree=2;
    *p->coeffs=a2;
    p->coeffs++;
    *p->coeffs=a1;
    p->coeffs++;
    *p->coeffs=a0;
    p->coeffs-=2;
  }

//evaluate using horner
  int poly_eval(poly_t const *p, int x){
    int d = p->degree;
    int next;
    int adr = *p->coeffs;
    int *arr = p->coeffs;
    int res = arr[d];
    for(int i=0; i<=d; i++){
      adr+=(d-i);
      next = arr[adr];
      adr-=(d-i);
      res = res*x+next;
    }
    return res;
  }


  //constructor : .txt
  poly_t *poly_alloc_d(){
    //needs to be finished
  }

第二名:main.c

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

int main(int argc, char** argv){

  if(argc<3){
    fprintf(stderr, "syntax: %s x coeffs...", argv[0]);
    return 1;
  }

  poly_t *p = poly_alloc(argc-3);

  for(int i = 2; i<argc; i++){
    int coeff = atoi (argv[i]);
    poly_set_coeff(p, i-2, coeff);
  }
  return 0;//for debugging 


  int x=atoi(argv[1]);
  int y=poly_eval(p,x);
  poly_free(p);
  printf("%d\n", y);

  return 0;

}

最后是我的头文件: poly.h

#ifndef POLY_H 
#define POLY_H

/* unvollständiger Verbund */ 
typedef struct poly_t poly_t;
poly_t *poly_alloc(unsigned degree);
void poly_free(poly_t *p);
void poly_set_coeff(poly_t *p, unsigned deg, int coeff);
int poly_eval(poly_t const *p, int x);

#endif /* POLY_H */

感谢您的每一次帮助。我希望你能帮我解决这个问题,请耐心等待我是 C 的新手...... 提前致谢

您没有正确分配或释放内存,函数甚至没有 return 指针!我想你试图为结构 和它包含的数组 分配一个内存块,但结构不包含数组:只有一个 pointer 到一个数组。您必须单独分配它们:

typedef struct { 
    unsigned degree;
    int *coeffs; 
    } poly_t; 

//constructor: heap
poly_t *poly_alloc(unsigned degree){

    poly_t *heap_p;
    heap_p = malloc(sizeof(*heap_p));
    if (heap_p == NULL)
        exit (1);           // allocation error

    heap_p->coeffs = malloc(degree * sizeof(int));
    if (heap_p->coeffs == NULL)
        exit (1);           // allocation error
    return heap_p;
}

//free heap
void poly_free(poly_t *p){
    free(p->coeffs);
    free(p);
}

还有其他错误,例如

p->coeffs += deg;

你不能玩分配的内存指针,你已经正确地这样做了

p->coeffs[deg] = coeff;

尽管您可以根据需要使用中间指针:

int *ptr = p->coeffs + deg;
*ptr = coeff;