简体   繁体   English

如何将数组分配给结构中的指针?

[英]How to assign a array to a pointer in a struct?

I have the following structs as example: 我有以下结构作为示例:

#define MAX_PEOPLE 16
typedef struct {
  int age;
}Person;

typedef struct {
  Person *people;
  int numPeople;
}Team;

I'm trying to allocate an array of persons in a function, passed by parameters. 我试图在一个函数中分配一个由参数传递的人数组。 My Team is supposed to store an array of 16 pointers of Person . 我的Team应该存储一个包含16个Person指针的数组。 I can't figure out what I'm doing wrong. 我不知道我在做什么错。

void initiateTeam(Team * team){
  team->numPeople = MAX_PEOPLE;
  Person *p[MAX_PEOPLE];
  for(int i=0; i<MAX_PEOPLE;i++){
    p[i] = malloc(sizeof(Person);
  }
  team->people = &p[0];
}

I printed out the addresses of my team->people[i] and I'm getting random junk. 我打印出了我的team->people[i]的地址,然后我收到了随机垃圾。 Why is the assingment team->people = &p[0] wrong? 为什么评估team->people = &p[0]错误? Shouldn't it get the first address of my array then perform pointer arithmetic? 它不应该获取我数组的第一个地址然后执行指针算术吗?

You are pointing team->people to a statically defined array of person pointers. 您正在将team->people指向静态定义的人员指针数组。 Once the function ends, the stack pointer moves back to where main left off, erasing all previously local memory in the addPeople function. 函数结束后,堆栈指针将移回main断开的位置,从而擦除addPeople函数中所有以前的本地内存。 You need to malloc p , and return it from the function 您需要malloc p ,并从函数返回它

Since in the comments you stated that you're trying to allocate an array of Person objects and not pointers , you should rather do: 由于您在注释中表示要尝试分配Person对象而不是指针的数组,因此您应该这样做:

void addPeople(Team * team){
    team->numPeople = MAX_PEOPLE;
    team->people = malloc(sizeof(Person) * MAX_PEOPLE);
}

mind that there's no * in sizeof since you don't want an array of pointers but of objects. 请注意, sizeof没有* ,因为您不想要对象而是指针数组。 You will later be able to access the single elements (ie each Person object) with 您稍后将可以通过以下方式访问单个元素(即每个Person对象)

team->people[2].age = 25; // The third person in the array

Finally, remember to free your memory. 最后,请记住释放您的内存。

your variable p is allocated in the stack of the addPeople() function. 您的变量p分配在addPeople()函数的堆栈中。 The assignment team->people = &p[0] (which is equivalent to team->people = p ) is valid but dangerous because that address will be invalid as soon as the function is finished. 分配team->people = &p[0] (等效于team->people = p )是有效的,但很危险,因为该地址在函数完成后将立即无效。

Better create p with malloc(sizeof (Person *) * MAX_PEOPLE) instead of using the stack. 最好使用malloc(sizeof (Person *) * MAX_PEOPLE)创建p ,而不要使用堆栈。

The problem is here: 问题在这里:

Person *p[MAX_PEOPLE];

This allocates a local variable in the function to hold the array of people pointers. 这会在函数中分配一个局部变量来保存人员指针数组。

You get the address of this local variable and send it back. 您获得此局部变量的地址,然后将其发送回去。 As soon as you are not in the function the local data is freed. 一旦您不使用该功能,便会释放本地数据。 It was just allocated locally. 它只是在本地分配的。 It is no longer valid. 它不再有效。 I might work for a while or it might not depending on the the program does next. 我可能会工作一段时间,或者可能不取决于下一步执行的程序。

You want this: 你要这个:

Person **p = (Person **)malloc(sizeof (Person *) * MAX_PEOPLE);

I think you are getting the thing with pointer and memory management in C a little bit wrong. 我认为您在C语言中使用指针和内存管理时遇到了一些错误。 Consider reading a little bit more about it, before you continue coding your application. 在继续编写应用程序之前,请考虑阅读更多有关它的内容。

Beside, I think you do not need an array of pointers to persons, but an array of persons. 另外,我认为您不需要指向人的指针数组,而是需要一个人数组。 Your struct is correct, but I would implement your function like that: 您的结构是正确的,但我会像这样实现您的功能:

void addPeople(Team * team){
    team->numPeople = MAX_PEOPLE;
    team->people = malloc(sizeof(Person) * MAX_PEOPLE);
}

And do not forget to free() your team->people . 并且不要忘记free()您的team->people

But if MAX_PEOPLE is an pre processor define, then it is totally unnecessary to use memory from the heap. 但是,如果MAX_PEOPLE是一个预处理器定义,则完全不需要使用堆中的内存。 If you are not storing too many people in your struct, the stack can easily fulfill your requirements. 如果您在结构中没有存储太多人,则堆栈可以轻松满足您的要求。

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

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