简体   繁体   English

C中的结构内存分配

[英]Struct Memory Allocation in C

Does allocating a struct instance using malloc have any effect on the allocation of its members? 使用malloc分配struct实例是否会影响其成员的分配? Meaning that, is doing something like this: 意思是,正在做这样的事情:

typedef struct { int *i; } test;
int main() {
     test *t = malloc(sizeof(test));
     t->i = malloc(sizeof(int));
}

... is meaningless because i should be already on the heap, right ? ......没有意义,因为i应该已经在堆上了,对吧?

Or, the struct is just conceptual to help the programmer group two completely separate variables floating in memory? 或者,结构只是概念上的帮助程序员组两个完全独立的变量浮动在内存中? Meaning that: test *t = malloc(sizeof(test)) just allocates the memory for storing pointers to the members on the heap? 意思是: test *t = malloc(sizeof(test))只是分配内存来存储指向堆上成员的指针?

I'm quite baffled about this .. 我对这个感到非常困惑......

The i field of test is a primitive int , and malloc ing test will take care of the memory needed by it. testi字段是原始intmalloc test将处理它所需的内存。 Assigning a result of malloc to i should produce a warning, as you're implicitly converting a pointer to an int . malloc的结果分配给i应该产生警告,因为您隐式地将指针转换为int

EDIT: 编辑:
As pointed out in the comments, the question has been edited since the above answer was posted. 正如评论中指出的那样,自上述答案发布以来,该问题已被编辑。 If your struct contains a pointer, malloc ing the struct will allocate the memory to hold a pointer, but will not allocate the memory this pointer points to. 如果你的struct包含一个指针, malloc struct将分配内存来保存一个指针,但是不会分配这个指针所指向的内存。 For that, you'll need a second malloc call (eg, test->i = malloc (sizeof (int)) ). 为此,您需要第二个malloc调用(例如, test->i = malloc (sizeof (int)) )。

You would only allocate the memory for i if i was a pointer to an int. 如果我是指向int的指针,你只会为i分配内存。 See this example: 看这个例子:

#include <stdlib.h>

typedef struct
{
        int i;
} test;

typedef struct
{
        int* i; 
} testp;

int main()
{
        test* foo = malloc(sizeof(*foo));
        foo->i = 3;

        testp* bar = malloc(sizeof(*bar));
        bar->i = malloc(sizeof(*bar->i));
        *bar->i = 3;
}

When you allocate the struct, there is already space allocated for the int i . 分配结构时,已经为int i分配了空间。

When you call the first malloc, there is enough memory to hold the whole structure, including the '*i', which has enough place to hold a pointer to an integer. 当你调用第一个malloc时,有足够的内存来保存整个结构,包括'* i',它有足够的位置来保存指向整数的指针。 The field *i however remains uninitialized! 然而,领域*我仍然没有初始化! so you must not use *i before you assigned it with the second malloc that reserves memory for an integer. 因此,在为第一个为整数保留内存的malloc分配它之前,不得使用* i。

Given a struct type "test", malloc(sizeof(test)) allocates space for a complete struct, including all members and any padding between. 给定结构类型“test”, malloc(sizeof(test))为完整的结构分配空间,包括所有成员和之间的任何填充。 You are perhaps confused about the case where one or more of the members is a pointer: 您可能会对一个或多个成员是指针的情况感到困惑:

typedef struct {
    char *string;
    char array[8];
} test;

In that case, the pointer itself is a member, and space is allocated for it, but the thing pointed to is not a member, and no space is allocated for that. 在这种情况下, 指针本身是一个成员,并为其分配空间,但指向的东西不是成员,并且没有为其分配空间。

Contrast that with the case where the struct has an array member: the whole array is a member of the struct, and sufficient space is allocated in the struct for all its elements. 与struct具有数组成员的情况形成对比:整个数组是结构的成员,并且在结构中为其所有元素分配了足够的空间。

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

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