Reputation: 27
I'm having trouble allocating memory is Linux Kernel space. I've created a linked list using the two structs below:
struct Node{
char *ptr;
struct Node *next;
};
struct List{
struct Node *head;
struct Node *tail;
};
Now when I try and allocate a list struct [Edited to reflect proper code]:
struct List *ll = kmalloc(sizeof(struct List), GFP_KERNEL)
I get:
error: Initializer element is not constant
What am I doing wrong here? I want to be add pointers to Nodes in my List struct so would I add them by:
struct Node n* = kmalloc(sizeof(Node));
n -> ptr = "Blah";
n -> next = NULL;
ll -> head = n;
Upvotes: 0
Views: 2238
Reputation: 16
The ERROR is not related to kernel programming, it is related to c programming.
error: Initializer element is not constant
Code:
struct List{
struct Node *head;
struct Node *tail;
};
struct List *ll = kmalloc(sizeof(struct List), GFP_KERNEL)
The structure object (by default) has static storage class. Initialization of Objects with Static Storage Duration must be with constant expression. Try allocating memory inside main() function.
Objects with static duration are declared either outside functions, or inside them with the keyword extern or static as part of the declaration. These can only be initialized at compile time. i.e, with constant expression
Upvotes: 0
Reputation: 283911
Not
struct List ll*;
but
struct List *ll;
You got this right in your type definitions, but wrong in both lines with kmalloc
.
Upvotes: 2