如何在memcpy之前使用malloc struct数组

时间:2014-02-12 05:53:26

标签: c malloc memcpy

我认为没有malloc'ing检查就可以做memcpy。但是,我不确定如何纠正下面的代码,即。我们如何malloc struct array'check'?

以下是结构的定义:

struct contain {
char* a;        //
int allowed;    //

struct suit {
   struct t {
          char* option;
          int count;
   } t;

   struct inner {
          char* option;
          int count;
   } inner;
} suit;
};

我们用一些值初始化它:

struct contain structArrayToBeCheck[] = {
    {
        .a = "John",
        .allowed = 1,

          .suit = {
            .t = {
                .option = "ON",
                .count = 7
            },

            .inner = {
                .option = "OFF",
                .count = 7
            }
        }
    },
    {
        .a = "John",
        .allowed = 1,

        .suit = {
            .t = {
                .option = "ON",
                .count = 7
            },

            .inner = {
                .option = "OK",
                .count = 7
            }
        }
    },
     {
        .a = "John",
        .allowed = 1,

        .suit = {
            .t = {
                .option = "ON",
                .count = 7
            },

            .inner = {
                .option = "OFF",
                .count = 7
            }
        }
    },

};
struct contain check[];

在main()

   int i;

   int n = sizeof(structArrayToBeCheck)/sizeof(struct contain);
   printf( "There are %d elements in the array.\n", n);

   struct contain **check = malloc(n*sizeof(struct contain *));

   for (i = 0; i != n ; i++) {
       check[i] = malloc(sizeof(struct contain));
   }

   memcpy(&check, &structArrayToBeCheck, sizeof(structArrayToBeCheck));
   //printf( "check is %s\n", check[1]->suit.inner.option);

[Michael Burr和JKB解决]

   int i;

   int n = sizeof(structArrayToBeCheck)/sizeof(struct contain);
   printf( "There are %d elements in the array.\n", n);

   struct contain *check = malloc(n*sizeof(struct contain));

   memcpy( check, structArrayToBeCheck, sizeof(structArrayToBeCheck));

   // do things with check[0], check[1], ... check[n-1]
   printf( "check is %s\n", check[1].suit.inner.option);

   free(check);

2 个答案:

答案 0 :(得分:1)

struct contain **check = malloc(n*sizeof(struct contain *));

for (int i = 0; i != n ; i++) {
    check[i] = malloc(sizeof(struct contain));
} 

可能这是分配的安全方式。你想要的是n你需要多少数组大小。

然后

// Do not forget to free the memory when you are done:
for (int i = 0; i != n ; i++) {
    free(check[i]);
}
free(check);

答案 1 :(得分:1)

此:

memcpy(&check, &structArrayToBeCheck, sizeof(structArrayToBeCheck));

无效,因为您正在将结构数组复制到指针数组中。

请记住,您动态分配的结构集不是连续的。指针数组是连续的,但它们指的是单独分配的东西。

尝试:

for (i = 0; i != n ; i++) {
    check[i] = malloc(sizeof(struct contain));
    memcpy( check[i], ArrayToBeCheck[i], sizeof(ArrayToBeCheck[i]));
}

此外,如果结构副本始终作为块分配/解除分配(如您的示例中所示),则无需单独分配它们。只需为整个数组分配足够的空间:

struct contain *check = malloc(n*sizeof(struct contain));

memcpy( check, structArrayToBeCheck, sizeof(structArrayToBeCheck));

// do things with check[0], check[1], ... check[n-1]

free(check);