C99 VLA大小的决心和sizeof操作符 [英] C99 VLA size determination and sizeof operator

查看:332
本文介绍了C99 VLA大小的决心和sizeof操作符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的一个答案,一个<一个组成部分href=\"http://stackoverflow.com/questions/28038156/need-suggestion-for-determining-array-size-in-c-in-efficient-way/28038181#28038181\">question关于的sizeof ,其对于C99沃拉斯行为:

I wrote the following as part of an answer to a question about sizeof, and its behavior with regard to C99 VLAs:

这不会是很难刻意营造的情况下的语义 count_of 的VLA将有效地不同,但它的可能难创建一个可读易懂/维护,和有用的情况下(我没试过)。

It would not be difficult to intentionally create a case where the semantics of count_of would effectively differ for a VLA but it might be difficult to create a readable, easily understandable/maintainable, and useful case (I haven't tried to).

想在此之后,我不知道,如果这句话是真的。要创建摆在首位一个VLA,编译器必须首先确定的空间VLA需要的金额。

After thinking about this, I'm not sure if this statement is actually true. To create a VLA in the first place, the compiler must first determine the amount of space the VLA will require.

有关的sizeof ,我们知道,

如果操作数的类型是可变长度数组类型,操作数被评估;否则,操作数未评价,其结果是一个整常数。 (6.5.3.4/2)

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. (6.5.3.4/2)

虽然VLA尺寸显然是一个运行时间确定,评估后(如果有的话,包括任何副作用)规模前pression为VLA声明符:

and although VLA size is obviously a run-time determination, after evaluation (if any, including any side effects) of the size expression for the VLA declarator:

变长数组类型的每个实例的大小并不在其一生中发生改变。其中大小前pression是sizeof运算的操作数的一部分,改变大小前pression的值不会影响操作的结果,它是未指定是否大小前pression进行评价。 (6.7.5.2/2)

The size of each instance of a variable length array type does not change during its lifetime. 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. (6.7.5.2/2)

因此​​,考虑到

#define count_of(arr)  (sizeof(arr)/sizeof(arr[0]))

任何的情况下的宏像这样实际的有效的行为可以为一个VLA不同与一个数组声明,其中阵列尺寸前pression是一个常数前pression(即普通老式pre-C99固定大小的数组)?

is there any case where the actual effective behavior of a macro such as this could differ for a VLA vs. an array declaration where the array-size expression is a constant expression (i.e. a plain-old pre-C99 fixed-size array)?

推荐答案

明显的答案是,当改编是包含副作用的前pression。如果的sizeof 的参数计算,副作用发生。如果它不评估,没有副作用。

The obvious answer is when arr is an expression containing a side effect. If sizeof's argument is evaluated, the side effect takes place. If it isn't evaluated, there is no side effect.

#include <stdio.h>
#define LENGTHOF(arr) (sizeof(arr) / sizeof(*(arr)))
void f() {
  puts("f");
}
int main() {
  const int n = 4;
  int array[n];
  return LENGTHOF(*(f(), &array)) - 4;
}

这是在C99的,有效的,其中阵列是一个VLA,而在C ++中,其中 N 是一个常数前pression和阵列不是VLA。在C99,这个打印˚F。在C ++中,这不打印任何东西。

This is valid in C99, where array is a VLA, and in C++, where n is a constant expression and array is not a VLA. In C99, this prints f. In C++, this does not print anything.

这篇关于C99 VLA大小的决心和sizeof操作符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆