Questions to memory allocation

Hello,

I've got some more questions to memory allocation:

If got a class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class MyClass {

private:
    SomeClass someBbj;

public:
    MyClass();
};

MyClass::MyClass() :
    someObj(SomeClass())
{

}


1)
When I allocate a object of MyClass dynamically, does someObj get allocated on the heap as well?

2)
If I replace the implementation of the constructor to
1
2
3
MyClass::MyClass() {
    someObj = SomeClass();
}

does anything change?

3)
If I reasign a pointer-member within a member function of a object to a locally created object, does the pointer get invalid after the function returns? (Statically allocating the object)

4)
If I reasign a (value)-object-member within a memberfunction of a object to a locally created object, does the object get invalid after the function returns?
(Statically allocating the object)

Thanks again!
1) Yes. It's contained within the MyClass object, and the entire MyClass object is allocated on the heap.

It would make no sense at all for part of the object to be on the heap, and another part of it not to be.

2) No. You're simply assigning values to someObj which, by this point, has been created on the heap.

To be more thorough: you're creating a temporary, nameless object of SomeClass on the stack, and then assigning someObj to have the same value as the temporary. But someObj is already on the heap, because it's part of the MyClass object.

3) If, by "locally-created", you mean on the stack, then yes. Obviously, when the function returns, the local object is destroyed, so the address stored in the pointer no longer has valid data.

4) Can you explain this a bit better, please? Maybe show some code to illustrate what you're trying to say?
Last edited on
Thanks so far.

@ 4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class MyClass {

public:
    SomeClass obj;
    
    MyClass();
    changeObj();
};

MyClass::MyClass() :
    obj = SomeClass();
{ 
}

MyClass::changeObj() {
    obj = SomeClass();
}    


By calling changeObj() I locally create a object and assign it to the member variable. Question is if it gets "delete" when the function returns (see pointers) or stay in memory and everthing is fine.

5)
Is it possible that forward declaring SomeClass in the header doesn't give me a incomplete type error when including the class in the cpp file? Because thats happening to me right now and I thought that using non-pointer/reference types with forward-decl is not permitted?
Last edited on
4)
obj is an object and you can't change it to be another object. When you assign another object to obj it copies the object, it doesn't become the other object. The temporary object you get from doing SomeClass() will only stay alive on that line, but that is not a problem because it is only used on that line.

5)
You can have pointers and references to a forward declared class but as soon as you are accessing any of the members (functions or variables) you need the full class definition. Often a forward declaration is enough in the header but in the source file, where you actually use the objects, an include is needed.
I don't understand your explanation to 4)

Say SomeClass takes an int as constructor argument and saves that int:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class MyClass {

public:
    SomeClass obj;
    
    MyClass();
    changeObj();
};

MyClass::MyClass() :
    obj = SomeClass(1);
{ 
}

MyClass::changeObj() {
    obj = SomeClass(2);
}    


Is the stored int of obj after the call of changeObj 1 or 2?
On line 16: obj = SomeClass(2);

What happens is:
1. A temporary object is constructed and initialised by passing 2 to the SomeClass::SomeClass(int) constructor (or similar).
2. The copy assignment operator SomeClass& SomeClass::operator=(const SomeClass&) is called on obj passing the temporary object as argument.
3. The temporary object is destructed and the destructor SomeClass::~SomeClass() is called.

Exactly what these functions do depends on the class SomeClass and how they are defined.
So after the call obj holds a copy of the temporary constructed object. The temporary constructed object get's destructed though?
4) By calling changeObj() I locally create a object and assign it to the member variable. Question is if it gets "delete" when the function returns (see pointers) or stay in memory and everthing is fine.


You're not thinking about it precisely. The member variable is created as part of the MyClass object, which means it's on the stack.

Then, during your changeObj method, you are doing the following:

- Creating a temporary, unnamed object of type SomeClass on the stack, in exactly the same way as you did in 2)

- Calling the assignment operator on your obj member, which should set the state of the obj object (which is already created on the stack) based on the state of the temporary object - in exactly the same way as it did in 2)

The line obj = SomeClass(2); simply changes the state of obj. It doesn't somehow destroy obj and replace it with a new object.
So after the call obj holds a copy of the temporary constructed object. The temporary constructed object get's destructed though?

Correct - assuming that the assignment operator for SomeClass simply copies the values of all the data members. And if you haven't created an assignment operator for it, the compiler will create a default one that does exactly that.
Last edited on
Well I come from Java and reasigning does change the object meaning it unasigns the old object from the reference-variable and asigns a new object to it.
I'm still not entierly sure if I understand this correctly.
Last edited on
Well I come from Java and reasigning does change the object meaning it unasigns the old object from the reference-variable and asigns a new object to it.

I wouldn't know.

I'm still not entierly sure if I understand this correctly.

What is it you don't understand?
Last edited on
Lets say the class SomeClass looks like that:

1
2
3
4
5
6
7
8
9
10
11
class SomeClass {

public:
     int value;
    SomeClass(int value);

};

SomeClass::SomeClass(int value) :
    value(value)
{ }


After obj = SomeClass(2) the folloewing is true: obj.value == 2
?
Last edited on
Since you haven't written an assignment operator for SomeClass, the default one will simply do a shallow copy of all the values of the data members.

Thus, since the temporary object has value set to 2, that value will be copied to obj.value.

So, yes, obj.value == 2 will be true.
Okay I think I got it now ... C++ is much more complicated than I thought!

Thanks!
You're welcome :)
Topic archived. No new replies allowed.