Lifetime of data in memory

Hi there!

I'm not new to programming, but kind of new to C++. One of my biggest concerns, and probably most C++ devs major concern, is memory management. I'll try to make my actual problem as clear as possible. Here's what I suspect to know for sure:

a) Local variables are allocated on the stack and deallocated after the end of a block and/or a function or special statement.

b) Dynamic memory allocated on the heap has unlimited lifetime until deallocated manually.

Ok. Still, what happens to objects and primitive data if used like this (completely hypothetical code, but exactly depicting what I mean):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

#include "Object.h"
#include "TypeT.h"

void insert(Object &obj);

int main(int argc, char* argv[]{
    
  // suppose we're looking at an object allocated automatically. 
  // the object contains an internal vector of a TypeT.
  
  Object obj;


  // now let's call the function "insert" and add some other 
  // object to obj's internal vector

  insert(obj);
  
  
  // now use some print method putting some value in TypeT out
  // on the standard output stream.

  obj.print();

  return 0;
}

void insert(Object &obj){

  TypeT t_obj;
  
  obj.addElement(t_obj);
}

//suppose this is addElement's declaration/definition

void Object::addElement(TypeT &e){
  data.push_back(e); //where data is "private vector<TypeT> data";
}


I hope the code is understandable. Shouldn't the object t_obj be destroyed when insert() returns and shouldn't the referenced address
stored in the vector through the above calls be invalid then? Obviously this isn't the case since the print() will put out the values correctly. But how does C++ know which variables to deallocate and which ones to keep in such cases?

Simply put: I don't exactly know what happens to locally defined types referenced by types defined in a higher level bock. Does something like the general rule for objects in Java apply (as long as an object is somehow referenced it won't not garbage collected)?

Hopefully my question isn't too confusing.

Thank you in advance for your help!

Thomas
I'm not sure about this, but I think in your example, not t_obj is added to the vector, but a copy of that object. What I do know for sure, is that there isn't such a rule in C++. For example, if you write a function that returns a pointer to a varaible in that function, you'll get a warning when compiling it. When using this you might find that the pointer still hold the right value, but this is just because the memory isnt (yet) overwritten.
I hope the code is understandable. Shouldn't the object t_obj be destroyed when insert() returns

It is.

and shouldn't the referenced address stored in the vector through the above calls be invalid then?

No. because when it was given to the .push_back() function it's copy constructor was invoked. Creating a shallow-copy of the object that was put into the vector.

But how does C++ know which variables to deallocate and which ones to keep in such cases?

Scope.

When you are passing the object by value into the function it's copy constructor is invoked. All objects in C++ have a default copy-constructor that performs a shallow-copy.

So to step through your program.
Line 12: Obj is created. Default constructor called
Line 18: Obj is passed by reference to Insert @ Line 29
Line 31: t_obj is created. Default constructor called
Line 33: t_obj is passed by reference to addElement @ Line 38
Line 39: e is pushed into vector, copy constructor is invoked.
-->
Line 34: t_obj goes outta scope and memory is re-claimed.
Line 27: obj is goes outta scope and is cleaned. Objects in the vector are also cleaned (but not de-allocated if they were pointers to objects).

http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html
Thanks you some much for your time and answers. Really appreciate it.

Well I might have missed the point then. I'm aware of the copy constructors but I thought the vector class didn't copy objects but use pure references.

Suppose it didn't copy anything. Let addElement(), instead of passing the reference to the vector, store the address in a private member. Would cleaning of the memory still occur after insert() returned, thus making the referenced object invalid? Or would the scope of the t_obj somehow be "transfered" to the scope of obj? I hope you get where I'm mentally stuck.

Please excuse my tenacity but I like to know for sure. :)

Thomas
The actual push_back() does the copy constructor call on e.

So once insert() is complete t_obj will go outta scope. obj ends up with a copy constructed version of t_obj in it's vector.
Topic archived. No new replies allowed.