Ashwyn
Ashwyn

Reputation: 669

At what point does the memory get allocated to a constant variable in c++?

As much as i know, constant class members must be initialized before the constructor runs, but since they cannot be initialized in the class body (as it is just a prototype), therefore we need to initialize it inside initializer list. My question is when does memory gets allocated to a constant variable, and what is the order of execution?

class constant
{
    const int a;
    public:
    constant(int k):a(k)
    {   
        cout<<"a is "<<a<<endl;
    }   
};

int main()
{
    constant cl(5);
    return 0;
} 

EDIT: Is it true that constant variables need to be initialized at the point where they are allocated memory?

Upvotes: 3

Views: 1228

Answers (4)

moooeeeep
moooeeeep

Reputation: 32522

If the variable is const, the compiler enforces you to not change that value after initialization. That is, you must initialize it (must in the sense of RFC2119).

You must directly initialize it:

struct constant {
  const int a;
  constant(int k) : a(k) {
    /* everything is fine here */
  }   
};

You must not leave it uninitialized:

struct constant {
  const int a;
  constant(int k) { 
    /* error: uninitialized member ‘constant::a’ with ‘const’ type ‘const int’ */
  }   
};

And you must not change it's value after construction:

struct constant {
  const int a;
  constant(int k) { 
    a = k; 
    /* error: uninitialized member ‘constant::a’ with ‘const’ type ‘const int’ */
    /* error: assignment of read-only data-member ‘constant::a’ */ 
  }
};

Upvotes: 1

Oleg Tsarev
Oleg Tsarev

Reputation: 146

Exact memory place for object members depends from object creation. If you create object by "new" it is would be a heap. If you create stack object (like on your example) it is would be a stack memory. "Constant" memory - it is memory for "constant", not for "const variables".

Other words, const memory used for literal strings, literal numbers ("text", 5), while a const modifier restrict the memory update.

Upvotes: 0

Useless
Useless

Reputation: 67743

when does memory gets allocated to a constant variable

Here, a is a data member of class constant, so it's allocated as part of constant. Whenever you create an instance of constant, there's an a already included.

Note that static members are different, but just because a isn't allowed to change after initialization, doesn't make its storage different from any other regular data member.

... is it necessary that constant variables be initialized at the point where they are allocated memory

Strictly, you have to have the memory available before you can call the constructor, so the phrase at the point where is a bit problematic (see specifically André Caron's comment about placement new).

However, allocation and construction are tied together in most normal use, and initialization of a const member must happen when the object is constructed.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258618

I think you have the wrong idea about const. Think less of it as related to implementation details (like memory), or runtime, and more there as a means to help the programmer and for the compiler.

It doesn't matter when the memory gets allocated (although it's before you construct the object, before entering the initializer list - not specified by the standard), what matters is you can only initialize the variable in the intializer list (pre C++11) or even the class definition for const integral types.

Upvotes: 3

Related Questions