本文介绍了什么是常量的相对的#define为全局常量的主要优势是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在嵌入式编程,例如,的#define GLOBAL_CONSTANT 42 是pferred到 $ P $ const int的GLOBAL_CONSTANT = 42; ,原因如下:

In embedded programming, for example, #define GLOBAL_CONSTANT 42 is preferred to const int GLOBAL_CONSTANT = 42; for the following reasons:


  • 它并不需要在内存中的地方(通常是在非常有限的微控制器,和μC应用程序通常需要大量的全球常数)

  • 常量不仅需要在闪存的存储位置,但是编译器在程序开始产生额外的code复制。

  • it does not need place in RAM (which is usually very limited in microcontrollers, and µC applications usually need a large number of global constants)
  • const needs not only a storage place in the flash, but the compiler generates extra code at the start of the program to copy it.

反对使用所有这些优点的#define ,有什么用常量的主要优点
在非μC环境内存通常没有这么大的问题,常量,因为它可以在本地使用,但对于全局常量是有用的?或者就是答案只是我们应该永远不使用全局常量?

Against all these advantages of using #define, what are the major advantages of using const?In a non-µC environment memory is usually not such a big issue, and const is useful because it can be used locally, but what about global constants? Or is the answer just "we should never ever ever use global constants"?

编辑:

这些例子可能造成一些误会,所以我必须指出,他们在C.如果C编译器生成的完全相同的code两个,我认为这是一个错误,而不是一个最优化。

The examples might have caused some misunderstanding, so I have to state that they are in C. If the C compiler generated the exact same code for the two, I think that would be an error, not an optimization.

我只是延长了问题C ++不假思索很了解,在获得新的见解的希望,但很明显对我来说,在面向对象的环境中,人们对全局常量空间非常小,而不管他们是否是宏或consts。

I just extended the question to C++ without thinking much about it, in the hopes of getting new insights, but it was clear to me, that in an object-oriented environment there is very little space for global constants, regardless whether they are macros or consts.

推荐答案

在回答你的问题的C和C ++不同。

The answer to your question varies for C and C++.

在C, const int的GLOBAL_CONSTANT 不是的的在C,所以在C中定义一个真正的常数的主要方式是使用的#define

In C, const int GLOBAL_CONSTANT is not a constant in C, So the primary way to define a true constant in C is by using #define.

在C ++中,对使用常量的#define 的主要优点是,#定义不尊重范围,所以没有方法来创建一个类范围的命名空间。虽然const的变量可以在类的作用域。

In C++, One of the major advantage of using const over #define is that #defines don't respect scopes so there is no way to create a class scoped namespace. While const variables can be scoped in classes.

除此之外,还有像其他微妙的优点:

Apart from that there are other subtle advantages like:

期间编译错误避免怪异神奇的数字:

如果您使用的是的#define 那些由pre-处理器,precompilation时间更换所以,如果你在编译过程中收到一个错误,它将是令人困惑,因为该错误信息不会指宏的名称,但该值,它会出现突然的价值了,一会浪费大量的时间跟踪下来在code。

If you are using #define those are replaced by the pre-processor at time of precompilation So if you receive an error during compilation, it will be confusing because the error message wont refer the macro name but the value and it will appear a sudden value, and one would waste lot of time tracking it down in code.

调试方便:

此外在#2所提到的,在调试时同样的原因的#define 将提供任何帮助真的。

Also for same reasons mentioned in #2, while debugging #define would provide no help really.

这篇关于什么是常量的相对的#define为全局常量的主要优势是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 05:34