malloc是结构体指针和结构体数组的数组

时间:2021-05-10 19:55:05

What's the difference between

有什么区别

struct mystruct *ptr = (struct test *)malloc(n*sizeof(struct test));

and

struct mystruct **ptr = (struct test *)malloc(n*sizeof(struct test *));

They both work fine, I'm just curious about the actual difference between the two. Does the first one allocate an array of structs, whereas the second one an array of struct pointers? The other way around? Also, which one has a smaller memory footprint?

他们都很好,我只是好奇两者之间的实际差别。第一个分配结构体数组,而第二个分配结构体指针数组?反过来呢?另外,哪个内存占用更少?

3 个解决方案

#1


18  

The first allocates an array of struct, and the other allocates an array of pointers to struct. In the first case, you can write to fields by assigning ptr[0].field1 = value; right away, while in the second case you must allocate the struct itself before doing the actual writing.

第一个分配一个struct数组,另一个分配给struct的指针数组。在第一种情况下,您可以通过分配ptr[0]来写入字段。field1 =价值;立即,而在第二种情况下,您必须在执行实际写入之前分配结构体本身。

It is OK to drop the cast of malloc result in C, so you could write

可以将malloc结果的类型转换为C,这样您就可以编写了

struct mystruct **ptr = malloc(n*sizeof(struct test *));
for (int i = 0; i != n ; i++) {
    ptr[i] = malloc(sizeof(struct test));
}
ptr[0]->field1 = value;
...
// Do not forget to free the memory when you are done:
for (int i = 0; i != n ; i++) {
    free(ptr[i]);
}
free(ptr);

#2


2  

I'm just curious about the actual difference between the two

我只是好奇两者之间的实际区别

The function malloc doesn't deal in structures or pointers. It only understands bytes. So the first allocates enough bytes for n struct test objects, which the second allocates enough space for n struct test * objects.

函数malloc不处理结构或指针。它只知道字节。第一个为n个struct test对象分配足够的字节,第二个为n个struct test *对象分配足够的空间。

They both work fine

他们都工作良好

The way things look, the 2 would be used for wildly different things. For example, in the second case, you'd have to allocate memory for each ptr[i] element.

事情看起来是这样的,2可以用在不同的地方。例如,在第二种情况下,您必须为每个ptr[i]元素分配内存。

Also, which one has a smaller memory footprint

而且,哪个内存占用更少

You can answer that yourself if you print sizeof(struct test) and sizeof(struct test *). But again, they're different things, with different purposes. Which has a smaller footprint, a tractor or a beetle ?

如果您打印sizeof(struct test)和sizeof(struct test *),您可以自己回答。但是,它们是不同的东西,有不同的目的。哪个有更小的足迹,拖拉机还是甲虫?

#3


1  

The first allocates an array of structs. The second allocates an array of pointers to structs (no memory for the structs themselves). So the second is smaller unless of course your struct is also very small like a pointer.

第一个分配一个结构体数组。第二个是为结构体分配指针数组(结构体本身没有内存)。所以第二个更小,除非你的结构也像指针一样很小。

#1


18  

The first allocates an array of struct, and the other allocates an array of pointers to struct. In the first case, you can write to fields by assigning ptr[0].field1 = value; right away, while in the second case you must allocate the struct itself before doing the actual writing.

第一个分配一个struct数组,另一个分配给struct的指针数组。在第一种情况下,您可以通过分配ptr[0]来写入字段。field1 =价值;立即,而在第二种情况下,您必须在执行实际写入之前分配结构体本身。

It is OK to drop the cast of malloc result in C, so you could write

可以将malloc结果的类型转换为C,这样您就可以编写了

struct mystruct **ptr = malloc(n*sizeof(struct test *));
for (int i = 0; i != n ; i++) {
    ptr[i] = malloc(sizeof(struct test));
}
ptr[0]->field1 = value;
...
// Do not forget to free the memory when you are done:
for (int i = 0; i != n ; i++) {
    free(ptr[i]);
}
free(ptr);

#2


2  

I'm just curious about the actual difference between the two

我只是好奇两者之间的实际区别

The function malloc doesn't deal in structures or pointers. It only understands bytes. So the first allocates enough bytes for n struct test objects, which the second allocates enough space for n struct test * objects.

函数malloc不处理结构或指针。它只知道字节。第一个为n个struct test对象分配足够的字节,第二个为n个struct test *对象分配足够的空间。

They both work fine

他们都工作良好

The way things look, the 2 would be used for wildly different things. For example, in the second case, you'd have to allocate memory for each ptr[i] element.

事情看起来是这样的,2可以用在不同的地方。例如,在第二种情况下,您必须为每个ptr[i]元素分配内存。

Also, which one has a smaller memory footprint

而且,哪个内存占用更少

You can answer that yourself if you print sizeof(struct test) and sizeof(struct test *). But again, they're different things, with different purposes. Which has a smaller footprint, a tractor or a beetle ?

如果您打印sizeof(struct test)和sizeof(struct test *),您可以自己回答。但是,它们是不同的东西,有不同的目的。哪个有更小的足迹,拖拉机还是甲虫?

#3


1  

The first allocates an array of structs. The second allocates an array of pointers to structs (no memory for the structs themselves). So the second is smaller unless of course your struct is also very small like a pointer.

第一个分配一个结构体数组。第二个是为结构体分配指针数组(结构体本身没有内存)。所以第二个更小,除非你的结构也像指针一样很小。