Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

const that has file scope are not stored unless its address needs to calculated or is extern. It is stored in symbol tables.

My question is where const inside a function is stored in C++?
e.g
C#
class A 
{
    int m_variable;
    
   public:

    A(int var = 0):m_variable(var)
    {

    }
    void fun()
    {
         const int j;  //// Where is j stored? Is it into stack or data segment? If it is in stack, how is it made constant?
         const int k = setA(m_variable); /// Where is k stored? Is it into stack or data segment?

    }
    int setA(int i)
    {
         return i
    }
};

///client code

A m(100);
m.fun();
Posted
Updated 13-Feb-11 20:58pm
v2

1 solution

First of all, your code will not compile: you should change first constant declaration to initialize it. Consider this:

C++
class MyClass {
    int Expression(int value) { return value * 2; }
    void ConstantUser(int parameter) {
        const int immediate = 3; //as your j
        const int calculated = Expression(2); //as your k
        //...
    }
};


What you show is the local non-static constant. It is either stored on you local stack frame or as immediate constant (that is, in code), it depends on both context and implementation. For example, in the code above, the constant immediate can be done immediate but can be placed on stack if it used use elsewhere below and the optimizer "decides" to do so. As to the constant calculated, it can only be on stack if its value cannot be statically determined (during compile-time, of course). In this case the modifier const only serves a role of syntax safeguard: the object cannot be assigned to something else after initialization. In C++, initialization is conceptually different from assignment.

Note: your idea of code, data or stack segments are obsolete. Even though the segments are technically present in the CPUs of Intel architecture, in modern systems with virtual memory the user mode work with "flat" memory model based on paging. Nevertheless, the code is protected from mixing up the memory addresses allocated for different roles, but the mechanism of it is quite different; it's called Data Execution Prevention, see: http://en.wikipedia.org/wiki/Data_Execution_Prevention[^].

—SA
 
Share this answer
 
v5
Comments
Nuri Ismail 14-Feb-11 3:37am    
Great explanation SA! 5++
CPallini 14-Feb-11 3:46am    
Have my 5.
Yuvaraj Gogoi 14-Feb-11 5:54am    
Thanks for the answer?
Is Data Execution Prevention only for Windows? Or is this valid for Linux also?
Sergey Alexandrovich Kryukov 14-Feb-11 9:42am    
Hardware support depends on CPU
See http://en.wikipedia.org/wiki/NX_bit
Goodle Linux "Data Execution Prevention"
It looks like buzzword is all Windows, Linux tends to do stuff without much buzz.
--SA
Yuvaraj Gogoi 14-Feb-11 6:28am    
Is contant char * stored in stack also inside the function?

e.g. char *str = "Hello";

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