我在为 Linux 内核空间分配内存时遇到问题。我使用以下两个结构创建了一个链表:
struct Node{
char *ptr;
struct Node *next;
};
struct List{
struct Node *head;
struct Node *tail;
};
现在当我尝试分配一个列表结构时[编辑以反射(reflect)正确的代码]:
struct List *ll = kmalloc(sizeof(struct List), GFP_KERNEL)
我得到:
error: Initializer element is not constant
我在这里做错了什么?我想在我的列表结构中添加指向节点的指针,所以我会通过以下方式添加它们:
struct Node n* = kmalloc(sizeof(Node));
n -> ptr = "Blah";
n -> next = NULL;
ll -> head = n;
最佳答案
不是
struct List ll*;
但是
struct List *ll;
你在你的类型定义中是正确的,但在 kmalloc
的两行中都是错误的。
关于c - Linux 内核空间中的动态内存分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19625440/