Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why call sizeof operator with two arguments?

Tags:

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

like image 221
David Citron Avatar asked Jun 08 '09 17:06

David Citron


People also ask

What is the purpose of sizeof () operator?

One of the most common uses for the sizeof operator is to determine the size of objects that are referred to during storage allocation, input, and output functions. Another use of sizeof is in porting code across platforms. You can use the sizeof operator to determine the size that a data type represents.

Why is sizeof () an operator and not a function?

In C language, sizeof( ) is an operator. Though it looks like a function, it is an unary operator. For example in the following program, when we pass a++ to sizeof, the expression “a++” is not evaluated. However in case of functions, parameters are first evaluated, then passed to function.

How many operand the special operator sizeof () contains?

On most modern computing platforms this is eight bits. The result of sizeof has an unsigned integer type that is usually denoted by size_t. The operator has a single operand, which is either an expression or the cast of a data type, which is a data type enclosed in parentheses.

Is sizeof overloaded?

sizeof cannot be overloaded because built-in operations, such as incrementing a pointer into an array implicitly depends on it.


1 Answers

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

like image 169
David Citron Avatar answered Oct 15 '22 06:10

David Citron