when doing like this:
const int a = 5;
I wonder if a will get 4-byte of me开发者_C百科mory just like a variable ? (in 32 bit system)
Yes it will. Although if you never take it's address then the optimizer might well remove it entirely and just replace any references to the constant with the number 5 in your case.
It depends.
const int a = 5;
Will take four bytes of memory (or however many bytes an int takes up on your system).
If you make it static:
static const int a = 5;
Then the optimizer is free to replace each instance of a
with the value of 5. The optimizer cannot do that in the first (non-static) case simply because you may refer to a
in a separate compilation unit with:
extern const int a;
It depends on the compiler.
For example:
const int a = 4;
This could be handled by the compiler allocating 4 bytes and just enforcing immutability.
If you had a constant string:
static final java.lang.String name = "Foobar";
The compiler could remove the variable and replace it with the actual string "Foobar" everywhere the variable is used. This doesn't take space from the heap but it still has to be stored somewhere in the programs data segment. Java tries to do this when it finds a quoted string that is being used in multiple places, so it only has to store one copy of it.
Either way, constants don't eliminate storage allocation. At best they can only minimize the storage needed.
It depends on your architecture but yes whether you make something const or not does not really affect its size but more its location in memory. Now, there are some compiler optimizations that may change what you think will actually happen but this is the basic idea.
There's no difference in memory consumption between int a
and const int a
.
Note though, that in C objects declared as const
don't form constant expressions (as opposed to C++) and have external linkage by default (as opposed to C++, again). All this means that in C a constant object is pretty much the same thing as a non-constant object, just non-modifiable.
Also, it means that in C a constant object has very little chance to get "removed", as other answers claim it will. If you really want it to make it "removable" in C, you have to declare it as static
explicitly. But even that won't make a const int
object to form constant expressions, i.e. you still can't use it to designate array size in C89/90 and in C99 the resultant array is still a variable-length array (VLA).
a constant variable requires 4 bytes of memory, but if it is a value it requires 0 bytes since the assembly code will embbed the value like this
mov eax, 5
here 5 don´t comes from a variable but it is the constant 5, and it will even generate faster code since no memory call are required to retrieve the value, it is just part of the assembly code
It might take the usual amount, but if you only use it in ways that never require it to have an address, the compiler/linker may optimize it away so it doesn't occupy any memory at all.
Generally the constant will take the same space as a variable, so if int is 32bit on your architecture, a will take 32bit as well. However the compiler might also decide to directly put the constant into the code, not assining space for the constant itself at all. This will depend on where the constant is actually defined, meaning if the compiler is able to determine that there is no chance to either modifiy a (e.g. through const casts) or take the address of a.
On an embedded system, where read-only memory is separate from writable memory, this constant will take up no RAM, it will be stored only in ROM. Likewise, on a system with virtual memory, constants will get loaded into read-only memory pages and will only take up RAM once no matter how many running copies of the program there are.
精彩评论