views:

117

answers:

2
+2  Q: 

malloc in kernel

when i try to malloc at kernel mod i get screamed by the compiler :

res=(ListNode*)malloc(sizeof(ListNode));

and the compiler is screaming :

/root/ex3/ex3mod.c:491: error: implicit declaration of function ‘malloc’

what should i do ?

+6  A: 

use kmalloc or vmalloc instead

knittl
Or `vmalloc()`. Memory allocation works differently in the kernel.
Wyzard
wyzard, thanks.
knittl
+3  A: 

You can't use libraries in the kernel. None whatsoever.

This means that ANY function you're calling in the kernel needs to be defined in the kernel. Linux does not define a malloc, hence you can't use it.

There is a memory allocator and a family of memory allocation functions. Read the kernel docs on the memory allocator for more information.

Incidentially, there are a few functions the kernel defines which are in the standard C library as well; this is for convenience.

It does, for instance, defined snprintf

MarkR