2013-11-22 45 views
0

崩潰的錯誤在main.c問題與指針和realloc,用程序用C

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

int main (void) 
{ 
    struct poly *p0 = polySetCoefficient (polySetCoefficient (polySetCoefficient (polyCreate() , 0, 4.0), 1, -1.0), 10, 2.0); 

    //polyPrint (p0); 

    return 0; 
} 

poly.c我剛剛列入,我處理兩種功能,它好像polySetCoefficient的是,竟然放棄我的人問題。我已經評論了printf語句,但是我使用這些來確定程序崩潰的位置。顯然,它在崩潰之前實際上會經歷整個函數,所以我不確定錯誤是從哪裏來的。其次,在我的main.c文件中,我只調用了兩個函數。另一個問題是,每次我經歷polySetCoefficient時,我的以前的條目都被替換爲0。我認爲這可能是因爲我將數組中的元素設置爲0的方式,但我仔細確保將元素的前一個大小設置爲0,直到數組的新大小(不包括最後一個索引)。

struct poly *polyCreate() 
{ 

    struct poly *q; 

    q = malloc(sizeof(struct poly)); 
    q->c = malloc(sizeof(double)); 

     q->c[0] = 0.0; 

    q->size = 0; 

    //printf("polyCreate: %g\n", q->c[0]); 

    return q; 
} 

struct poly *polySetCoefficient(struct poly *p, int i, double value) 
{ 
    //printf("%d\n", i*sizeof(double)); 

    if (p->size < i) 
    { 
     printf("Old: %d, New: %d\n", sizeof(p->c)/sizeof(double), i); 

     p->c = realloc(p->c, i+1*sizeof(double)); 

     printf("New: %d \n", sizeof(p->c)); 

     for(int l = p->size; l <= i; l++) 
     { 

      if(l != i) 
      { 
       p->c[l] = 0; 
       printf("set to 0\n"); 
      } 
      else 
      { 
       p->c[l] = value; 
       printf("F:set to %g\n", p->c[i]); 
      } 
     } 

     printf("Did we come here?\n"); 

     p->size = i; 

    } else { 

     p->c[i] = value; 

    } 

    printf("The %d'th coefficient is %g\n", i, p->c[i]); 

    printf("Cof 0: %g, Cof 1: %g, Cof 10: %g", p->c[0], p->c[1], p->c[10]); 


    return p; 


} 

poly.h

struct poly 
{ 
    double *c; 
    int size, length; 
}; 


struct poly *polyCreate(); 
struct poly *polyDelete(struct poly *p); 
struct poly *polySetCoefficient (struct poly *p, int i, double value); 
double polyGetCoefficient (struct poly *p, int i); 
int polyDegree (struct poly *p); 
void polyPrint (struct poly *p); 
struct poly *polyCopy (struct poly *p); 
struct poly *polyAdd (struct poly *p0, struct poly *p1); 
struct poly *polyMultiply (struct poly *p0, struct poly *p1); 
struct poly *polyPrime (struct poly *p); 
double polyEval (struct poly *p, double x); 
+1

順便說一句,你使用'realloc'不正確。如果'realloc'失敗,那麼你失去了原來的指針,即你泄漏了內存。您需要首先分配給一個臨時指針。另外,在創建結構體的實例之後,爲指針成員分配一個double。爲什麼你將'size'設置爲0?這只是令人困惑,它應該是1. –

+0

大小基本上是數組的索引,使它更容易處理,當我將它設置爲0.我爲指針成員分配一個雙重實質上創建一個數組與一個條目具有零值。我不明白代碼中的錯誤,假設realloc不會失敗。 – Maaz

+0

好的,但請注意,size是實際上是索引的一個非常奇怪的名稱。大多數人會認爲這意味着,你知道......陣列的「大小」。 –

回答

1

這個代碼的版本完全編譯並運行plausib (但不是真的正確)沒有崩潰。

一個關鍵的變化是由immibis在他的answer中確定的一個,即更正的尺寸計算。

另一個關鍵變化是當沒有係數10時沒有打印係數10。如果您嘗試儘快打印,則會調用未定義的行爲;什麼都可能發生。

我修正了一些printf()格式;在一臺64位機器上,您需要使用%zu(或%zd)來打印size_t。我刪除了該結構的未使用的length成員。我真的不知道如何整齊地格式化你的單線多線電話。我不介意略長於80個字符,但我通常不會超過120.我絕對更喜歡p1版本,不僅僅是因爲它可能會隨着您的錯誤檢查。目前,我用assert(ptr != 0);應該有一個分配錯誤檢查。

輸出:

The 0'th coefficient is 4 
The 1'th coefficient is -1 
Old: 1, New: 10 
New: 8 
p[1] set to 0 
p[2] set to 0 
p[3] set to 0 
p[4] set to 0 
p[5] set to 0 
p[6] set to 0 
p[7] set to 0 
p[8] set to 0 
p[9] set to 0 
p[10] set to 2 
Did we come here? 
The 10'th coefficient is 2 
Cof 0: 4, Cof 1: 0, Cof 10: 2 

注意,當你擴展到10

代碼,你打一頓第一系數爲零:

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

struct poly 
{ 
    double *c; 
    int size; 
}; 

struct poly *polyCreate(void); 
struct poly *polySetCoefficient(struct poly *p, int i, double value); 

int main(void) 
{ 
    /* I don't know how to format this neatly! */ 
    struct poly *p0 = polySetCoefficient(
     polySetCoefficient(
      polySetCoefficient(polyCreate(), 0, 4.0), 
      1, -1.0), 
     10, 2.0); 

    assert(p0 != 0); 

    /* 
    struct poly *p1 = polyCreate(); 
    p1 = polySetCoefficient(p1, 0, 4.0), 
    p1 = polySetCoefficient(p1, 1, -1.0); 
    p1 = polySetCoefficient(p1, 10, 2.0); 
    */ 

    return 0; 
} 

struct poly *polyCreate(void) 
{ 
    struct poly *q = malloc(sizeof(struct poly)); 
    assert(q != 0); 
    q->c = malloc(sizeof(double)); 
    assert(q->c != 0); 

    q->c[0] = 0.0; 
    q->size = 1; 

    return q; 
} 

struct poly *polySetCoefficient(struct poly *p, int i, double value) 
{ 
    assert(p != 0); 

    if (p->size < i) 
    { 
     printf("Old: %zu, New: %d\n", sizeof(p->c)/sizeof(double), i); 

     p->c = realloc(p->c, (i+1)*sizeof(double)); 
     assert(p->c != 0); 

     printf("New: %zu\n", sizeof(p->c)); 

     for (int l = p->size; l <= i; l++) 
     { 
      if (l != i) 
      { 
       p->c[l] = 0; 
       printf("p[%d] set to 0\n", l); 
      } 
      else 
      { 
       p->c[l] = value; 
       printf("p[%d] set to %g\n", i, p->c[i]); 
      } 
     } 

     printf("Did we come here?\n"); 

     p->size = i; 
    } 
    else 
    { 
     p->c[i] = value; 
    } 

    printf("The %d'th coefficient is %g\n", i, p->c[i]); 

    if (i >= 10) 
     printf("Cof 0: %g, Cof 1: %g, Cof 10: %g", p->c[0], p->c[1], p->c[10]); 

    return p; 
} 
+0

通過使for循環將控制變量初始化爲p-> size + 1來固定爲零的設置,因此它不會將p-> size中的元素設置爲0。 – Maaz

1

丟失的括號這裏可能是這個問題:

p->c = realloc(p->c, i+1*sizeof(double)); 

將其更改爲:

p->c = realloc(p->c, (i+1)*sizeof(double)); 
+0

嗯,那是我的愚蠢。這似乎解決了錯誤,但是,問題是當我重複調用此函數時,就像在main.c文件中完成它重置之前分配的數組元素的值一樣。 例如: 第一迭代將例如設置第一元件4, 然後第二次迭代將所述第三元件設置爲2,但隨後的第一元件不再具有4,因爲它的值,而不是它具有0 。 – Maaz