4
foo(a = b+c);
//new value of a(after the call) = b+c
//but
sizeof(a = b+c);
//new value of a = old value of a

Why isn't the the result of the assignment statement reflected in the stack of the function( which contains the above code) in the latter case?

rohit-biswas
  • 815
  • 1
  • 11
  • 23
  • My [example in the dup](http://stackoverflow.com/a/21995718/1708801) shows the one case where an expression is evaluated in `sizeof`. – Shafik Yaghmour Jul 16 '14 at 15:33

2 Answers2

6

sizeof is an operator not a function. Operand of sizeof is not evaluated except when it is a variable length array.

C11: 6.5.3.4 p(2):

The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the type of the operand. The result is an integer. If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.

haccks
  • 104,019
  • 25
  • 176
  • 264
2

sizeof does not evaluate its operand.

  size_t x = sizeof(i++);        // i is not incremented

Except when variable length arrays are involved:

(C99, 6.5.3.4p2) "If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant."

  size_t y = sizeof(int [j++]);  // j is incremented;

(C99, 6.7.5.2p4) "Where a size expression is part of the operand of a sizeof operator and changing the value of the size expression would not affect the result of the operator, it is unspecified whether or not the size expression is evaluated."

 size_t z = sizeof (*(int (*)[k++]) 0);  // k may or may not be incremented
                                         // gcc increments k
ouah
  • 142,963
  • 15
  • 272
  • 331
  • if the expression was `++k`, would it be mandatory to increment k? btw.. does this expression invoke UB due to NULL dereference? – tstanisl Apr 28 '21 at 22:23