-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))
    }
  }
}
4

3 回答 3

1

除了上面提到的 Als 之外,您还遇到了数组越界读取问题,因为在您的循环之后,i 值将是 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));

}

此代码还应提供与您的代码逻辑试图实现的相同行为。

于 2012-05-11T08:02:41.003 回答
1

您只能调用realloc()由先前的内存分配函数返回给您的地址,malloc()否则calloc()它会给您Undefined Behavior

C99 标准 7.20.3.4-3:realloc 函数:

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

如果ptr是空指针,则 realloc 函数的行为类似于指定大小的 malloc 函数。否则,如果 ptr 与内存管理函数先前返回的指针不匹配,或者如果空间已通过调用 free 或 realloc 函数被释放,则行为未定义。

于 2012-05-11T07:48:17.710 回答
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;
}
于 2012-05-11T10:12:00.177 回答