简体   繁体   English

声明要在c中构造的内存

[英]declare memory to struct in c

I have a struct abc in one file 我在一个文件中有一个abc结构

struct abc {
    some variaables
    and functions
}

I am using this struct in other file as follows : struct abc *t = kmalloc(sizeof(struct abc)); 我在其他文件中使用此结构,如下所示: struct abc *t = kmalloc(sizeof(struct abc)); kmalloc is equivalent to malloc kmalloc等效于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. 忘记了由于任何原因而使用kmalloc而不是malloc的事实,当在当前处理文件中您不知道abc struct的大小时,就不能使用sizeof(struct abc)。 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. 在头文件中声明abc结构,然后将其包含在当前文件中,或者在当前文件中声明/定义该结构。编译器需要知道要为其分配空间的对象的大小,声明是不够的。

1, 2, 4 and 5 errors are caused by missing ; 1、2、4和5错误是由丢失引起的; 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. 3错误是由于缺少include/linux/slab.h文件的include/linux/slab.h引起的。 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" #include < linux/slab.h> #请删除“ linux”之前的空格

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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