-1

I have a C++ class Foo that has a private member variable bar that is only used once by Foo's constructor and is then unused afterwards for the lifetime of a Foo object, i.e. no other method of Foo uses bar. Assume that bar is a rather large object. Assume also that there is a reason why bar needs to be a member of Foo even though it is used only in its constructor.

My question: Will bar exist in memory for the lifetime of a Foo object, or will the compiler free up the memory bar occupies after construction of the Foo object?

Note: There is this similar question on entirely unused member variables, but the answer for member variables that are used at least once may or may not be different, so I deemed it worth a separate question.

Alex
  • 3,316
  • 4
  • 26
  • 52
  • 1
    The size of an object will _never_ change. It's fixed at compile time. – Lukas-T Jan 29 '21 at 11:27
  • I think anything that uses sizeof operator (so `std::vector` for example) would block such optimization. So the main situation when such optimization could be performed would be if the required members would fit the registers and compiler could scrap the rest. But those are just random guesses, the only way to know for sure is to check generated assembly - compiler could decide that such optimization is not worth it, it could be simply not implemented to handle such cases, etc. – Yksisarvinen Jan 29 '21 at 11:37

2 Answers2

1

No. The compiler will not free it unless the container object (foo) is destroyed. Remember that classes and structs are derived types that means when we allocate memory for the class/struct objects, we allocate memory for the underlying types/objects. So if the class object foo is allocated in the memory that means the member variables/objects (bar object) allocated.

If you want to know more about classes and structs, you can go through my video tutorial: https://youtu.be/m696gT83f_o

Ravi Prakash
  • 313
  • 1
  • 8
1

My question: Will bar exist in memory for the lifetime of a Foo object,

Yes.

or will the compiler free up the memory bar occupies after construction of the Foo object?

No.

If bar uses dynamic memory allocation and supports move semantics (like a vector or string etc), you could however free the resources it's allocated.

Foo::Foo() {
    // use bar
    // .
    // .
    // .
    decltype(bar) dummy = std::move(bar);
}

Here the dynamically allocated resources are moved out from bar into dummy. dummy then goes out of scope and destroys them.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 1
    You can also just create it in the constructor as you see fit. It will go out of scope and no extra memory is consumed. Having it a member is the problem here, as I understand it. –  Jan 29 '21 at 11:42
  • 2
    @user32434999 I'm working with this assumtion "_Assume also that there is a reason why **`bar` needs to be a member of `Foo`** even though it is used only in its constructor._" however strange it seems :-) – Ted Lyngmo Jan 29 '21 at 11:43
  • 1
    Agreed. Move may conflict with that mysterious reason, right? –  Jan 29 '21 at 11:45
  • @user32434999 Yes - if something other than `Foo` actually uses it. – Ted Lyngmo Jan 29 '21 at 11:46
  • Well, if not is there another possibility, as foo is certainly not using it? Sorry, maybe a stupid question, just don't quite get it ;) –  Jan 29 '21 at 11:47
  • Mmmh, I guess the constructor itself could make use of the size of Foo ;) Never mind, its getting to specific for me :D –  Jan 29 '21 at 11:49
  • 1
    The `Foo` constructor could be calling other objects and provide a pointer to `bar` - so that they use that resource without going through member functions in `Foo`. It may be poor design, but it's still a possible design :-) – Ted Lyngmo Jan 29 '21 at 11:49
  • 1
    ;) Thanks. Comments helped me at least. +1 from my side –  Jan 29 '21 at 11:50