如何动态分配一个结构而不是它的属性?
How do I dynamically allocate a struct but not it's attributes?
我是一个编程菜鸟。我想分配一个结构,但由于我的属性的大小已经定义,在这种情况下我将如何使用 malloc()?
这是我的结构:
typedef struct sc sc_t;
struct sc {
// Attributes
};
我看过几个不同的例子,但我仍然不确定这些例子是否完全适用于我。这可以通过sc_t * sc_t_new = (sc_t *)malloc(sizeof(sc_t));
来完成吗?
这是一个很好的例子:
#include <stdio.h>
#include <stdlib.h>
#define MAX_ELEMENTS 5
typedef struct {
char name[100];
} e_struct;
int main(void) {
// Allocating MAX_ELEMENTS number of elements of type 'e_struct'
e_struct *n = (e_struct *)malloc(sizeof(e_struct) * MAX_ELEMENTS);
// Some usage of *n then free the memory...
free(n);
return 0;
}
这一行:
e_struct *n = (e_struct *)malloc(sizeof(e_struct) * MAX_ELEMENTS);
将告诉编译器为 e_struct
类型的 n
指针分配 MAX_ELEMENT
字节乘以 e_struct
字节大小的内存(即 MAX_ELEMENTS * sizeof(e_struct)
:
+----------+----------+----------+----------+----------+
| ELEM1 | ELEM2 | ELEM3 | ELEM4 | ELEM5 |
+----------+----------+----------+----------+----------+
|name[100] | name[100]| name[100]| name[100]| name[100]|
+----------+----------+----------+----------+----------+
这个问题没有意义。结构是它的成员(你称之为“属性”)。这就像问如何在没有燃烧器的炉子上用没有底部的煎锅制作不含鸡蛋的煎蛋。
更多的是编程相关的,就像问如何分配一个没有数字的int
您只需分配一个结构,而该结构内部已经有它的成员。
您可以使用:
sc_t * sc_t_new = malloc(sizeof(sc_t));
(额外的 (sc_t*)
在 C 中不需要,仅在 C++ 中需要)
您还可以使用:
sc_t * sc_t_new = malloc(sizeof(*sc_t_new));
如果您以后决定将其更改为不同的结构,这有助于防止您犯错误。
For context to my nonsensical question, someone in my class asked the TA "Since the array attributes in our struct have a defined size and are not allocated on the heap, it's safe to only free the struct itself since it's the only thing allocated, correct?" and TA said that is correct.
你的同学可能对包含指针的结构感到困惑。如果您有这样的代码:
struct S {
int *a;
};
struct S *s = malloc(sizeof(struct S));
s->a = malloc(sizeof(int)*100);
那么指针变量a
是结构的成员,但是100个整数的数组不是——那是完全独立于结构的!
如果您这样做:
free(s);
它会释放结构,但不会释放数组。要释放结构和数组,您可以这样做:
free(s->a);
free(s);
注意:这是因为在这种情况下数组不是成员。只有指向数组的 指针 是成员。
我是一个编程菜鸟。我想分配一个结构,但由于我的属性的大小已经定义,在这种情况下我将如何使用 malloc()?
这是我的结构:
typedef struct sc sc_t;
struct sc {
// Attributes
};
我看过几个不同的例子,但我仍然不确定这些例子是否完全适用于我。这可以通过sc_t * sc_t_new = (sc_t *)malloc(sizeof(sc_t));
来完成吗?
这是一个很好的例子:
#include <stdio.h>
#include <stdlib.h>
#define MAX_ELEMENTS 5
typedef struct {
char name[100];
} e_struct;
int main(void) {
// Allocating MAX_ELEMENTS number of elements of type 'e_struct'
e_struct *n = (e_struct *)malloc(sizeof(e_struct) * MAX_ELEMENTS);
// Some usage of *n then free the memory...
free(n);
return 0;
}
这一行:
e_struct *n = (e_struct *)malloc(sizeof(e_struct) * MAX_ELEMENTS);
将告诉编译器为 e_struct
类型的 n
指针分配 MAX_ELEMENT
字节乘以 e_struct
字节大小的内存(即 MAX_ELEMENTS * sizeof(e_struct)
:
+----------+----------+----------+----------+----------+
| ELEM1 | ELEM2 | ELEM3 | ELEM4 | ELEM5 |
+----------+----------+----------+----------+----------+
|name[100] | name[100]| name[100]| name[100]| name[100]|
+----------+----------+----------+----------+----------+
这个问题没有意义。结构是它的成员(你称之为“属性”)。这就像问如何在没有燃烧器的炉子上用没有底部的煎锅制作不含鸡蛋的煎蛋。
更多的是编程相关的,就像问如何分配一个没有数字的int
您只需分配一个结构,而该结构内部已经有它的成员。
您可以使用:
sc_t * sc_t_new = malloc(sizeof(sc_t));
(额外的 (sc_t*)
在 C 中不需要,仅在 C++ 中需要)
您还可以使用:
sc_t * sc_t_new = malloc(sizeof(*sc_t_new));
如果您以后决定将其更改为不同的结构,这有助于防止您犯错误。
For context to my nonsensical question, someone in my class asked the TA "Since the array attributes in our struct have a defined size and are not allocated on the heap, it's safe to only free the struct itself since it's the only thing allocated, correct?" and TA said that is correct.
你的同学可能对包含指针的结构感到困惑。如果您有这样的代码:
struct S {
int *a;
};
struct S *s = malloc(sizeof(struct S));
s->a = malloc(sizeof(int)*100);
那么指针变量a
是结构的成员,但是100个整数的数组不是——那是完全独立于结构的!
如果您这样做:
free(s);
它会释放结构,但不会释放数组。要释放结构和数组,您可以这样做:
free(s->a);
free(s);
注意:这是因为在这种情况下数组不是成员。只有指向数组的 指针 是成员。