Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have a query regarding const data that are stored. e.g const inside functions.

How is it internally implemented?

How is it made non-writable but can be initialized?

Also what does const_cast do internally to a pointer such that the const-ness is removed?
Posted
Updated 14-Feb-11 23:50pm
v2
Comments
Sergey Alexandrovich Kryukov 15-Feb-11 21:39pm    
Are you writing a thesis: "Use of C++ constants in Indian industry"? :-)
--SA
Yuvaraj Gogoi 16-Feb-11 1:13am    
hehehehe.... No, not at all. But I had many misconceptions with "const" keyword. I hope I didn't disturb you!!!

Thanks for your kind reply!

I already answered part of in response to your previous Question.

Q1. I have a query regarding const data that are stored. e.g const inside functions.

There is not such thing as "inside function", if it is a local constant, it can be stored in the current stack frame created when the code in inside function. Is it is static constant, it is stored where all static data is. In all cases, if compiler can determine that the value of the constant is statically known and if the optimizer decides to do so, the constant value is stored in all placed in the code where it is used; this is called immediate constant. In practice, it happens more often is there is not explicit constant, but some literal is uses immediately in the code, line in int a = 3;. In this example, 3 is immediate constant and usually used as such in assembled code. Programming in this way mean poor supportability though; immediate constants in C++ are best avoided except most common cases like 0, 1 and the like
Q2. How is it internally implemented?

Typically, constants are implemented like variables (excluding immediate constants); so const makes compile-time difference only — compile time error is reported at the attempt to make assignment. It is theoretically possible to put constants (if they are non-stack and not immediate) in a special read-only section of memory (page or segment), but, to best of my knowledge, this is not commonly used.
Q3. How is it made non-writable but can be initialized?

As I already explained in me Answer to your previous question, C++ makes conceptual difference between initialization and assignment. For constant, it is only initialized but never assigned. Remember, in C++ (in contrast to some other languages) a constant can be assigned to an expression with statically unknown result. It means, such constant can not be optimized out into immediate constant. In this case, initialization is implemented like assignment, but conceptually it is not assignment. You can think of this feature like a special enforcement of once-a-lifetime assignment.
Q4. Also what does const_cast do internally to a pointer such that the const-ness is removed?

That's pretty much what really happens. This is just a compile-time feature, as I explained in Q.2.
—SA
 
Share this answer
 
Comments
Yuvaraj Gogoi 16-Feb-11 1:23am    
Thanks!!!
Sergey Alexandrovich Kryukov 16-Feb-11 19:14pm    
You're welcome!
Thanks for accepting my Answer.
Good luck, call again.
--SA
Harrison H 16-Feb-11 18:50pm    
Wow man. Take my 5
Sergey Alexandrovich Kryukov 16-Feb-11 19:14pm    
Thanks man :-)
--SA
Constant data isn't really stored inside the function itself. There is a reference to the .data segment that holds the actual value. So it is non writable but the value is compiled into the program and doesn't need to be initialized because it is already there. Also, it is just a rule enforced by the compiler and if you would write your own assembly you could just overwrite it (assuming the page is writable). Some compilers have a "writable constants" option that makes constant values writable like any other variable. const_cast is a compiler directive that is somewhat similar and removes the const (and some other) attribute from the constant value so it can be written. With that you let the compiler know it was done on purpose and should ignore it this time.

More info:
http://en.wikipedia.org/wiki/Data_segment[^]

http://msdn.microsoft.com/en-us/library/bz6at95h%28v=vs.80%29.aspx[^]

Good luck!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-Feb-11 21:41pm    
Not always, also, you recollect segmentation from, maybe your very old experience.
--SA
Yuvaraj Gogoi 16-Feb-11 1:40am    
I also think so. I also thought that const are always stored in data segment. But it is not so now. For local const, it is stored also in stack as SA has rightly explained. Here I am talking about Visual Studio 2010 compiler and GNU compiler.
Please correct me if I am wrong.

Anyway thanks a lot for your kind effort.

Cheers,
Yuvaraj
E.F. Nijboer 16-Feb-11 4:59am    
Well, it is fine that it is stored on the stack but the only problem with that is that the stack is not part of the executable. The stack is created when starting the application and is empty. This means that the data is loaded onto the stack from the data segment. The exception here would be a for example local integer constant because it could be easily stored into an opcode itself. But I left that out because that is just a constant value and not constant data when looked at it from an assembly point of view. It also would not even be stored on the stack because it is embedded in the opcode directly. But writable constants could store the value on the global stack. Global because it would otherwise be lost when returning from the method and also because there is only one constant for all the calls to that function, otherwise it would simply be an initialized variable.
Hopefully this helps you some more and let me know if you more info or have any questions.
Sergey Alexandrovich Kryukov 16-Feb-11 10:53am    
E.F.,
Every piece of memory is not part of executable.
Static memory is not, stack memory
Both static and stack memory is either allocated by the loader and later during calls, like heap and stack.

Yes, stack memory is not allocated during call. All of it is allocated at once. Calls only use the stack; they chance position of stack pointers (pop, push) and use memory. Nothing is allocated or release during calls, ever.

Each thread has its own stack, so new stack memory is really allocated when a new thread is created.

--SA
Yuvaraj Gogoi 16-Feb-11 6:10am    
What I can get from above is that the local const datum(not static) is stored in global stack? Is this really true? Where is this global stack located on stack? I am sorry for my ignorance.

Thanks for the input!Yuvaraj
In addition to what E. F. Nijboer said about const, if you have a const variable of a builtin type (char, short, int, long, unsigned, ...) that is not a pointer or reference then there is usually no space created for the variable in the .data section.

Take for example:
const int nSize = 100;
char szString[nSize];


This is a rare scenario where you can allocate space on the stack from a variable.
This is because the compiler actually replaces occurances of nSize with 100 turning the above code into
char szString[100];


This is because it is computationally much simpler to load an integer constant directly by value than by loading a variable.
 
Share this answer
 
Comments
Yuvaraj Gogoi 16-Feb-11 1:43am    
Yes, its true for static const and extern const or const folding. For local const, there is some other behaviour.

Thanks again for your post.Cheers,Yuvaraj
Harrison H 16-Feb-11 18:56pm    
The reason is since the data is const, it isn't dynamic, therefore the compiler KNOWS the size of the memory and doesn't have to dynamically allocate it. Doesn't have to do with computational complexity.

Edit: If the memory allocated isn't dynamic, it is on the stack. Otherwise it is on the heap. This is not a rare scenario, it's just literally the same thing as saying "I have a char array of size 100".
Yuvaraj Gogoi 17-Feb-11 3:25am    
Thanks!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900