简体   繁体   English

如何在memcpy之前分配结构数组

[英]How to malloc struct array before memcpy

I think it is ok to do memcpy without malloc'ing check. 我认为不用mealloc'ing检查就可以进行memcpy。 However, I am unsure how shall we correct the code below, ie. 但是,我不确定如何纠正下面的代码,即。 How do we malloc struct array 'check'? 我们如何分配结构数组“检查”?

Here is the definition of the struct: 这是该结构的定义:

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

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

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

We initialized it with some values: 我们用一些值初始化了它:

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[];

in main() 在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);

[Solved by Michael Burr and JKB] [由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);
struct contain **check = malloc(n*sizeof(struct contain *));

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

might be this is safe way to allocate.what you want here n is how much array size you want. 可能是这是一种安全的分配方法。您要在此处n是所需的数组大小。

and then 接着

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

This: 这个:

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

is invalid because you're copying an array of structures into an array of pointers. 无效,因为您要将结构数组复制到指针数组中。

Keep in mind that the set of structures that you dynamically allocate is not contiguous. 请记住,您动态分配的结构集不是连续的。 The array of pointers is contiguous, but they point to things that are separately alllocated. 指针数组是连续的,但是它们指向分别分配的对象。

Try: 尝试:

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

Also, if the structure copies are always allocated/deallocated as a block (like in your example), there's no need to allocate them separately. 另外,如果结构副本始终作为一个块进行分配/取消分配(如您的示例中所示),则无需单独分配它们。 Just allocate enough space for the whole array: 只需为整个数组分配足够的空间即可:

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM