简体   繁体   中英

Using kmalloc in a device driver

In an assignment I have I have to create a device driver for a deck of cards. However I am having trouble using kmalloc on an array of structs. The array of structs the deck and of size 52. I have the following so far (obviously it's incomplete):

#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;
}

It has errors when I try and try and compile using the makefile of error: incompatible types when assigning to type 'struct card' from type 'void *' for the first and error: wrong type argument to unary exclamation mark for the second. I assume the second one will go away once the kmalloc one is fixed, but I don't know what is wrong and how to fix it.

Pay attention to your errors. You're attempting to assign a pointer type to a struct card . You seem to have wanted an array of card* .

struct card *deck[52];

Otherwise you don't need dynamic allocation at all; you already have 52 valid card objects.

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