简体   繁体   English

文字数字是否视为常量?

[英]Are literal numbers treated as constants?

It is always better for contants such as PI to #define them or declare them const so the compiler can optimize and it becomes less error prone. 对于像PI这样的竞争对象,最好先#define它们或将它们声明为const,以便编译器可以进行优化,并且不易出错。 I was wondering however, how are literal numbers in statements treated? 但是,我想知道语句中的立即数如何处理? Ex: 例如:

float x;
const int y = 60;
x = y / 3.0f;

In this example how would 3.0f be treated? 在此示例中,将如何处理3.0f? Would it inherit the optimizations of a constant? 它会继承常量的优化吗?

What optimizations will take place depends on the compiler. 将进行哪些优化取决于编译器。 In your case, both C and C++ compilers will normally have enough information to optimize your source code into identical machine code. 在您的情况下,C和C ++编译器通常都将具有足够的信息来将源代码优化为相同的机器代码。 In other words, it doesn't really depend much on what is literal and what is constant in this code. 换句话说,它实际上并不太依赖于此代码中的字面量常量

Having said that, the meaning of the terms literal and constant are significantly different in C and C++ (and you tagged your question C and C++ at the same time). 话虽如此,在C和C ++中, 字面量常量的含义存在显着差异(并且您同时标记了问题C和C ++)。

  • In C 60 and 3.0f are constants , but y is not a constant . 在C 603.0f常数 ,但是y不是常数 You can call y a const-qualified variable if you wish, but it is not a constant in C terminology, in a sense that a single y is not a constant expression in C. 如果愿意,可以将y称为const限定变量 ,但在C术语中它不是常数 ,从某种意义上说,单个y在C中不是常数表达式

As for literals , in C language the term literal only applies to string literals (and also compound literals in C99), ie there are no literals in your code at all. 至于文字 ,在C语言中, 文字一词仅适用于字符串文字(以及C99中的复合文字 ),即,您的代码中根本没有文字。

  • In C++, 60 and 3.0.f are literals , which form constant expressions (integral and floating -point respectively). 在C ++中, 603.0.f文字 ,它们形成常量表达式 (分别为整数和浮点数)。 y is also a constant of int type, in a sense that a single y is a constant expression in C++. y也是一个恒定 int型,在一定意义上,单一y是在C ++中的常量表达式

The situation when you might notice the difference has nothing to do with optimizations, but rather with the way the languages are defined. 您可能会注意到差异的情况与优化无关,而与语言的定义方式无关。 For example, using the above y in a file-scope array type declaration is legal in C++, but not in C 例如,在文件范围数组类型声明中使用上述y在C ++中是合法的,但在C中则不合法

 typedef int int_array[y]; /* OK in C++, ERROR in C */

Since by using a #define you ask the preprocessor to do a text replacement, your code is the same as the following: 由于使用#define要求预处理器进行文本替换,因此您的代码与以下代码相同:

#define VAL 3.0f

float x;
const int y = 60;
x = y / VAL;

How the direct const value is optimized is obviously dependent on the compiler. 直接const值的优化方式显然取决于编译器。 However if you watch at the assembly code (by example the one produced by gcc) you'll notice that the compiler directly writes the binary sequence encoding the value 3.0 in the floating point standard. 但是,如果您观看汇编代码(例如,由gcc生成的汇编代码),则会注意到编译器直接在浮点标准中编写了编码值3.0的二进制序列。

On some architectures, there is a limit to the size of string literal that can be used directly. 在某些体系结构上,可以直接使用的字符串文字的大小受到限制。 When the size of the string literal is too big, the compiler will need to store the constant somewhere in read-only data memory and then load the value from memory when required. 当字符串文字的大小太大时,编译器将需要将常量存储在只读数据存储器中的某个位置,然后在需要时从内存中加载值。

If one stores the value in a constant variable, there is a good chance that the compiler will store only one value of the constant and use as appropriately. 如果将值存储在常量变量中,则编译器很有可能仅存储常量的一个值并适当使用。 However if one #defines the constants, this just makes the preprocessor place the literal value into the code and therefore there is a chance that the compiler not realize that you are using the same value and store the constant multiple times. 但是,如果使用##定义常量,则只会使预处理程序将文字值放入代码中,因此,编译器有可能无法意识到您正在使用相同的值并多次存储该常量。 It is for this reason that const variables are better than #defines. 正是由于这个原因,const变量比#defines更好。

The answer depends on whether you're using C or C++, so you need to pick one and stop pretending they're the same thing. 答案取决于您使用的是C还是C ++,因此您需要选择一个并停止假装它们是同一件事。 In C, the const keyword does not declare a constant ; 在C中, const关键字未声明常量 it declares a variable for which attempts to modify that variable invoke undefined behavior. 它声明了一个变量,尝试对其进行修改的变量将调用未定义的行为。 In C, you should never declare const int y; 在C语言中,永远不要声明const int y; unless it's static/global and intended to convey internal information from one module to another which you don't want to be fixed at the other module's compiletime (for example, const int my_library_version = 0x1001; in a shared library might be useful, although a function call to return the value would be cleaner). 除非它是静态的/全局的,并且打算将内部信息从一个模块传递到另一个模块,而您不想在另一个模块的编译时将其固定(例如, const int my_library_version = 0x1001;在共享库中可能很有用,尽管函数调用返回的值会更干净)。

As for optimizations, no C compiler I know of will optimize out the division in your example, and it's questionable whether the compiler is even allowed to. 至于优化,据我所知,没有C编译器会优化示例中的除法,是否允许编译器也存在疑问。

As for C++, you should ask a C++ expert to explain (of which I am not one). 至于C ++,您应该请C ++专家来解释(我不是谁)。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM