简体   繁体   English

sizeof() 用于表达式

[英]sizeof() for an expression

Can anyone explain me this code:谁能给我解释一下这段代码:

int a[]={1,2,3},b[]={6,7,8},c;

c=sizeof(!a+b);

cout<<c;

I'm getting as output: 8. But I don't understand why.我得到的输出是:8。但我不明白为什么。

a and b in this context decay to pointer types.在此上下文中的ab衰减为指针类型。

So the expression !a+b is an int* type.所以表达式!a+b是一个int*类型。 We need to go into a little more detail here : !a is actually a bool type, and that added to an int* type is an int* type (think pointer arithmetic following the implicit conversion of the bool to an int ).我们需要在这里更详细一点!a实际上是一个bool类型,并且添加到int*类型是一个int*类型(想想将bool隐式转换为int之后的指针算术)。

sizeof(int*) is 8 on your platform. sizeof(int*)在您的平台上为 8。


Note: sizeof is always evaluated at compile-time in C++.注意:在 C++ 中, sizeof总是在编译时计算。

You have to read sizeof(!a+b);你必须阅读sizeof(!a+b); , from innermost to outermost expression. ,从最里面到最外面的表达。

  1. a decays to int* . a衰减到int*
  2. b decays to int* b衰减到int*
  3. !a is of type bool , since the logical NOT operator is applied to a . !abool类型,因为逻辑 NOT 运算符应用于a
  4. !a+b is of type bool + int* --> int + int* --> int* !a+b的类型是bool + int* --> int + int* --> int*

So you basically do:所以你基本上这样做:

sizeof(int*)

which is the size of a pointer, which, in your system is equal to 8.这是指针的大小,在您的系统中等于 8。

[I added another answer to discard the upvotes I got for my now deleted answer.] [我添加了另一个答案,以放弃我为现在已删除的答案获得的赞成票。]

There are a few things that are happening here.这里正在发生一些事情。

The first is that arrays naturally decays to pointers.首先是数组自然衰减为指针。 When an array is passed, as-is, when a pointer is expected it will be equal to a pointer to the arrays first element.当按原样传递数组时,当期望指针时,它将等于指向数组第一个元素的指针。 In other words, the expression a is then equal to &a[0] .换句话说,表达式a然后等于&a[0]

The second thing to remember is that boolean values can be implicitly converted to an int with value 0 (for false ) or 1 (for true ).要记住的第二件事是布尔值可以隐式转换为值为0 (对于false )或1 (对于true )的int

The third thing to remember is that about pointer arithmetic, that integers can be added to (or subtracted from) pointers.要记住的第三件事是关于指针算术,整数可以添加到(或从)指针中减去。

The fourth thing to remember is that about operator precedence .要记住的第四件事是关于运算符优先级 The expression !a + b is equal to (!a) + b .表达式!a + b等于(!a) + b

And the last thing to remember is that addition is commutative .最后要记住的是加法是可交换的 Ie x + y is the same as y + x .x + yy + x相同。

Putting this all together, a decays to a pointer to its first element.将所有这些放在一起, a衰减为指向其第一个元素的指针。 Then that pointer is passed to the logical not operator !然后将该指针传递给逻辑非运算符! which will turn the expression !a into a bool value of false (since the decayed pointer of a is not a null pointer).这将转动表达!aboolfalse (自的腐朽指针a不是空指针)。 Then that false is converted to the integer 0 which is added to the decayed pointer of b .然后,该false被转换为整数0 ,该整数被添加到b的衰减指针。 The result is a pointer, pointing to the first element of b ( b + 0 ).结果是一个指针,指向b ( b + 0 ) 的第一个元素。

You then pass this pointer to sizeof , and you're apparently on a 64-bit system where the size of a pointer is 64 bits, ie eight bytes.然后将此指针传递给sizeof ,显然您在 64 位系统上,其中指针的大小为 64 位,即 8 个字节。

Expressions as the operand of sizeof operator are not evaluated, it determines the size by the type of its operand.不计算作为sizeof运算符操作数的表达式,它由其操作数的类型决定大小。 The expression !a+b is of type int * (after the array to pointer conversion), and sizeof(!a+b) is giving the size of int * on your platform.表达式!a+bint *类型(在数组到指针转换之后),而sizeof(!a+b)给出了您平台上int *的大小。
Note that, though array as an operand of sizeof do not decay to pointer to it's first element, but in the expression !a+b (pointer arithmetic) they will.请注意,虽然数组作为sizeof的操作数不会衰减到指向它的第一个元素的指针,但在表达式!a+b (指针算术)中它们会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sizeof(void())是一个合法的表达式吗? - Is sizeof(void()) a legal expression? sizeof(int())是一个合法的表达式吗? - Is sizeof(int()) a legal expression? sizeof(variableName or expression) 和 sizeof(decltype(variableName or expression)) 一样吗? - Is sizeof(variableName or expression) the same as sizeof(decltype(variableName or expression))? SFINAE + sizeof =检测表达式是否编译 - SFINAE + sizeof = detect if expression compiles 在sizeof(++ n)表达式中不调用Increment运算符 - Increment operator is not invoked at sizeof(++n) expression 这个sizeof表达式如何评估? 为什么这样称呼? - how is this sizeof expression evaluated? and why it is called that way? 可以将嵌套两次的sizeof作为依赖表达式吗? - Can sizeof nested twice ever be a dependent expression? 在C ++中,不评估对应用sizeof的表达式是否合法,以取消引用sizeof中的null或无效指针是合法的? - Does not evaluating the expression to which sizeof is applied make it legal to dereference a null or invalid pointer inside sizeof in C++? 内联函数等于“sizeof 表达式”,但返回有符号值 - Inline function which is equal to "sizeof expression", but returns a signed value 为什么sizeof表达式不是像2,4,8等编译时常量? - Why is the sizeof expression not a compile-time constant like 2, 4, 8, etc.?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM