在C ++中使用变量而不是`#define`来指定数组大小是不好的做法吗? (C错误:在文件范围内可变地修改) [英] Is it bad practice to specify an array size using a variable instead of `#define` in C++? (C error: variably modified at file scope)

查看:428
本文介绍了在C ++中使用变量而不是`#define`来指定数组大小是不好的做法吗? (C错误:在文件范围内可变地修改)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C中,不允许使用变量声明数组大小,即使它是const变量也是如此.例如:这无法在C:

In C, declaring an array size using a variable, even if it is a const variable, is NOT allowed. Ex: this fails to compile in C:

#include <stdio.h>

const int SIZE = 2;
int a[SIZE];

int main()
{
    a[0] = 1;
    a[1] = 2;
    printf("%i, %i", a[0], a[1]);       
    return 0;
}

在C中运行此代码.

输出:

$gcc -o main *.c
main.c:5:5: error: variably modified ‘a’ at file scope
 int a[SIZE];
     ^

但是,在C ++中,它运行得很好.
在C ++中运行以上代码.

In C++, however, it runs just fine.
Run the above code in C++.

输出:

$g++ -o main *.cpp
$main
1, 2

要使其在C中运行,必须使用#define而不是变量.即:

To make it run in C, you must use #define instead of a variable. ie:

这在C或C ++中可以正常运行:

This runs just fine in C OR C++:

#include <stdio.h>

#define SIZE 2
// const int SIZE = 2;
int a[SIZE];

int main()
{
    a[0] = 1;
    a[1] = 2;
    printf("%i, %i", a[0], a[1]);
    return 0;
}

在C中运行此代码.

因此,在C ++中,几乎总是使用变量而不是#define来声明我的数组大小.我只是将数组大小变量设置为const,一切都很好!最近,我开始使用纯C语言进行大量微控制器编程,但是当我遇到此错误并弄清问题时,一位高级开发人员告诉我,除了#define -ed常量(否则可能很难使用)之外的任何做法都是不好的做法.编码的数字)来声明数组大小.

So, in C++ I've almost always used a variable, rather than #define, to declare my array sizes. I just make the array size variable const and it's all good! Recently I started doing a lot of microcontroller programming in pure C, however, and when I ran into this error and figured out the problem, a senior developer told me it's bad practice to use anything but #define-ed constants (or maybe hard-coded numbers) to declare array sizes.

这是真的吗?指定数组大小时,在C ++中使用const变量而不是#define是不好的做法吗?如果是这样,为什么?

Is this true? Is it bad practice in C++ to use const variables instead of #define when specifying array sizes? If so, why?

在C语言中,显然您受制于#define:您别无选择.但是在C ++中,您显然至少有2个选择,所以一个比另一个更好吗?使用一个在另一个之上是否有风险?

In C, apparently you're stuck with #define: you have no other choice. But in C++ you clearly have at least 2 choices, so is one better than the other? Is there a risk to using one over the other?

  1. 在C中文件范围内的可变修改数组
  2. static const vs #define <-这是一个可靠的问题,非常有帮助.这绝对是与我的问题有关的,但是我的问题不是重复的,因为尽管它们都是关于const vs #define的,但我的问题是一个非常特殊的情况,其中一个选项甚至无法正常使用一种语言被认为是C ++的子集.这是非常不寻常的,这使我的问题成为一个更狭窄的子集,适合另一个问题的广泛范围.因此,不能重复.
  3. https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es31-dont-use-macros-for-constants-or-functions
  4. 与"#define"与枚举"
  5. https://en.wikipedia.org/wiki/Variable-length_array#C99
  1. variably modified array at file scope in C
  2. static const vs #define <-- this is a solid question and very helpful. It is most definitely related to my question, but my question is NOT a duplicate because although they are both about const vs #define, my question is a very special case where one of the options doesn't even work in a language which is regularly considered to be a subset of C++. That's pretty unusual, and makes my question a more narrow subset which fits within the broad scope of this other question. Therefore, not a duplicate.
  3. https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es31-dont-use-macros-for-constants-or-functions
  4. "static const" vs "#define" vs "enum"
  5. https://en.wikipedia.org/wiki/Variable-length_array#C99

推荐答案

在此问题上听从Scott Meyer的建议会很好.摘自他的著作《有效的C ++》:
项目2:更喜欢const,枚举和内联而不是#defines.

It would be good to follow Scott Meyer's advice in this matter. From his book "Effective C++":
Item 2: Prefer consts, enums, and inlines to #defines.

适合您的示例的项目摘要.

Summary of the item adapted to your example.

最好将此项目称为首选编译器而不是预处理器", 因为#define可能会被视为不属于该语言的一部分 本身.这是它的问题之一.

This Item might better be called "prefer the compiler to the preprocessor," because #define may be treated as if it’s not part of the language per se. That’s one of its problems.

当您执行这样的操作时,

When you do something like this,

#define SIZE 2

符号名称SIZE可能永远不会被编译器看到;它 在获得源代码之前,预处理器可能会将其删除 编译器.结果,可能无法输入名称SIZE 进入符号表.如果您在操作过程中遇到错误,可能会造成混淆 涉及使用常量的编译,因为错误消息 可能引用2而不是SIZE.如果SIZE是 定义在您没有写的头文件中,您根本不知道该在哪里 2来自,您会浪费时间对其进行跟踪.这个问题 也可以在符号调试器中出现,因为名称再次 您进行编程的对象可能不在符号表中. 解决方案是将宏替换为常量:
const double SIZE = 2; // uppercase names are usually for macros, hence the name change
作为语言常量,SIZE肯定会被编译器和 肯定会输入到他们的符号表中.

the symbolic name SIZE may never be seen by compilers; it may be removed by the preprocessor before the source code ever gets to a compiler. As a result, the name SIZE may not get entered into the symbol table. This can be confusing if you get an error during compilation involving the use of the constant, because the error message may refer to 2, not SIZE . If SIZE were defined in a header file you didn’t write, you’d have no idea where that 2 came from, and you’d waste time tracking it down. This problem can also crop up in a symbolic debugger, because, again, the name you’re programming with may not be in the symbol table. The solution is to replace the macro with a constant:
const double SIZE = 2; // uppercase names are usually for macros, hence the name change
As a language constant, SIZE is definitely seen by compilers and is certainly entered into their symbol tables.

✦对于简单的常量,优选使用const对象或枚举而不是#defines.
✦对于类似函数的宏,最好使用内联函数而不是#defines.

✦ For simple constants, prefer const objects or enums to #defines.
✦ For function-like macros, prefer inline functions to #defines.

另请参阅项目3:尽可能使用const".有关其用法和用法例外的更多信息.

Also refer to "Item 3: Use const whenever possible." for more info on its usage and exceptions to its usage.

因此在标题中回答您的问题:
,在C ++中使用变量而不是#define来指定数组大小不是一个坏习惯.

So to answer your question in the title:
No, it is NOT a bad practice to specify an array size using a variable instead of #define in C++.

这篇关于在C ++中使用变量而不是`#define`来指定数组大小是不好的做法吗? (C错误:在文件范围内可变地修改)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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