Why call sizeof operator with two arguments?
I recently came across some code that looked like:
if(sizeof(var,2) == 4) { ... }
(where var
is a type)
I was quite surprised to see what appeared to be two arguments to the sizeof
operator. A quick scan of the ISO/ANSI C99 standard did not yield any secrets. I couldn't come up with any reading of the grammar that allowed a comma there.
Searching Google Code, I was able to find an example of this syntax in some PPC code.
Is this some PPC-specific syntax? What does it mean?
EDIT: It turns out that both what I was looking at--as well as the linked code--is syntax specific to the WindRiver Diab compiler:
sizeof (type, int-const):
If int-const is 0 sizeof returns the size in bytes of type.
If int-const is 1 sizeof returns the alignment of type.
If int-const is 2 sizeof returns an integer constant designating the type of type. Look up "sizeof operator" in the Diab C/C++ User's Guide for values.
Wow, they've really overloaded the meaning of the sizeof
operator.
EDIT2: Full documentation is here: http://www.vxdev.com/docs/vx55man/diab5.0ppc/c-additi.htm#3001432
On further research, I discovered that this is behavior specific to the WindRiver Diab compiler. Please see the EDIT in the question for details.
It looks to me like a simple application of the comma operator, which evaluates its first argument, throws away the result, then evaluates its second argument.
In this case, it's determining whether the literal 2
has size 4. The " var
" part is irrelevant.
Looks like a red herring. My guess is that you are accidentally using the comma operator and sizeof is being applied to the last value.
链接地址: http://www.djcxy.com/p/21144.html上一篇: 为什么sizeof(int)小于
下一篇: 为什么用两个参数调用sizeof运算符?