sizeof(int[1]) 是什么意思? [英] What does sizeof(int[1]) mean?

查看:35
本文介绍了sizeof(int[1]) 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Linux 内核的新手.我正在读取文件 ioctl.h,在那里我遇到了一个宏_IOC_TYPECHECK(t),如下所示:

I am new to the Linux kernel. I am reading the file ioctl.h, there I encountered a macro _IOC_TYPECHECK(t), which looks like this:

#define _IOC_TYPECHECK(t) 
        ((sizeof(t) == sizeof(t[1]) && 
          sizeof(t) < (1 << _IOC_SIZEBITS)) ? 
          sizeof(t) : __invalid_size_argument_for_IOC)

你能解释一下这段代码吗?在这段代码中,sizeof(t[1]) 是什么意思?

Can you explain me this code? In this code, what does sizeof(t[1]) mean?

推荐答案

这个用来检查_IOR/_IOW/_IOWR 宏,应该是一个类型.它检查参数实际上是类型(而不是变量或数字),否则会导致编译器或链接器错误.

This is used to check the validity of the third parameter to the _IOR/_IOW/_IOWR macros, which is supposed to be a type. It checks that the parameter is actually a type (and not a variable or a number), and causes a compiler or linker error otherwise.

  • 如果t 是一个类型,那么t[1] 就是1 个t 的数组"类型.此类型与 t 的大小相同,因此 sizeof(t) == sizeof(t[1]) 为真.

  • If t is a type, then t[1] is the type "an array of 1 t". This type has the same size as t, and therefore sizeof(t) == sizeof(t[1]) is true.

如果 t 是一个数字,sizeof(t) 将无法编译.

If t is a number, sizeof(t) will fail to compile.

如果 t 是一个简单的(非数组)变量,那么 t[1] 将导致编译器错误.

If t is a simple (non-array) variable, then t[1] will cause a compiler error.

如果 t 是一个数组变量,sizeof(t) == sizeof(t[1]) 将是假的,并且链接器错误将是导致(因为 __invalid_size_argument_for_IOC 未定义).

If t is an array variable, sizeof(t) == sizeof(t[1]) will be false, and a linker error will be caused (because __invalid_size_argument_for_IOC is not defined).

表达式 sizeof(t) <(1 << _IOC_SIZEBITS) 检查 t 类型的大小没有超过 ioctl 允许的最大值,否则会导致相同的链接器错误.

The expression sizeof(t) < (1 << _IOC_SIZEBITS) checks that the size of the type t does not exceed the maximum allowed for ioctl, and causes the same linker error otherwise.

仍然有一些无效的情况不会被这个宏捕获 - 例如,当 t 是一个指向指针的指针时.

There are still some invalid cases which will not be caught by this macro - for example, when t is a pointer to a pointer.

这篇关于sizeof(int[1]) 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆