运行时操作员用C [英] Run time operators in C
问题描述
什么是编译时的定义和使用C运行时操作人员?我知道的sizeof()是在C编译时运营商,但它是在C运行时操作?
What is the definition of compile time and run time operator in C ? I know sizeof() is a compile time operator in C but which are the run time operators in C ?
推荐答案
对于C;各种优化技术(例如常量传送和常量折叠)意味着每个运营商有可能在编译时(如果情况允许的话)来完成。
For C; various optimisations (e.g. constant propagation and constant folding) mean that every operator can potentially be done at compile time (if the situation permits it).
对于一个简单的例子,考虑这个功能:
For a simple example, consider this function:
int foo(int a, int b) {
return a+b;
}
这看起来像需要在运行时做加法。
This looks like the addition needs to be done at run time.
现在考虑这个code:
Now consider this code:
int x = foo(1, 2);
现在的编译器可以内联的功能,传播常数,并做加法在编译时。你最终与 INT X = 3;
(加上 foo的一个潜在的冗余副本()
函数是能够做到在运行时增加)的。
Now the compiler can "inline" the function, propagate the constants, and do the addition at compile time. You'd end up with int x = 3;
(plus a potentially redundant copy of the foo()
function which is capable of doing the addition at run time).
还有哪里的优化不能在编译时完成,但可以连接(使用LTO /链接时优化)期间完成情况;其中,运营商不是在编译时或运行时进行评估。一个简单的例子是,如果在另一个目标文件code(编译单元)做了 INT X = foo的(1,2);
There are also cases where an optimisation can't be done at compile time but can be done during linking (with LTO/Link Time Optimisation); where the operator isn't evaluated at compile time or run time. A simple example would be if code in another object file ("compilation unit") did int x = foo(1, 2);
.
另外,在一般情况下,相反也是如此 - 无保证操作者将在当它是可能的编译时进行评估;所以你不能说,运营商始终是编译时。对于一个简单的例子,考虑字符串连接(如的char * MyString的=你好,世界;
!) - 这将是法律的编译器生成code,做串联在运行时(即使它是很难想象的一个原因,编译器会想)。
Also, in general, the opposite is also true - nothing guarantees that an operator will be evaluated at compile time when it is possible; so you can't say that an operator is always "compile time". For a simple example of this, consider string concatenation (e.g. char *myString = "Hello " "World!";
) - it would be legal for the compiler to generate code that does the concatenation at run time (even though it's hard to think of a reason why a compiler would want to).
这篇关于运行时操作员用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!