最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

C: Memory Allocation on Stack for Nested Structures - Stack Overflow

programmeradmin2浏览0评论

Suppose I define a structure type as follows:

typedef struct Child {
    int a;
    char b[10];
} Child;

Because the compiler knows the space requirements for all of Child's members, I don't need to use malloc.

Child child; /* Allocates space on the stack for child and its members. */

Next, I define a structure type Parent that includes Child.

typedef struct Parent {
    int c;
    char d[10];
    Child child;
} Parent;

When I declare a variable of type Parent, is enough stack memory allocated for members of both the parent and the child structures?

Parent parent; /* Allocates space for all members of parent, including the nested child? */
parent.child.a = 5; /* Is this assignment guaranteed to work? */

(This is an elaboration of the question Nested Structures memory allocation.)

Suppose I define a structure type as follows:

typedef struct Child {
    int a;
    char b[10];
} Child;

Because the compiler knows the space requirements for all of Child's members, I don't need to use malloc.

Child child; /* Allocates space on the stack for child and its members. */

Next, I define a structure type Parent that includes Child.

typedef struct Parent {
    int c;
    char d[10];
    Child child;
} Parent;

When I declare a variable of type Parent, is enough stack memory allocated for members of both the parent and the child structures?

Parent parent; /* Allocates space for all members of parent, including the nested child? */
parent.child.a = 5; /* Is this assignment guaranteed to work? */

(This is an elaboration of the question Nested Structures memory allocation.)

Share Improve this question asked Jan 29 at 17:36 9-Pin9-Pin 4502 silver badges11 bronze badges 5
  • 1 Yes; if the compiler knows the size of each element of both structures then it will allocate the correct amount of space. The only issue that you may need to consider in future is if any element is a pointer, in which case you may need to use malloc for the item being pointed to. – OldBoy Commented Jan 29 at 17:40
  • 2 After you define the Child type-alias, it's like any other native type like int. I assume you don't have the same question for the c or d members? There's really no difference between them and child. – Some programmer dude Commented Jan 29 at 17:57
  • How could it possibly work if it didn't allocate memory for both? What's your imagined model of how structure nesting works? – Barmar Commented Jan 29 at 18:43
  • Do note that the memory is reclaimed when the function parent is defined in returns. So X *NewX() { X x; return &x; } is not valid in C. – mevets Commented Jan 29 at 21:02
  • The Child member should be placed as first member of the struct, or otherwise you won't be able to implement polymorphism. See How do you implement polymorphism in C? – Lundin Commented Jan 30 at 7:58
Add a comment  | 

1 Answer 1

Reset to default 0

As the Parent struct contains the whole Child struct, when you define an object having the Parent type, it will have space for all of its members, including child.

发布评论

评论列表(0)

  1. 暂无评论