Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C程序:分段错误_C_Arrays_Pointers_Segmentation Fault - Fatal编程技术网

C程序:分段错误

C程序:分段错误,c,arrays,pointers,segmentation-fault,C,Arrays,Pointers,Segmentation Fault,我目前正在尝试解决一个任务,这对我这个C语言的初学者来说是很难处理的,所以我到了这个地步,我不知道该做什么了 我的任务是用几个函数实现多项式。。。。 我认为,在查看代码时,函数应该是清晰的 我的确切问题是,我没有得到一个编译器错误,而是一个分段错误。我标记了调试尝试的方向。但我完全不知道我要改变什么。我希望有人能帮我修改代码 下面是三个代码部分: 第一名:保利 #include <stdlib.h> #include <stdio.h> #include <ass

我目前正在尝试解决一个任务,这对我这个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
  }
我感谢你的帮助。我希望你能帮我解决这个问题,并请耐心等待我一个新手来。。。
提前感谢

您没有正确分配或释放内存,函数甚至没有返回指针!我认为您试图为结构及其包含的数组分配一块内存,但结构不包含数组:只包含指向数组的指针。您必须单独分配它们:

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;

你试过使用调试器吗?它们通常对查找运行时错误(如分段错误)的位置非常有用。添加到Frxstrem的注释中:Read here可能会有所帮助:但是,您到底想在这里实现什么:
poly_t*p=poly_alloc(argc-3)?您通常通过
3-3
,至少在遵循用法时是这样。根据
gdb
,分段错误出现在poly.c中的第31行:
p->coefs[deg]=coeff
poly\u alloc
永不返回值。嘿,谢谢你的回答!你所说的确实帮助我修复了我的代码,我对它进行了测试,直到它释放了分配的内存,但随后我又出现了另一个错误,free()invalid size(fast),如果我在谷歌上搜索到的是真的,当我写下我创建的数组并到达一个超出界限的点时,就会发生这种错误。你知道那可能在我的代码里吗?编辑:我找到了:我必须更改以下行:heap_p->coefs=malloc((度+1)*sizeof(int));因为如果我的度数是3,我需要4个系数的空间a1*x^3+a2*x^2+a3*x^1+a4*x^0
p->coeffs[deg] = coeff;
int *ptr = p->coeffs + deg;
*ptr = coeff;