简体   繁体   中英

declare memory to struct in c

I have a struct abc in one file

struct abc {
    some variaables
    and functions
}

I am using this struct in other file as follows : struct abc *t = kmalloc(sizeof(struct abc)); kmalloc is equivalent to malloc

then following errors occur:

expected '=', ',', ';', 'asm' or '__attribute__' before 'struct'
error: variable 't' has initializer but incomplete type
warning: implicit declaration of function 'kmalloc'
invalid application of 'sizeof' to incomplete type 'struct trapframe'
storage size of 't' isn't known

where am I going wrong?

Forgetting about the fact that you are using kmalloc instead of malloc for whatever reason, you can't use sizeof(struct abc) when in the current processing file you don't know the size of abc struct. Either declare abc struct in a header file, and then include that in your current file, or declare/define the struct in your current file... The compiler needs to know the size of the object you want to allocate space for, a forward declaration is not enough.

1, 2, 4 and 5 errors are caused by missing ; at the end of your struct declaration. It must be:

struct abc { some variaables and functions };

3 error is caused by missing the including of include/linux/slab.h file. You have to add the below file at the head of your source code file:

#include < linux/slab.h> # please remove the space before "linux"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM