2

Can someone explain me how the memory allocation and initialization in c++ works?

File Test.h
-----------
class Test
{
    public:
        Test();

        void clearSet();

    private:
        std::set<std::string> m_SetStringMember;
        int m_initMe;
}

File Test.cpp
-------------

Test::Test():m_initMe(0)
{}

Test::clearSet()
{
    m_SetStringMember.clear(); 
}

What I do understand is:
The int member m_initMe is correctly initialized in the constructor and therefore has a valid adress in memory and a valid value.
But what happens with the m_SetStringMember?
Does it has to have a valid adress in memory?
Does it has to have a valid default value?
Set by the default constructor of std::set<std::string>()?
Or do I have to explicitly set m_SetStringMember = std::set<std::string>() in the constructor?

1
  • even if you dont initialize m_initMe it has memory(when you create the object) just that it has indeterminate value. same thing holds true for m_SetStringMemberbut its invoked through default constructor which would give it a valid state.. Commented May 2, 2013 at 9:15

3 Answers 3

4

But what happens with the m_SetStringMember? Does it has to have a valid address in memory?

Yes, but the address has nothing to do with the constructor. The constructor only initialises an object once it has been given a valid address by the compiler or by the heap allocator.

Does it has to have a valid default value?

Yes

Set by the default constructor of std::set()?

Yes

Or do I have to explicitly set m_SetStringMember = std::set() in the constructor?

If you want to be explicit do this

Test::Test() : m_initMe(0), m_SetStringMember() {}

but it the same thing would happen by default oo.

Sign up to request clarification or add additional context in comments.

1 Comment

Just to note, it isn't the compiler that gives valid addresses. That's a runtime activity.
1

m_SetStringMember is an object of a class std::set that has it's own constructor. It's correctly initialized by its constructor.

5 Comments

Because it is allocated on the stack!!! If you had a pointer, it would be garbage.
@bash.d: if it was a pointer it wouldn't be an object of class std::set.
I'm sorry? If I std::set<std::string>* m_SetStringMember; it must be allocated using new, because it would contain garbage otherwise. `
@bash.d std::set<std::string>* and std::set<std::string> are two different things. The second has type "set of string", the first has type "pointer to set of string" and it is NOT (itself) a "set of string". Your assertion is correct, but is out of scope here, since we are not talking about a pointer.
@bash.d It has nothing to do with "the stack" - you can't tell from a class definition where the members are going to be allocated. If the Test instance is dynamically allocated, then all its members are necessarily contained within it (i.e. not "on the stack").
0

One more addendum to John's correct post:

The order in which those variables will initialize is the order in which they are declared, so actually if you wanted to be explicit you should do this:

Test::Test() : m_SetStringMember(), m_initMe(0)  {}

Making sure the order in your initialization list matches the order in which you declared them is a good practice to get into, and you can even have the compiler warn you. A lot of the time your variables will end up depending on each other and thus order matters.

Furthermore, if you don't specify a default value then the classes default constructor will be used (your compiler will generate one for you if you don't create it, and assuming it's available, i.e. doesn't have any access restrictions) -- side note: check out the rule of 3 on a common policy for creating c++ class constructors. If the variable is a POD (plain old data type -- ints, floats, etc.) then it will default to 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.