Reputation: 325
I guess my question requires no minmal working example; it's possible a no-brainer and easy to describe.
Let's assume there is a class instance which stores some objects as members. Now one of the members grows during runtime. After creating the instance member1 consumed 10 bytes and member2 20 bytes. Then object1 is modificated somehow and needs now 15 bytes.
My question is if the address of (the first byte of) member1 is unchanged? Or can it potentially be possible that the first byte of member1 has now another address as before? Are member variables allocated at the heap?
Thanks for your feedback!
Best
Upvotes: 0
Views: 200
Reputation: 238351
Now one of the members grows during runtime.
This scenario is not possible in C++. The size of an object (and the size of a type) is constant at runtime.
member1 has now another address as before?
No. The address of an object will never change through its entire lifetime.
I have a Class instance whose members are from an external library, I don't even know how they internally store the members.
The type of a member variable must be complete. That means that the type must have been defined. If it is defined, then its size and the internal members are known. You have probably included the definition of the type by including a header file. You can read that header to find out the definition.
So this is only possible using heap allocation, is it?
Not necessarily. For example, there could be a pre-allocated buffer that can contain objects up to some constant limit.
But typically yes, dynamic objects use dynamic storage. Based on the observation of increasing memory use, this seems to be the case.
Upvotes: 8