tags:

views:

504

answers:

4

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

+3  A: 

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.

Rob Kennedy
I thought the same thing at first, but what's the point? And if that were true, then the linked code would never work, right? The second argument is an int literal, so its size would always be the size of an integer on that architecture.
David Citron
Were the comma operator being used, wouldn't it be necessary to enclose the pair in another set of parentheses?
veefu
Point? why do you think it has a point?
Brian Postow
@veefu, no, because it's sizeof(exp) and the expression just happens to be var,2...
Brian Postow
@veefu: no, sizeof is an operator, not a function. You don't need parenthesis to "call" sizeof, "sizeof 1" works as well as "sizeof (1)". In general it's "sizeof <expression>" and in this case the expression is "(var, 2)".
sth
+1  A: 

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.

JaredPar
So why is the linked code written as it is, then? Hmmm....
David Citron
A misnomer? What has the wrong name?
Rob Kennedy
@Rob, because I am not a master of the english language :). Switched it to a red herring
JaredPar
+1  A: 

As mentioned, the comma operator is being applied and sizeof is returning the size of an integer literal. Offhand this looks like an error on the author's part, but there could be some sinister coding happening.

sizeof expressions are not evaluated so they can be used for a number of tricky things. One example is to provide a reference for an otherwise unreferenced variable without causing the compiler to generate any code. See this article on creating a better assert macro for an example. Alexandrescu has some other examples of sizeof trickery in Modern C++ Design, if memory serves. It's possible, but not likely, that one of these non-obvious usages is intended.

Whatever the usage, if it's not commented in this situation then it's clearly not worth the trade-off in readability and should be changed.

Dan Olson
+6  A: 

On further research, I discovered that this is behavior specific to the WindRiver Diab compiler. Please see the EDIT in the question for details.

David Citron