Re: variables in memory

From: Josef Moellers (josef.moellers_at_fujitsu-siemens.com)
Date: 02/10/05


Date: Thu, 10 Feb 2005 11:43:23 +0100

Levente KOVACS wrote:
> Hi,
>
>
> Just a simple question...
>
> If I declare a variable, will the system allocate physical memory for
> it, or will it be allocated only if I "write" a value in it?
>
> What I want to do is a big big matrix of unsigned integers... and I want
> my program as small as possible.

There are several aspects to your question:
1. global vs. local:
a global variable will be allocated on the "heap" and will exist all the
time while a local variable will be allocated on the stack and will only
exist during the invocation of the function it is declared within.

2. initialized vs. uninitialized
an initialized variable will use space within the executable file while
an uninitialized variable will only be allocated when the program is run.

3. virtual memory issues
physical memory is only allocated the first time you access a variable.
If your variable is small, it will share a memory page with other
variables. If your variable is large (e.g. a large array), then parts of
it may have physical memory allocated while others may not (yet).

HTH

-- 
Josef Möllers (Pinguinpfleger bei FSC)
	If failure had no penalty success would not be a prize
						-- T.  Pratchett


Relevant Pages