void wickedSunny(char* szBlog);

Friday, May 28, 2004

static vs dynamic

There are multiple things in C++ that can be 'static' or 'dynamic'. If
none of the following match your situation, you should consider reposting
and clarifying the context a little more. Like, "Where, in what context,
were these words mentioned ?"

Most commonly, these two refer to the way a variable is stored in memory.
When a variable is considered 'static', it is stored in the local stack
memory. Each process that executes in a Win32 environment is allocated a
piece of memory for it's private purposes. This memory is known as
'stack'. It cannot be directly accessed by any other application, it is
private to the process in question. When a stack variable goes
out-of-scope (a function where the variable was declared is finished), it
is automatically destroyed and freed.

The alternative to 'static' variable is a 'dynamic' variable. At this
point, the variable is stored in heap memory. This heap memory is
considered all the RAM memory on your computer that is not
process-specific. It is available for use by different processes. Each
process can reserve a slice of this memory, use it to store a variable,
and then release the memory when it is no longer needed. A heap variable
never goes out-of-scope. You must manually reserve and release the
associated memory. Failing to release memory will lead to memory leaks.

The actual C++ keyword, static, when used with a variable, creates a
scope-independent variable. This means that when you create a static
variable inside a function, and then at some point re-call this function,
the variable is automatically initialized to the value it was when the
last function execution ended. So to speak, the variable is not 'lost'
when it goes out-of-scope.

All in all, I suggest you grab a good C++ -book and read it through with
thought. C++ is quite difficult at start, but when you get a grasp of it,
it becomes like a second language

-Antti Keskinen
--
" the future is in the air, can feel it everywhere "
href="http://students.iiit.net/~sanyam">http://students.iiit.net/~sanyam

0 Comments:

Post a Comment

<< Home