开发者

Where are const objects stored

I do understand that functions should not return references to automatic variables. However I just wanted to understand where constant objects are stored i.e if it is stored in the memory section along with static global variables .

Here is the code on Visual studio 8 . It looks like const objects are stored as auto variables. Am i assuming things right or is it implementation specific or does it depend on whether the constructor is trivial ?

Would be really great if someone could explain why each of these cases behave the way they do.

//here i'm intentionally returning a ptr to local const ptr hope the syntax is right

const char* const* get_const_char_ptr() {
    const char * const ptr = "downontheupside";
    return &ptr;
}

const i开发者_如何学Pythonnt& get_const_int() {        
    const int magic_number = 20;
    return magic_number;
}

const string& get_const_string() {       
    const string str("superunknown");
    return str;
}

const string* get_const_string_ptr() {
    const string str("louderthanlove");
    return &str;
}

int main() {
    //case1
    const int &i = get_const_int();
    cout<<"case1:"<<i<<endl;

    //case 2
    const char * const* c =get_const_char_ptr();
    cout<<"case2:"<<*c<<endl;

    //case3
    const string &str = get_const_string();
    //this crashes
    //cout<<"case3:"<<str<<endl;

    return 1;
}


const does not change where things are stored it's a keyword to tell the compiler to prevent variables or functions from modifying things. Example:

std::string myNormalStr("Hello");
const std::string myConstStr("Don't Change Me");

myNormalStr = myConstStr; // this is ok
myConstStr = myNormalStr; // this will give you a compile error

That's a super simplistic example but the same thing applies to const objects which are passed into functions, returned from functions, or if the function itself is const.

Here's a great article by Herb Sutter on all the correct ways to use the const keyword.

Edit:

Currently there is almost no reason to use the auto keyword as everything is implicitly auto within it's scope. This keyword is a storage class specifier for an automatic variable.

However the auto keyword is changing as part of the new C++ standard in progress but is supported already by Visual Studio 2010 and some other compilers in it's new glorious form. It can be used like so in C++0x:

std::vector<int> numbers;
for (std::vector<int>::const_iterator itr(numbers.begin());
    itr != numbers.end(); ++itr)
{
        // do something with each iterated element
}

// compiler auto deduces from rvalue
// and determines that you want a
// std::vector<int>::const_iterator type
for (auto itr = numbers.cbegin();
        itr != numbers.cend(); ++itr)
{
        // do something with each iterated element
}


Constant objects allocated within a function are just like any other automatic variable; they just have const types. Global (and class-static) variables are slightly different: some constants can be placed in read-only parts of the executable file and then just copied into memory. That is used for things like string and integer constants; I do not believe it is used for anything with a nontrivial constructor.


Absolutely everything about where something is stored is implementation specific. Never forget that. With that caveat, here are some typical rules.

Automatic variables are either stored on the stack or in a register. Doesn't matter if they're const or not.

Static variables are stored in program memory. There may be multiple blocks of program memory, some read-only and some not. Declaring a variable const may affect which block something is stored in.

Variables allocated with new will be on the heap. Doesn't matter if it's const or not.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜