简体   繁体   English

动态分配给结构的指针数组有什么问题?

[英]What is wrong with my dynamically allocated array of pointers to structs?

I have the following program that references array elements through a double pointer. 我有以下程序通过双指针引用数组元素。

typedef struct {
  int num1;
  int num2;
  int num3;
} DATA_SET;

typedef struct {
  int          structID;                                              
  DATA_SET     *data_set_array;  // Pointer to an array of DATA_SET structs
} MY_STRUCT;

int main () {
   MY_STRUCT   *Struct1;
   DATA_SET    **DataSetArray;   // Array of pointers

   Struct1 = malloc(sizeof(MY_STRUCT));
   Struct1->data_set_array = malloc(sizeof(DATA_SET*))  //Allocate mem for the pointer to array of DATA_SETs

   DataSetArray = malloc(sizeof(DATA_SET*) * 2)  // Allocate mem for an array of 2 DATA_SET pointers

   DataSetArray[0] = malloc(sizeof(DATA_SET))    // Allocate mem for the actual DATA_SET struct
   DataSetArray[0]->num1 = 1;
   DataSetArray[0]->num2 = 2;
   DataSetArray[0]->num3 = 3;

   DataSetArray[1] = malloc(sizeof(DATA_SET))    // Allocate mem for the actual DATA_SET struct
   DataSetArray[1]->num1 = 1;
   DataSetArray[1]->num2 = 2;
   DataSetArray[1]->num3 = 3;

   memcpy(Struct1->data_set_array, *DataSetArray, sizeof(DATA_SET*);  //Copy data set array into Struct1

When I print all the data out in Struct1, i get: 当我在Struct1中打印所有数据时,我得到:

   Struct1->data_set_array[0].num1 = 1
   Struct1->data_set_array[0].num2 = 2
   Struct1->data_set_array[0].num3 = 3
   Struct1->data_set_array[1].num1 = 50       //This should be 1
   Struct1->data_set_array[1].num2 = 50       //This should be 2
   Struct1->data_set_array[1].num3 = 65       //This should be 3

Seems to be misuse/data corruption for the 2nd element in the array. 似乎是数组中第二个元素的滥用/数据损坏。

I know there's probably different ways to do this, but I wanted to get familiar with referencing the array indices via double pointers. 我知道可能有不同的方法来执行此操作,但是我想熟悉通过双指针引用数组索引。 Am I allocating memory properly? 我是否正确分配了内存? I have a feeling the memcpy is incorrect. 我感觉到memcpy不正确。

Struct1->data_set_array = malloc(sizeof(DATA_SET*)
/*                                              ^ wrong */

DataSetArray = malloc(sizeof(DATA_SET*) * 2)
/*                                   ^ wrong */

You need to do sizeof(DATA_SET) because that is the actual size of a struct, not a pointer to a DATA_SET struct. 您需要执行sizeof(DATA_SET)因为这是结构的实际大小,而不是指向DATA_SET结构的指针。

You're making the mistake of allocating only the size of a pointer times 2, which in many cases can be smaller than you'd hope for. 您犯了一个错误,即仅分配一个指针大小乘以2,这在许多情况下可能比您希望的要小。

memcpy(Struct1->data_set_array, *DataSetArray, sizeof(DATA_SET*);

This is also wrong. 这也是错误的。 You must copy the sizeof(DATA_SET) . 您必须复制sizeof(DATA_SET) Keep in mind that sizeof(DATA_SET) is not equal to sizeof(DATA_SET*). 请记住,sizeof(DATA_SET)不等于sizeof(DATA_SET *)。 A pointer's size in bytes is the same regardless of the type of pointer. 无论指针的类型如何,指针的大小(以字节为单位)都是相同的。

What is wrong with my dynamically allocated array of pointers to structs? 动态分配给结构的指针数组有什么问题?

DataSetArray is an array of pointers to struct and there is not anything wrong with it. DataSetArray是一个指向struct的指针数组,它没有任何问题。

The problem is 2 other things: 问题是另外两件事:

You can't use memcpy on an array of pointers to struct. 您不能在指向struct的指针数组上使用memcpy The memory isn't consecutive. 内存不连续。

Further, data_set_array is not an array of pointers to struct so you are trying to copy between incompatible types. 此外, data_set_array不是结构指针的数组,因此您尝试在不兼容的类型之间进行复制。

I guess you basically want to create a Double Dimension array. 我想您基本上是想创建一个二维数组。 So basically you can do the code like this 所以基本上你可以像这样做代码

struct abc {
    int i;
};

struct abc **arr;
arr = (struct abc **) malloc(sizeof(struct abc) * 2);
arr[0] = (struct abc*)malloc(sizeof(struct abc));
arr[0]->i = 100;
arr[1] = (struct abc*)malloc(sizeof(struct abc));
arr[1]->i = 200;

One thing that you need to remember is that the outer array is just a pointer, so it doesn't need to be sizeof(abc) , it could very well be sizeof(int*) / sizeof (void*) etc. 您需要记住的一件事是,外部数组只是一个指针,因此它不必是sizeof(abc),它很可能是sizeof(int *)/ sizeof(void *)等。

so basically the above code could be written as 所以基本上上面的代码可以写成

struct abc **arr;
arr = (struct abc **) malloc(sizeof(int *) * 2);
arr[0] = (struct abc*)malloc(sizeof(struct abc));
arr[0]->i = 100;
arr[1] = (struct abc*)malloc(sizeof(struct abc));
arr[1]->i = 200;

Hope this helps in underlying memory management you want to do with your code 希望这可以帮助您对代码进行基础内存管理

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

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