Gaijinco wrote:
I have always indirectly know how RAM is used when a C++ program is
run, but I wanted to be sure I got it right:
First the OS copies the whole program to RAM. Every statement of the
kind int x; would make the OS to look for free RAM to storage the value
of x.
Not really. The statement
int x;
allocates memory for x either from static memory or stack memory. See
below for more explanations.
Every statement like cout << something would cause the OS to
store "something" in RAM so it can be outputed.
When you have a statement such as:
cout << x;
You are calling an overloaded operator to do the output for you. You
know that either the value or the address of x is copied to the stack
frame of the routine (operator <<) being invoked. What the routine does
is up to the implementation. Usually, the routine converts the value of
x to a text string and sends the string to the OS for output.
The OS is ready to free
memory as it moves outside the scope of variables.
The OS does not free memory for you. Your program frees memory for
itself. That said, when your program frees a piece of dynamic memory, it
typically does request the OS to "unplug" the memory from the program's
memory space. See below for details.
So when a program says it has a minimun requirement of X RAM it means
that the most RAM used at any given time is never more than X.
Is this an accurate picture?
Forget about what you said. C++ basically deals with 3 kinds of memory:
static memory, stack memory, and dynamic memory.
Static memory is used to store global variables, static variables and
some constants. They can be used through out the program. This memory is
typically part of the program image so it is loaded along with the
program because the compiler always know the exact size of this piece of
memory. In addition, they are loaded to a different segment than the
program texts.
Stack memory is used to store local variables and data necessary to
facilitate function invocation (such as arguments, return address and
return value.) All local variables are deallocated in the reverse order
of allocation once they are out of scope. This memory is usually not
part of the on-disk program image and is created by the OS when the
program is loaded into RAM. Typically, this memory is loaded to the
stack segment with a fixed size. The stack memory is usually large
enough for most operations, but you can overflow it if not careful.
Dynamic memory is used to store "free-store" variables, such as those
created by operator new. The dynamic memory is not part of the program
image, nor is it allocated at load time. Rather, it is brought into the
program's memory space by the OS upon runtime requests (such as calling
operator new().)
For example consider the following program under a typical
implementation and platform:
int a = 1;
int main(void)
{
int b = 2;
int* c = new int(3);
delete c;
}
The global variable a is allocated statically. b is allocated in main's
stack frame when the flow of control enters main. The pointer c is also
allocated in main's stack frame, but the content it points to is
allocated in the dynamic memory by operator new.
If you wish to know more you should consider reading on assembly
language because that's the language that explains all of the memory
arrangements.
Regards,
Ben