[英]Using kmalloc in a device driver
在一項作業中,我必須為一副紙牌創建一個設備驅動程序。 但是我在結構數組上使用kmalloc遇到麻煩。 結構數組的甲板和大小為52。到目前為止,我有以下內容(顯然它是不完整的):
#include <linux/slab.h> // kmalloc()
#include <linux/fs.h> // everything
#include <linux/errno.h> // error codes
#include <linux/types.h> // ssize_t
#include <linux/fcntl.h> // O_ACCMODE
#include <linux/miscdevice.h>
#include <asm/uaccess.h> // copy_to_user copy_from_user
MODULE_LICENSE("GPL");
struct card{
char num;
char suit;
};
struct card deck[52];
static int __init_deck(void){
int i;
int returnValue;
for(i = 0; i < 52; i++){
deck[i] = kmalloc(sizeof(struct card), GFP_KERNEL); // error here
if(!deck[i]){ // error here
printk(KERN_ERR "Unable to allocate memory.");
}
}
return returnValue;
}
當我嘗試使用error: incompatible types when assigning to type 'struct card' from type 'void *'
的makefile進行編譯時,它會出現error: incompatible types when assigning to type 'struct card' from type 'void *'
第一個error: incompatible types when assigning to type 'struct card' from type 'void *'
,第二個則返回error: wrong type argument to unary exclamation mark
。 我認為,一旦kmalloc固定了,第二個就會消失,但是我不知道哪里出了問題以及如何解決。
注意您的錯誤。 您正在嘗試將指針類型分配給struct card
。 您似乎想要一張card*
。
struct card *deck[52];
否則,您根本不需要動態分配。 您已經有52
有效的card
對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.