linux kernel - Pointer returning NULL from extern structure in C -
i have defined next global construction in .h file:
file1.h
struct kobject_saved { struct kobject* kobject; } extern struct kobject_saved *ksaved;
file1.c
#include <file1.h> struct kobject_saved *ksaved = kmalloc(sizeof(struct kobject_saved), gfp_kernel); ksaved->kobject = some_kobject; // some_kobject initialized pointer kobj
file2.c
#include <file1.h> struct kobject *ko = ksaved->kobject;
here, getting value of kobject null. though phone call file2.c made after initialization happens @ file1.c
can please help me access kobject without losing data? perchance point out going wrong?
many thanks,
update: exact code some_object. getting right address "ko" though..
void save_my_kobject(struct kobject *ko) { ko_saved = kmalloc(sizeof(struct kobject_saved), gfp_kernel); *ko_saved = (struct kobject_saved) { .kobj = ko }; }
some elements of source code missing understand want do, illustration :
struct *ko = ksaved->kobject;
can't right. guess meant :
struct kobject* ko = ksaved->kobject;
you setting global variable, modified 2 source files. not recommended practice (difficult maintain).
anyway, if that's want do, have define global variable in 1 file, , reference sec one, did. extern
statement improve remained localized within file2.c
.
we don't have exact sequence, don't know how code behaves. apparently doesn't crash, guess malloc file1.c
has worked correctly. means file1.c
has been run intended. , means problem statement :
ksaved->kobject = some_kobject; // some_kobject initialized pointer kobj
might wrong : maybe some_kobject == null
;
c linux-kernel
No comments:
Post a Comment