[英]What does sizeof(int[1]) mean?
I am new to the Linux kernel.我是 Linux 内核的新手。 I am reading the file ioctl.h
, there I encountered a macro _IOC_TYPECHECK(t)
, which looks like this:我正在阅读文件ioctl.h
,在那里我遇到了一个宏_IOC_TYPECHECK(t)
,它看起来像这样:
#define _IOC_TYPECHECK(t) \
((sizeof(t) == sizeof(t[1]) && \
sizeof(t) < (1 << _IOC_SIZEBITS)) ? \
sizeof(t) : __invalid_size_argument_for_IOC)
Can you explain me this code?你能解释一下这段代码吗? In this code, what does sizeof(t[1])
mean?在这段代码中, sizeof(t[1])
是什么意思?
This is used to check the validity of the third parameter to the _IOR
/ _IOW
/ _IOWR
macros, which is supposed to be a type.这用于检查_IOR
/ _IOW
/ _IOWR
宏的第三个参数的有效性,这应该是一个类型。 It checks that the parameter is actually a type (and not a variable or a number), and causes a compiler or linker error otherwise.它检查参数实际上是类型(而不是变量或数字),否则会导致编译器或链接器错误。
If t
is a type, then t[1]
is the type "an array of 1 t
".如果t
是一种类型,则t[1]
是“一个 1 t
的数组”类型。 This type has the same size as t
, and therefore sizeof(t) == sizeof(t[1])
is true.此类型与t
具有相同的大小,因此sizeof(t) == sizeof(t[1])
为真。
If t
is a number, sizeof(t)
will fail to compile.如果t
是一个数字,则sizeof(t)
将无法编译。
If t
is a simple (non-array) variable, then t[1]
will cause a compiler error.如果t
是一个简单的(非数组)变量,那么t[1]
将导致编译器错误。
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).如果t
是数组变量,则sizeof(t) == sizeof(t[1])
将为 false,并且会导致链接器错误(因为__invalid_size_argument_for_IOC
未定义)。
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.表达式sizeof(t) < (1 << _IOC_SIZEBITS)
检查类型t
的大小是否不超过ioctl
允许的最大值,否则会导致相同的链接器错误。
There are still some invalid cases which will not be caught by this macro - for example, when t
is a pointer to a pointer.仍然有一些无效的情况不会被这个宏捕获 - 例如,当t
是一个指向指针的指针时。
It means the same as all other uses of sizeof
.这与sizeof
所有其他用途相同。 It computes the size of the expression.它计算表达式的大小。
In this particular case, I suspect that the check is intended to ensure some property of t
(which should be a type name, not a variable) which I don't know from the context ... Perhaps that it's possible to treat it as a pointer (needed for the array indexing) which would rule out some types.在这种特殊情况下,我怀疑该检查旨在确保t
某些属性(应该是类型名称,而不是变量),而我从上下文中不知道这些属性......也许可以将其视为将排除某些类型的指针(数组索引所需)。 The comment next to the macro says /* provoke compile error for invalid uses of size argument */
which seems to support this theory.宏旁边的注释说/* provoke compile error for invalid uses of size argument */
这似乎支持这个理论。
Note that sizeof
is an operator, not a function.请注意sizeof
是一个运算符,而不是一个函数。 The parenthesis are not needed, except when you want to compute the size of a type directly, and then they're part of the expression (it's a cast expression).不需要括号,除非您想直接计算类型的大小,然后它们是表达式的一部分(它是一个强制转换表达式)。 So this could be written sizeof t == sizeof t[1] && ...
, or maybe (sizeof t == sizeof t[1])
for clarity.因此,为了清楚起见,可以将其写为sizeof t == sizeof t[1] && ...
,或者(sizeof t == sizeof t[1])
。
This is a very good style to use, since it "locks" the size being computed to the proper array, instead of repeating the type of t
.这是一种非常好的使用方式,因为它将计算的大小“锁定”到正确的数组,而不是重复t
的类型。 So, if the type were to change, the expression would automatically adapt and still compute the right thing.因此,如果类型发生变化,表达式会自动适应并仍然计算正确的值。
Many C programmers seem to prefer having parenthesis around the argument to sizeof
in all cases, for some reason.出于某种原因,许多 C 程序员似乎更喜欢在所有情况下为sizeof
的参数加上括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.