2015-05-24 110 views
-2

我目前正試圖解決一個任務,這對我來說是一個初學者很難處理的任務,所以我到了這個地步,我不知道該怎麼做了。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新手... 在此先感謝

+2

您是否嘗試過使用調試器?它們通常對於查找運行時錯誤的位置非常有用,例如分段錯誤。 – Frxstrem

+0

添加到* Frxstrem *的評論:在這裏閱讀可能有幫助:http://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – alk

+1

但是,你到底想要在這裏實現什麼? :'poly_t * p = poly_alloc(argc-3);'?通常情況下,至少在遵循使用情況時通過'3-3'。 – alk

回答

0

你沒有正確分配或釋放內存,並且該功能甚至沒有返回指針!我想你試圖爲它包含的數組結構分配一塊內存,但該結構不包含數組:只有一個指針指向一個數組。你必須單獨對其進行分配:

還有其他的錯誤太多,例如

p->coeffs += deg; 

你決不可分配的內存指針玩,你已經做了正確的這樣

p->coeffs[deg] = coeff; 

雖然你可以使用中間指針,如果你想:

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

嘿將有用的printf小號調試它,謝謝你的答案!你說的確實幫助我修復了我的代碼,我測試了它並且它可以工作,直到它釋放分配的內存,但是然後我得到另一個錯誤,free()無效的大小(快速),這發生,如果我googled是真實的,當我寫入數組時,我創建並達到了一個超出界限的位置。你有什麼想法可以在我的代碼中?編輯:我發現它:我必須更改以下行:heap_p-> coeffs = malloc((degree + 1)* sizeof(int));因爲如果我的度數是3,我需要4個coeffs的空間a1 * x^3 + a2 * x^2 + a3 * x^1 + a4 * x^0 – WoodPecker