views:

180

answers:

3

Just wondering, if I statically create an object that has a pointer as a data member and then the object goes out of scope, what happens to the pointer? Chuma

+4  A: 

The pointer gets destroyed with the rest of your object. Whatever it was pointing at isn't affected at all (unless the object's destructor does something with it).

Carl Norum
+12  A: 

Nothing happens to the pointer at all, it just ceases to exist. If it was pointing to something that needed to be freed, you just got a memory leak.

Either add code to the destructor that does the proper cleanup of the pointer, or use "smart pointers" that clean up after themselves automatically.

Edit: If you actually meant you were creating a static object, by declaring it with the static keyword inside a function, then the answer is different. A static object, once constructed by the first execution of the function that declares it, continues to live until the program ends. Its data members, including pointers, will remain valid. Subsequent calls to the function will access the same object. If the object has allocated any memory, it will remain allocated unless something explicitly deletes it.

Mark Ransom
(Since it triggered a bit of discussion under another response:) It is probably worth nothing here that while in the everyday C++ linguo "going out of scope" is often [mis]used to mean "to die, to end its lifetime", the formal meaning of "going out of scope" has nothing to do with "ceasing to exist". Mark obviously interpreted the term in the former sense, and most likely the OP did the same. So Mark's answer is "correct" within that assumed interpretation. Formally though, in general case when objects go out of scope they don't cease to exist and don't get their destructors called.
AndreyT
While it's certainly possible to execute out of lexical scope, it isn't possible (aside from threads) to execute out of dynamic scope. For most C++ purposes, I'm not sure that there is a good use for "going out of scope" that doesn't mean leaving the block a variable is defined in.
David Thornley
@David Thornley: Once again, a static variable defined inside a block goes out of scope every time you leave the block. This doesn't make it to "cease to exist". Additionally, the notion of "scope" is used rather heavily in the definitions of some important features of language (like name lookup). I'd say that promoting the incorrect definition of such an important term is not a good thing to do. In simple words, in C++ "going out of scope" means "becoming invisible to unqualified name lookup".
AndreyT
I was going under the assumption that "statically create" was a misnomer, and that he actually meant "locally create". Now that I think about it a little more, I realize that he could have declared a static object within a function - that of course would have a whole different answer.
Mark Ransom
No it wasn't created using the static. I didn't know any other way to say that it wasn't created using the new keyword.
chustar
Thanks for the clarification.
Mark Ransom
+2  A: 

Revised answer

There are two properties of a variable which are relevant here - scope and lifetime - and I think the question is conflating the two.

In all the contexts I can think of, a statically allocated object has a lifetime that is essentially the lifetime of the process. There are some technical details about exactly when the object is first initialized (constructed), but the net result is essentially the same - a statically allocated object exists for the duration of the process.

However, an object may come into scope, and go out of scope, as the thread of control moves between functions in the program. The scope of the object is where it is visible by name. It may be accessible elsewhere if a pointer to it (or reference to it) is passed to other functions where it would not otherwise be in scope.

Since a statically allocated object has a lifetime of the duration of the program, pointer members of that object do not change because the object goes out of scope; the object continues to exist unchanged, and the pointer members continue to point to the same place. Clearly, if a pointer in the statically allocated object points to a variable that had automatic duration and that pointed-to variable ceases to exist because it is destroyed, then the pointer in the statically allocated object points to an invalid location.

However, the key point is that the statically allocated object is not changed, and the pointer members are not changed, but changes in scope. And there are no leaks caused by the changes in scope.


Original answer

In all the contexts I can think of, a statically allocated object can't go out of scope, pretty much by definition. I suppose that if a shared library was loaded and then unloaded, then a statically allocated object might go 'out of scope', but otherwise...

If this premise is correct, then the second half of the question is easy. You can take either of two views:

  1. Since the static object never goes out of scope, nothing happens to the static object and its pointer member, and it will be pointing to the same place when the object comes back into scope - where scope here means 'into a function that could access the static object'.
  2. When the thread of control leaves a scope that could access the static object, nothing happens to the static object and its pointer member, and it will be pointing to the same place when the object next comes back into scope.

Which is basically saying the same thing, twice. If I said it a third time, it would automatically be true, wouldn't it? So, a statically allocated object doesn't go out of scope (even if it is not always accessible from the current function) and so nothing happens to the pointer members. There...what I said is so. I think!

What am I missing? Does 'statically created object' have a meaning I've not thought of?

Jonathan Leffler
Apparently you are misunderstanding the notion of "scope". Scope in C++ is the region in which the name of a declared entity is visible. To "go out of scope" does not mean "to die" in C++. Not even close. For example, a static object declared in a function goes out of scope every time function returns. However, it doesn't die. Next time you enter the function, you see the same object alive and well. It looks like the OP also misunderstands something about either "scope" or "static". I don't know which...
AndreyT
@Andrey: I didn't express it quite as well as I should have - what you're saying is what I was trying to say - I was (and still am) a but puzzled about the answer "Nothing happens to the pointer at all, it just ceases to exist. If it was pointing to something that needed to be freed, you just got a memory leak." which is currently selected as 'best'. The 'cease to exist' and 'leak' parts are antithetical to my understanding of what is going on.
Jonathan Leffler
Agree with AndreyT of course. Also see http://stackoverflow.com/questions/1388685/local-variable-scope-question/1389978#1389978
Johannes Schaub - litb