【问题标题】:How to malloc struct array before memcpy如何在 memcpy 之前 malloc 结构数组
【发布时间】:2014-03-10 07:07:35
【问题描述】:

我认为在没有 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);

【问题讨论】:

  • "结构包含检查[];"你能使用这样的语法吗?这里的 sizeof(check) 是多少?

标签: c malloc memcpy


【解决方案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);

【讨论】:

  • struct contains check = malloc(nsizeof(struct contains)); 似乎不适用于 for (i = 0 ; i != n ; i++) { check[i] = malloc(sizeof(struct contains)); memcpy(检查[i], structArrayToBeCheck[i], sizeof(structArrayToBeCheck[i])); }
  • @Babbit:答案末尾的替代方案是连续数组的副本。在这种情况下,无需单独分配每个元素 - 整个数组被一次性复制。所以check[0]check[1]等直接引用struct contain对象,而不是指向此类对象的指针。
【解决方案2】:
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);

【讨论】:

  • JKB,你如何从那里访问内容? printf("check is %s\n", (*check)[1].suit.inner.option);不打印。
  • printf( "check is %s\n", check[1]->suit.inner.option); 对你有效
  • JKB,我也试过了,但没有打印任何东西。
  • 你能把你修改过的代码贴上我的修改吗?
  • checkstructArrayToBeCheck 在您不使用数组取消引用时已经是指针。删除 memcpy 中的 &。
猜你喜欢
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-04
  • 2015-07-30
  • 2015-02-12
  • 1970-01-01
  • 2016-02-12
相关资源
最近更新 更多