2012-05-11 69 views
-1
typedef struct{ 
    int number; 
    char name[100]; 
} Apple 

typedef struct{ 
    Apple *apple; 
    int bit[2]; 
} AppleArray; 

int main(){ 
    AppleArray *aArray; 
    loadApple(&aArray); 
} 

loadApple(AppleArray **aArray){ 
    *aArray = NULL; 
    for(i=0; i<100; i++){ 
    *aArray = realloc(*aArray, (i+1) * sizeof(AppleArray)); 

    /*SEGFAULT here*/ 
    aArray[i]->apple = NULL; 

    for(j=0; j<2; j++){ 
     aArray[i]->apple = realloc(aArray[i]->apple, sizeof(Apple) * (j+1)) 
    } 
    } 

} 

我想擁有一個具有特定大小的AppleArray。每個AppleArray將有兩個Apple。但是,當我將NULL分配給aArray [i] - > apple時,我得到段錯誤。那裏有什麼問題?獲取結構數組中的結構的值

編輯:

loadApple(AppleArray **aArray){ 
    *aArray = malloc(100 * sizeof(AppleArray)); 
    for(i=0; i<100; i++){ 

    /*SEGFAULT here*/ 
    aArray[i]->apple = NULL; 

    for(j=0; j<2; j++){ 
     aArray[i]->apple = realloc(aArray[i]->apple, sizeof(Apple) * (j+1)) 
    } 
    } 
} 
+0

我認爲它應該是* aArray [i]。請檢查我的答案。 – Jay

+0

@Jay錯誤:行中的賦值中的不兼容類型* aArray [0] - > apple = NULL; –

+0

Try(* aArray)[0] .apple = NULL; – Jay

回答

1

你只能在被像malloc()calloc()返回給您由之前的內存分配函數的地址來調用realloc()否則它給你未定義行爲

C99標準7.20.3.4-3:本realloc函數:

void *realloc(void *ptr, size_t size); 

If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size. Otherwise, if ptr does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to the free or realloc function, the behavior is undefined.

+0

我已經設置了* aArray = NULL。 但我不能將aArray [i] - > apple設置爲NULL。 –

+0

設置* aArray爲NULL是不必要的,只要您使用** malloc **分配內存而不是重新分配內存。 – SirDarius

+0

編輯爲使用malloc。它仍然是一樣的。 –

1

除了什麼阿爾斯已上面提到的,你也有一個數組出界外看問題,你的循環後,我的價值將是100,你正試圖訪問aArray[100]而在你的邏輯(雖然不正確)你只會分配內存,直到aArray[99]

你loadApple功能可以改寫如下:

loadApple(AppleArray **aArray) 
{ 
    *aArray = NULL; 
    *aArray = malloc(100 * sizeof(AppleArray)); 

    //I have shown index as 0 here just as an example. 
    (*aArray)[0].apple = NULL;  
    (*aArray)[0].apple = malloc(2 * sizeof(Apple)); 

} 

此代碼也應該給你的代碼的邏輯是試圖實現相同的行爲。

+0

@Mahmoud,謝謝你編輯我的答案。我沒有得到標記代碼等選項或使一個字粗體等。有誰知道爲什麼我的瀏覽器(IE)不顯示這些?之前它被用來顯示。 – Jay

+0

錯誤:行中的賦值中的不兼容類型* aArray [0] - > apple = NULL; –

+0

@Jay沒有問題。我注意到更新會在夜間頻繁進行。也許是一個短暫的錯誤 - 我在Chrome上。 –

0
<pre> 
#include "stdio.h" 
#include "stdlib.h" 

#define MAX_SIZE 5 
#define NAME_SIZE 100 
#define APPLE_NUM 2 

typedef struct 
{ 
    int number; 
    char name[NAME_SIZE]; 
}Apple; 

typedef struct 
{ 
    Apple* apple; 
    int bit[2]; 
}AppleArray; 

void printApple(AppleArray** aArray) 
{ 
    int i; 
    for (i = 0; i < MAX_SIZE; ++i) 
    { 
     fprintf(stderr, "%4d: bit[0] = %d, bit[1] = %d\n", i, (*aArray + i)->bit[0], (*aArray + i)->bit[1]); 

     int j; 
     for (j = 0; j < APPLE_NUM; ++j) 
     { 
      fprintf(stderr, "\tapple[%d]: number = %d, name = %s\n", 
        j, 
        (*aArray + i)->apple[j].number, 
        (*aArray + i)->apple[j].name); 
     } 

     printf("\n"); 
    } 
} 

void loadApple(AppleArray **aArray) 
{ 
    *aArray = NULL; 
    int i; 
    for(i = 0; i < MAX_SIZE; i++) 
    { 
     AppleArray* tmp = (AppleArray*)realloc(*aArray, (i+1) * sizeof(AppleArray)); 
     if (tmp != NULL) 
     { 
      *aArray = tmp; 
     } 
     else 
     { 
      //error 
      free(*aArray); 
      *aArray = NULL; 
      exit(0); 
     } 

     /*SEGFAULT here*/ 
     //aArray[i]->apple = NULL; 
     (*aArray + i)->apple = NULL; 
     (*aArray + i)->bit[0] = i; 
     (*aArray + i)->bit[1] = i + 1; 

     /* 
     int j; 
     for (j = 0; j < 2; j++) 
     { 
      (*aArray + i)->apple = realloc(aArray[i]->apple, sizeof(Apple) * (j+1)); 
     } 
     */ 

     (*aArray + i)->apple = (Apple*)realloc(NULL, sizeof(Apple) * APPLE_NUM); 

     int j; 
     for (j = 0; j < APPLE_NUM; ++j) 
     { 
      (*aArray + i)->apple[j].number = j; 
      snprintf((*aArray + i)->apple[j].name, NAME_SIZE, "apple_%d_%d", i, j); 
     } 
    }//for 
} 

void destroyApple(AppleArray* thiz) 
{ 
    if (thiz == NULL) 
    { 
     return; 
    } 

    int i; 
    for (i = 0; i < MAX_SIZE; ++i) 
    { 
     free(thiz[i].apple); 
     thiz[i].apple = NULL; 
    } 

    free(thiz); 
} 

int main() 
{ 
    AppleArray *aArray; 
    loadApple(&aArray); 
    printApple(&aArray); 
    destroyApple(aArray); 

    return 0; 
}