0

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;
6
  • I'm pretty sure that Linux kernel headers have some macros for linked lists.... See e.g. here Commented Oct 28, 2013 at 5:58
  • Thanks, I'll check that out if this doesn't work. I saw this earlier but thought it would be easier to code it myself. C scares me! Commented Oct 28, 2013 at 17:27
  • If you wish to later submit your code into the kernel, you should use right now the habits and coding rules of the kernel.... and if you are scared by C you should start by coding user-level applications, not kernel code. Commented Oct 28, 2013 at 17:34
  • I don't wish to submit it into the Kernel. I wish I could but this is a requirement for a class. Commented Oct 28, 2013 at 17:40
  • Then I hope your teacher requires you to follow kernel habits. So you need to understand how to use the existing macros for lists. Commented Oct 28, 2013 at 17:49

2 Answers 2

2

Not

struct List ll*;

but

struct List *ll;

You got this right in your type definitions, but wrong in both lines with kmalloc.

Sign up to request clarification or add additional context in comments.

1 Comment

My mistake I had struct List *ll; in my code, I must have copied it over wrong. This did not fix my problem, still getting the error.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.