Hi,

I want to know when and where is memory allocated for static variables. If:
1) static variable is in a function
2) static variable is a member of a class
3) a global static variable inside a file of code.

Much appreciated. Google hasn't been much of a friend in this case. :)

Recommended Answers

All 9 Replies

Memory for all types of static variables is allocated when the program is compiled, just the same as when memory is allocated to normal global variables. When your compiler creates the executable program that program includes memory for all the functions that you wrote (or linked with in libraries) as well as global and static variables. The operating system deals with all that when it loads the program into memory.

In short, memory allocation for the three cases you described are all identical.

Wonderfully explained. Thanks a lot. So this memory allocation for static variables is neither on heap nor on stack right, its all inside the executable.

Wonderfully explained. Thanks a lot. So this memory allocation for static variables is neither on heap nor on stack right, its all inside the executable.

this is an excerpt from The C++ programming language which explains the three types of memory usage available in C++. This should provide you with a bit more detail on where various items are created.

There are three fundamental ways of using memory in C++:

Static memory, in which an object is allocated by the linker for the duration of the program. Global and namespace variables, static class members (§10.2.4), and static variables in functions (§7.1.2) are allocated in static memory. An object allocated in static memory is constructed once and persists to the end of the program. It always has the same address. Static objects can be a problem in programs using threads (shared-address space concurrency) because they are shared and require locking for proper access.

Automatic memory, in which function arguments and local variables are allocated. Each entry into a function or a block gets its own copy. This kind of memory is automatically created and destroyed; hence the name automatic memory. Automatic memory is also said “to be on the stack.” If you absolutely must be explicit about this, C++ provides the redundant keyword auto.

Free store, from which memory for objects is explicitly requested by the program and where a program can free memory again once it is done with it (using new and delete). When a program needs more free store, new requests it from the operating system. Typically, the free store (also called dynamic memory or the heap) grows throughout the lifetime of a program because no memory is ever returned to the operating system for use by other programs.

The implementation details are totally offtopic here.But it worth to mention
that here. Executable file normally contains a data section and that data
section is splitted into several sub sections.
1. initialized data.
2. uninitialized data.
3. heap.
source: http://en.wikipedia.org/wiki/Data_segment

so according to that wiki page , static variables can contain in the initialized data and uninitialized data sections and they never be in the
heap.heap is normally located at the end of the data segment according
to the wiki page.Please correct me if I am wrong.

Thanks mattjbond, the explanation was very nice, and cleared almost everything for me.

Hi,

I want to know when and where is memory allocated for static variables. If:
1) static variable is in a function
2) static variable is a member of a class
3) a global static variable inside a file of code.

Much appreciated. Google hasn't been much of a friend in this case. :)

Google quickly got me here, which is a pretty concise description of the memory areas.

The implementation details are totally offtopic here.But it worth to mention
that here. Executable file normally contains a data section and that data
section is splitted into several sub sections.
1. initialized data.
2. uninitialized data.
3. heap.
source: http://en.wikipedia.org/wiki/Data_segment

so according to that wiki page , static variables can contain in the initialized data and uninitialized data sections and they never be in the
heap.heap is normally located at the end of the data segment according
to the wiki page.Please correct me if I am wrong.

1.) So from this it seems that heap memory is a part of the executable. It would increase the size of the executable as more memory is allocated(using new, malloc) while the program is executing. Whereas stack memory is in the RAM and doesn't change affect the size of the executable?

Would this analogy be correct:
heap:Hard drive::stack:RAM

2.)
static int i = 10;
If "i" is a global static variable would it be initialized at the compile time when its memory is allocated.

Would the same be true if "i" is inside a function.

1.) So from this it seems that heap memory is a part of the executable. It would increase the size of the executable as more memory is allocated(using new, malloc) while the program is executing. Whereas stack memory is in the RAM and doesn't change affect the size of the executable?

No.
The executable is generally going to be a fixed size.

Would this analogy be correct:
heap:Hard drive::stack:RAM

No.
"Heap": RAM, "Stack": RAM

2.)
static int i = 10;
If "i" is a global static variable would it be initialized at the compile time when its memory is allocated.

No.
Constants would be initialized at compile time. A static variable will be initialized at runtime (before main).

Would the same be true if "i" is inside a function.

It would be initialized at runtime (before main), yes.

Thanks a lot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.