gpt4 book ai didi

c - 已分配结构中的未分配数据类型

转载 作者:行者123 更新时间:2023-12-02 05:40:21 25 4
gpt4 key购买 nike

假设我有一个这样的结构定义:

typedef struct Sample
{
int s;
}Sample;

然后我像下面这样声明一个指针变量

Sample *sample = (Sample *)malloc(sizeof(Sample));

现在我的问题是,这个“int s”放在哪里,是在栈中还是在堆中?我的意思是 'int s' 也有动态内存分配吗?

最佳答案

当您为 struct 分配空间时通过malloc , 整个 struct (即 struct 中包含的所有字段)都在堆上。

关于c - 已分配结构中的未分配数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35043456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com