C程序:分段错误

时间:2015-05-24 17:00:41

标签: c arrays pointers 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 ... 提前致谢

1 个答案:

答案 0 :(得分:0)

您没有正确分配或释放内存,并且该功能甚至没有返回指针!我认为你试图为struct 它包含的数组分配一块内存,但是struct不包含一个数组:只有一个指针到一个数组。你必须分开分配它们:

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;