建议需要在有效的方式确定使用C数组的大小 [英] Need suggestion for determining array size in C in efficient way

查看:102
本文介绍了建议需要在有效的方式确定使用C数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java中,Java脚本和其他语言具有可供知道数组长度为循环和他人的需求array.length功能。而在C编程我使用

In java, java script and others languages has array.length functions available for knowing array length for looping and others needs. And in C programming I'm using

sizeof()    

运营商通过以下步骤确定数组大小或数组长度:

operator for determining the array size or array length by following procedure:

  int array [] ={12, 3, 4, 6};

  for (int i = 0; i < (sizeof(array)/sizeof(int)); i++) {
 // do sometings
}

我想知道的是

  sizeof(array)/sizeof(int)

高效查找数组的大小?
或者,如果别人有效的方法是缴费,请建议我。

is efficient for finding array size? Or if others efficient way is avaiable, please suggest me.

推荐答案

这是非常有效的,因为它是导致编译时间常数(除C99沃拉斯,见下文)的结构。你可能想使用:

It is extremely efficient because it is a construct resulting in a compile-time constant (except for C99 VLAs, see below). You might want to use:

sizeof(array)/sizeof(array[0])

代替,这样你就可以与其他超int型使用。也许把它包在一个宏(当然,所有关于谨慎平时使用注意事项宏时适用)。

instead, so you can use it with other-than-int types. Maybe wrap it in a macro (of course, all the usual caveats about caution when using macros would apply).

在更新:我上面原来的答复,我是不是在C99,想着沃拉斯其中:

On update: for my original answer above, I wasn't thinking about VLAs in C99, where:

如果操作数的类型是可变长度数组类型,操作数被评估;否则,操作数未评价,其结果是一个整常数。 (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数组元素的计数可能仅在运行时(和评价本身可具有副作用)确定,所以要VLA它不是一个纯粹编译时构建体。在大多数格式良好的情况下,对于一个VLA的code级的行为将得到有效相同。例如:

The count of VLA array elements may only be determinable at run-time (and evaluation can itself have side-effects), so for a VLA it is not a purely compile-time construct. In most well-formed cases, the code-level behavior for a VLA will be effectively the same. For example:

#include <stdio.h>

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

int vlaSize(void)
{
  return 8;
}

int main(void)
{
   int fixed_array[9] = { 1,2,3,4,5,6,7,8,9 };
   int vla_array[vlaSize()];

   printf("Fixed array: size = %zu bytes, count = %zu elements\n", 
           sizeof(fixed_array), countof_array(fixed_array));
   printf("VLA array:   size = %zu bytes, count = %zu elements\n", 
           sizeof(vla_array), countof_array(vla_array));

   return 0;
}

结果:

$ gcc -Wall -std=c99 so-misc.c
$ ./a.out
Fixed array: size = 36 bytes, count = 9 elements
VLA array:   size = 32 bytes, count = 8 elements

在进一步的编辑,醒目的这一部分:

On further edit, striking this part:

警告:这不会是难以故意制造的情况下的语义 countof_array 的VLA将有效地不同[...]

Caveat: It would not be difficult to intentionally create a case where the semantics of countof_array would effectively differ for a VLA [...]

由于思考它更长时间后,我不知道如果这是真的。在<一问到这个问题href=\"http://stackoverflow.com/questions/28040792/c99-vla-size-determination-and-sizeof-operator\">separate问题。

because after thinking about it more, I'm not sure if this is true. Asking about this in a separate question.

在任何情况下,它仍然是非常有效的,因为摆在首位,编译器找出VLA多少空间占用创建VLA。因此,即使不是编译时间常数的VLA,仍然有可能是最有效的方式来确定数组的大小。

In any case, it is still extremely efficient, because to create the VLA in the first place the compiler has to figure out how much space the VLA will take up. So even if not a compile-time constant for a VLA, it is still the most efficient way possible to determine an array size.

这篇关于建议需要在有效的方式确定使用C数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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