gpt4 book ai didi

c - 带指针的静态结构数组

转载 作者:太空宇宙 更新时间:2023-11-04 08:14:16 25 4
gpt4 key购买 nike

我们有一个这样的结构数组:

struct allocation
{
size_t alloc_size_;

char* alloc_memory_;
};

static struct allocation allocations[] =
{{1024, NULL},{2048, NULL},};

稍后在 main() 中,它的成员 alloc_memory_ 使用 numa_alloc_onnode() 进行初始化。

所以问题是:alloc_memory_ 也是静态的吗?它们位于何处(堆、堆栈)?如果它们不是静态的,那么如何使它们成为静态的?

最佳答案

数组allocationsalloc_memory_成员是静态的,但指向的内存不一定是静态的。

在您的情况下,由于您在 main 中使用 numa_alloc_onnode 分配了它们,这意味着它们指向动态存储。

如果你真的想要静态存储,你可以在结构之前定义内存:

static char buffer1[1024];
static char buffer2[2048];

static struct allocation allocations[] =
{ {1024, buffer1}, {2048, buffer2} };

关于c - 带指针的静态结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36730818/

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