gpt4 book ai didi

c - 为什么我们要为一个int数据的链表分配内存?

转载 作者:行者123 更新时间:2023-12-04 12:07:42 24 4
gpt4 key购买 nike

我读过这个tutorial我想知道为什么要为 root 分配内存:

struct node {
int x;
struct node *next;
};
struct node *root;
root = malloc( sizeof(struct node) );

我会说 struct node *root; 保证内存加载到堆栈中不是吗?这里没有未知的大小。此外,我们在同一帧中使用该节点。

最佳答案

I would say that struct node *root; guarantees that memory is loaded in the stack no? There is no unknown size here. Moreover, we use this node in the same frame.

不完全是。 struct node *root; 保证有内存来保存指向 struct node指针,但没有内存来保存节点本身。 malloc() 正在为节点分配足够的空间,并将该节点的地址放入 root

关于c - 为什么我们要为一个int数据的链表分配内存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35734152/

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