When we de开发者_StackOverflow中文版clare the const
variable it is said in Bruce eckel book that constant folding happens that means memory is not allocated for the variable.
What happens when we declare the variable as const
?
- How is the compiler free to optimise
const
variables? - Is memory for
const
variables is always allocated, and under which circumstances it may not be?
The compiler is free to optimise const variables (and anything else) as long as you can't tell the difference from what the program does.
For example:
const int three = 3;
const int four = 4;
int f() { return three * four; }
Here the compiler is free to omit allocating storage for the variables, and will probably produce exactly the same code as if it was written return 12;
But if you took the address of three
, or bound a reference to it, or declared it as extern, then the compiler would probably be forced to allocate memory for it.
I found the following quote on a PDF attributed to Bruce Eckel:
const int bufsize = 100; You can use bufsize anyplace where the compiler must know the value at compile time. The compiler can use bufsize to perform constant folding, which means the compiler will reduce a complicated constant expression to a simple one by performing the necessary calculations at compile time. This is especially important in array definitions:
Assuming that you meant "constant folding" and not "memory folding", what he means is the following:
const int i = 10;
const int j = 20;
int k = i + j;
Memory does not have to be allocated for i
and j
if the compiler can prove that this is the only time they are used. In that case, it can replace all three lines with int k = 30;
Because the variables were marked const
, the compiler is free to assume their values never change, and it is smart enough to do the addition for you in advance rather than wait for run-time.
For what its worth, I would not worry about this too much if you're still learning the language. Its an implementation detail; you can generally count on it happening, but the end result is the same either way. Write code that makes sense, use const
as needed to prevent mistakes [not as a means to save memory], and if the compiler is able to spit out something special for you then all the better.
----------- Original answer follows, since it is a sort of related topic
Without a direct quote, my guess is he is referring to something like this:
//in file 1:
const char* string1 = "Hello world!";
//in file 2:
const char* string2 = "Hello world!";
//in file 3:
const char* string3 = "world!";
Under some circumstances, the compiler/linker is able to spot that string1
and string2
both point to the same string. So it does not have to create two separate string objects that contain the same data, and assert(string1 == string2)
would end up succeeding. An even more intelligent setup could notice that string3
is a substring of the first two, and you end up with assert(string3 == string1+6)
. In the executable's actual data, you end up with one string, even though in the code there are three of them.
In C++, it doesn't really matter that they are const
, because you can't assign a non-const char*
to a string literal. But in general, that substitution would not be valid if any of the three strings were allowed to modify the memory they point to. The user probably doesn't intend for *string1 = '5'
to change *string2
, so the three strings can not be condensed in that case.
" means memory is not allocated for the variable .. "
You are mistaken. If it is a variable( be it either const or non const), it should reside in some part of memory.
When you declare a variable as const
means, the variable cannot be modified in it's life time.
const int num = 10 ;
num = 20 ; // Error : num cannot be modified. It is just a read only variable.
int var = 10 ;
var = 20 ; // var is a modifiable integer variable.
精彩评论