0

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

trincot
  • 317,000
  • 35
  • 244
  • 286
Simon
  • 325
  • 1
  • 6
  • 3
    Objects can't change size in C++. This might help you: https://stackoverflow.com/questions/55478523/how-does-stdvector-support-contiguous-memory-for-custom-objects-of-unknown-siz – NathanOliver Jul 07 '21 at 20:17
  • 8
    "requires no minmal working example;" sorry but you are wrong on this, because "Now one of the members grows during runtime." is a misinterpretation of some code. We don't know what that code is unless you show it – 463035818_is_not_an_ai Jul 07 '21 at 20:18
  • If something is growing dynamically at runtime, there's probably heap allocation involved. Though that may be abstracted away via things like `string`, `vector`, `map`, etc. But basically that's how you get things that can "grow". If that seems vague then you need a more specific question/example. – TheUndeadFish Jul 07 '21 at 20:32
  • 1
    I have a Class instance whose members are from an external library, I don't even know how they internally store the members. But I checked something: The address of the member is unchanged but its memory consumption increased during runtime. So this is only possible using heap allocation, is it? – Simon Jul 07 '21 at 20:46

1 Answers1

8

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.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • "No. The address of an object will never change through its entire lifetime." Just to be sure, the address of the object and thus the address of all members don't change, correct? – Simon Jul 07 '21 at 20:58
  • 1
    @Simon Data members are objects too (unless they are references), and that applies to all objects. (References aren't objects, nor do they have an address - or at least any address that you could access within the C++ language. Also, it's quite rare to use reference members). – eerorika Jul 07 '21 at 21:13