So, the value of my static pointer is in memory after a
lightning storm and a power outage much less when the OS
decides to clean it up along with other globals after my
program exits?
The standard specifies the behavior of a conformant
implementation. I don't think any implementation is conformant
when the power is removed. Alteratively: the standard says that
behavior is undefined if resource limits are exceeded. One
generally thinks of things like stack overflow in this regard,
but one can easily argue that the program requires a certain
amount of electricity to run, and if that electricity is not
there, a resource limit has been exceeded.
Either way, the implementation is off the hook.
I think not. I don't even buy that it is there 5 minutes after
my return statement is executed. There is no written guarentee
of that. If there is please cut and paste it.
I don't have my copy of the standard here to cut and paste from,
but it's not a question of time. The fact is that neither in C
nor in C++ is returning from main the end of program execution;
both programs define it as calling exit with the return code,
and define the behavior of exit. Program execution only ends
when this call to exit is finished.
I am claiming that Windows, Linix, and Unix will indeed clean
up or allow write over of all the memory in which your
application resides, after the application exits the entry
function.
And that is obviously wrong. The standard is very clear that
e.g. the standard streams must flush their buffers, the standard
FILE streams (or all FILE streams---I forget) must be closed,
functions registered with atexit must be called and destructors
must be called.
If what you claimed was true, you'd be quickly out of memory
after 3 or 4 runs of most of your applications. It is changed,
it is no longer valid after the program exits main!
The C++ standard doesn't say what happens after the function
exit() has finished; you're no longer in a C++ program, so the
C++ standard has no say about it. The Posix standard, however,
does say that finishing exit is the equivalent of calling _exit,
and that does require that all open files (at the system level)
be closed, all memory affected to the process be released, etc.
But that's irrelevant here, since destructors of static objects
are run as part of exit(). (If you call _exit, under Posix,
your destructors won't be run. Nor will any functions
registered with atexit be called, nor will the buffers of your
standard streams be flushed.)
Instead of this thread growing to 90 posts each containing a
different vocabulary word. Let's please just cut and paste the
standard as it pertains to the initialization and
deinitialization of:
static objects in global scope.
global objects.
and both as it pertains to built in types if you like.
As I said, I don't have my copy of the standard here, but I
paraphrased it fairly closely in another posting. The standard
doesn't talk about "global objects" as such: it defines program
startup and shutdown in terms of object lifetime (static
lifetime), scope, and whether the initialization and destruction
is dynamic or static. (Destruction is static if and only if the
destructor is trivial. Initialization can be dynamic even
without a constructor, since the initialization expression may
require code to be executed.)
I believe, although I don't have the standard with me, that it
says C++ does not guarentee the initialization order or
de-initialization order of any of the above.
See my explinations else thread. The standard spends about a
page defining order of initialization (and specifies that order
of destruction is the reverse of order of initialization). Most
importantly, in this regard, it defines three types of
initialization of objects with static lifetime: zero
initialization, static initialization and dynamic
initialization, which are guaranteed to occur in that order (and
both zero initialization and dynamic initialization my apply to
the same object, e.g. as in:
Singleton* ourInstance = &Singleton::instance() ;
Singleton&
Singleton::instance()
{
if ( ourInstance == NULL ) {
ourInstance = new Singleton ;
}
return *ourInstance ;
}
).
They do not exist forever, they cannot, my PC will not even
exist forever.
No, but they exist as long as your program runs. And your
program doesn't stop running when you return from main.