C ++: Static primitives become invalid when a program exits?

Suppose I have a function like this:

MyClass &MyFunction(void) { static MyClass *ptr = 0; if (ptr == 0) ptr = new MyClass; return MyClass; } 

The question is when the program will exit, will the ptr variable become invalid (i.e. the contents of this ptr will be cleared by the exit process)? I understand that this function is leaking, but this is just an example for simplicity.

The same question applies to primitives other than pointers. How about if I have a static integer, is the value of that integer always stored during exit or is it variable due to problems with the order of static destruction?

EDIT:

To clarify, I want to know what really happens with the contents of a static pointer (or any other primitive type, such as int or float), and not the memory it points to. For example, imagine ptr points to some memory address that I want to check in the destructor of some other static class. Can I rely on the fact that the contents of ptr will not be changed (i.e., that the value of the pointer will not be cleared during the static destruction process)?

Thanks Joe

+4
source share
5 answers

To answer your question:

 'imagine that the ptr points to some memory address which I want to check in the destructor of some other static class' 

The answer is yes. You can see the value of the pointer (address).
You can see the contents if you did not call delete on the pointer.

Static function variables behave the same way as class static variables and global variables ( aka not local static), since destructors will be called in the opposite order of creation. Integers, float, and pointers (PODs) have no destructors, so nothing happens to them until processes are deleted.

POD objects: data can safely refer to the destructor of other objects (even global ones).

Other static objects (i.e. those that have destructors): In general, it is unsafe to access these objects after the main () exit, because the order of destruction is unknown (this is the opposite of the order of creation, but the order of creation is complex: Construction order ) . This can be done, but you must take explicit precautions to make sure that the object is still alive.

Note: not local static:

The memory will always be there, the object will simply be invalid after the destructor is called (the POD note does not have a destructor).

Note: Stack:

Valid only until the area in which they are declared is declared.
After the stack pops up, the page of memory on which it resides can potentially be reset, which will lead to SEG errors if you try to access it.

Note: Heap:

Valid until you name delete on the pointer that allocated it.
After the pointer is deleted, the value is potentially random, since it can be reused. Potentially, the page on which the memory was turned on can also be deleted. Any access to the dropped page will result in a SEG error.

+3
source

When you finish exiting, all memory pages allocated to it will be freed up by the OS (compared to other shared memory files that others may use).

However, as others point out, the MyClass destructor is never called. Also, the value pointed to by ptr has not changed. If you have a static int with value 123, then its value will remain from 123 until the very end of the process.

+6
source

In modern operating systems, all application memory is allocated according to the heap specific to this application. When the application exits, all memory in this heap will be freed.

So, the memory will be freed, but : the destructor for MyClass will never be called. This can be a problem if the destructor is responsible for freeing any resources other than memory (file system locks are a common example).

+4
source

To answer your updated question, I would say yes: you can rely on the value of this static pointer remaining during the static destruction process. The memory to which it points can be freed, but the value of the pointer itself must remain unchanged, unless the destructor of another static class changes it.

+3
source

The short answer is "no": your pointer will not "become invalid" during program exit. That is, the value of the pointer will not automatically be reset to null, and the destructor of the MyClass object to which it points will not be called automatically.

This is because a pointer is a "primitive type", i.e. not an object.

If you have a non-local (i.e. global or static) variable that is an object, then the rules are different: the object's destructor will be called when the program terminates by calling exit () or by returning from the main function. It will not be called if the program terminates by calling the abort () function.

+1
source

All Articles