8

Just wondering why the increment operator is not working in the below code snippet:

 int main() { int a = 10; int b = sizeof(a++); cout<<"a: "<<a<<endl; cout<<"b: "<<b<<endl; return 0; } 

Output-

a: 10

b: 4

3
  • 1
    Sizeof is a compile-time operator, and the entire sizeof() gets replaced with its resultant value before compiling the code. See: stackoverflow.com/questions/8225776/… Commented Dec 27, 2012 at 16:31
  • What is the expected behavior for that code? Commented Dec 27, 2012 at 16:35
  • @EtiennedeMartel: I guess the asker's expectation is a=11, b=10. Commented Dec 27, 2012 at 16:58

5 Answers 5

11

sizeof does not evaluate its argument. It calculates the argument's size statically at compile-time without causing any code to be executed.

Sign up to request clarification or add additional context in comments.

4 Comments

Why am I surprised by this?
@JanDvorak: Because you don't know C++?
@sbi Yeah, that's a likely cause
So I have a doubt, if it doesnot evaluate the argument expression why it is throwing error if I declare that variable a as a constant ?
8

When the type of the expression to sizeof is not a variably modified array type, then the expression is not evaluated because the type is completely known at compile time. int has no variably modified parts.

In C++ (up to at least C++11) there are no variably modified types (at least not as in the concept of C - you can argue that new int[a++] uses a variably modified array type; but the type does not escape to any other part of the language. In particular, not to sizeof), so in C++, the expression to sizeof is never evaluated. In C, it is unspecified whether an expression is evaluated if it doesn't influence the size of a variably modified array type. For example

int main() { int a = 10; int b = sizeof(int[a++ ? 1 : 1]); cout<<"a: "<<a<<endl; cout<<"b: "<<b<<endl; return 0; } 

In C (from C99 onwards), this may output 11 for a, but it may also output 10, depending on whether the compiler is clever enough to omit evaluating a++, deducing that the sizeof int[10] is computed at compile time.


Footnote: Variably modified array types are also called VLA (variable length array) types. In short, a variably modified type is a type that is either a VLA type or a type that depends on one. For example int(*)[a++].

1 Comment

+1: in C++11, 5.3.3p1, "The sizeof operator yields the number of bytes in the object representation of its operand. The operand is either an expression, which is an unevaluated operand (Clause 5), or a parenthesized type-id." emphasis added.
2

The operand of the sizeof operator is unused, it's not evaluated. This is standard behavior.

1 Comment

But a is used both to determine sizeof(a++) and later for output.
1

sizeof is not a function in C.

Its argument is not really evaluated, only its type is and this is done at compile-time. In your code, the assignment is equivalent (in your architecture) to :

int b = 4 

1 Comment

"this is done at runtime. " -- really?
0

In an unevaluated-context, only the type matters. The same happens when calling functions:

void f(); sizeof(f()); // f not called 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.