1

I am having a hard time getting used to the way C++ handles dynamic and automatic memeory.
My question:

  • Can a pointer, that points to an automatic allocated instance, keep that instance from deallocation even after the scope of the instanciation has been left?
    In this post I read that all pointers pointing to dealloc memory are invalid.
    But is this guy talking about the behaviour after a manual dealloc or an automatic dealloc?

This is an example:

int main(){
   UiTreeRecord child = UiTreeRecord::UiTreeRecord();
   createSomeScope(child);
   //Does child still have a valid parent?
   //Or does parent point to a piece of memory that has been deallocated?
}

void createSomeScope(const UiTreeRecord& child){
   UiTreeRecord root = UiTreeRecord::UiTreeRecord();
   child.attachParent(root); 
}

void UiTreeRecord::attachParent(UiTreeRecord& newParent) {
    if(parent != nullptr) {
        detachParent();
    }
    parent = &newParent;
}
Community
  • 1
  • 1
Noel Widmer
  • 4,444
  • 9
  • 45
  • 69
  • 2
    No, when you leave a function all local variables are gone. – Bo Persson Dec 27 '15 at 21:30
  • @BoPersson So, "root" is allocated on the stack and will be cut when we leave the current scope. Damn you're right. I am still in a C# world. – Noel Widmer Dec 27 '15 at 21:32
  • If you want something like this auto managed it you'd have to use `unique_ptr` or `shared_ptr` that ease dynamic memory management and automatically delete when needed. – AliciaBytes Dec 27 '15 at 21:44
  • 1
    You have a strange way of default-constructing things, which is actually copy-construction! Don't write `T obj = T();` (or `T obj = T::T()`, which is just weird; did you think that temporary creation is a direct constructor call? It's not. All you're doing here is using the object's nested name, so it's the same as `T obj = T::T::T::T::T::T::T::T::T::T();`.). Write `T obj;`. – Lightness Races in Orbit Dec 27 '15 at 22:52
  • @LightnessRacesinOrbit So "T obj" will call the default constructor by initialization? ok - but what if there is no default constructor? Oh and yes I really thought this is a call to the ctor :) – Noel Widmer Dec 28 '15 at 00:15
  • @NoëlWidmer: Yes. And if there's no default constructor, it'll fail just as your current version would. – Lightness Races in Orbit Dec 28 '15 at 01:00

3 Answers3

1

automatic memory: When is it deallocated?

When the variable goes out of scope. Or in the case of automatic member variable, when the owning object is destroyed.

Can a pointer, that points to an automatic allocated instance, keep that instance from deallocation even after the scope of the instanciation has been left?

No, it can't. The pointer simply keeps pointing at the memory that is now invalid. Pointers and references to a destroyed object always become invalid regardless of the way the object was destroyed - be it automatic, dynamic or static deallocation.

In your example, root is deallocated at the end of createSomeScope and the pointer that you assigned in UiTreeRecord::attachParent becomes invalid.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

It is potentially dangerous to do stuff like this. Either use * or use &, but don't use the pointer of a reference as the reference might get invalidated like in your case. In your example you create an instance first, then pass it as reference to attachParent(), then convert it to a pointer but the instance will be deallocated when createSomeScope() returns, thus your pointer becomes invalid.

ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58
0
void createSomeScope(const UiTreeRecord& child){
  UiTreeRecord root = UiTreeRecord::UiTreeRecord();
  child.attachParent(root); 
}

root is invalid immediately after the function is ends and controlling the deallocation is not possible afaik. Memory might be freed at different "times" depending on code optimizations etc.

Simo Erkinheimo
  • 1,347
  • 9
  • 17