C sizeof Operator – Why Call sizeof with Two Arguments?

c++sizeof

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

Best Answer

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

Related Question