简体   繁体   中英

the use of double pointer (type **)ptr in __register_chrdev_region (linux kernel, 4.14.13, x86_64)

In the linux kernel (4.14.13, x86_64), the function [__register_chrdev_region()][1] declared a double pointer variable struct char_device_struct **cp; However, throughout the function, the value of cp is never directly used, it is always used like: *cp . I don't get the point here, why not just define a pointer like: struct char_device_struct *cp , then use the variable like cp . To be more clear, for example, if the variable has been defined like: struct char_device_struct *cp , the following piece of code from the function:

for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
    if ((*cp)->major > major ||
       ((*cp)->major == major &&
       (((*cp)->baseminor >= baseminor) ||
       ((*cp)->baseminor + (*cp)->minorct > baseminor))))
            break;

could have been written like:

for (cp = chrdevs[i]; cp; cp = (*cp)->next)
    if ((cp)->major > major ||
       ((cp)->major == major &&
       (((cp)->baseminor >= baseminor) ||
       ((cp)->baseminor + (cp)->minorct > baseminor))))
            break;

So I don't know why the pointer cp is defined as a double pointer: struct char_device_struct **cp , then it is always used by its 'dereferenced' form *cp ? [1]: https://elixir.bootlin.com/linux/v4.14.13/source/fs/char_dev.c#L100

Because several lines below there is an assignment to *cp :

cd->next = *cp;
*cp = cd;

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