When do global variables allocate their memory?

I was worried for a while, but I did not find a good resource on this. I have some global variables in my code. Obviously, they are initialized in some order, but is this a memmory needed for all those objects that were reserved before any initialization started?

here is a simple example of what might go wrong in my code and how I can use the answer:

I had a map<RTTI, object*> objectPool that contains samples of each class in my code, which I used to load objects from a file. To create these samples, I use some global variables to introduce an instance of the objectPool class. But sometimes, these instance instances were initialized before ObjectPool. And this generated runtime error.

To fix this error, I used the delayed initializer map<RTTI,object*>* lateInitializedObjectPool; . Now each instance first checks to see if objectPool is initialized and initializes it if not, and then it is embedded in the object pool. Everything seems to be working fine, but I'm worried even if the memmory needed for the object pool pointer is not reserved before other classes begin to represent themselves and could cause access violation.

+8
c ++ global-variables
source share
3 answers

Variables declared in the namespace area (as opposed to classes or functions) have space for the objects themselves (sizeof (ObjectType)) allocated by the executable (or DLL) loader. If the object is a POD that uses aggregate initialization, then it usually gets its values, establishing that the linker writes these values ​​directly to the executable file, and the exe loader simply explodes all of them in memory. Objects that do not use aggregate initialization first get their values.

After all this, if any of these objects has constructors, then these constructors are executed before main starts. Thus, if any of these constructors dynamically allocates memory, that is, when they do it. After loading the executable file, but before running main .

+6
source share

Typically, variables use separate memory areas that the compiler:

  • spent ones initially contain all 0s - possibly with the pre main() constructor working to change its content
  • predefined ones have a specific value other than 0, so that they can be written in a pre-designed form into an executable image, and the error page is ready for use.

When I say “separate memory area”, I mean some memory that the executable OS loader executes for the process, as well as for the stack or heap, but it differs in that these areas have a fixed predetermined size. On UNIX, the all-0 memory region mentioned above is commonly known as “BSS,” the non-0 initialized region as “data” - see http://en.wikipedia.org/wiki/Data_segment for more details.

+3
source share

C ++ has the concept of "static storage duration." This applies to all kinds of variables that occupy a fixed amount of space during program execution. These include not only global, but also static variables in the namespace, class, and function level.

Note that memory allocation in all cases can be done before main , but the actual initialization is different. In addition, some of them are initialized to zeros before they are usually initialized. Just like all this happens, it is not indicated: the compiler can add a call to a hidden function, otherwise the OS will simply be in the zero space of the process, etc.

+1
source share

All Articles