Flipkart.com

II.1: Where in memory are my variables stored?

Answer:
Variables can be stored in several places in memory, depending on their lifetime. Variables that are defined outside any function (whether of global or file static scope), and variables that are defined inside a function as static variables, exist for the lifetime of the program’s execution.

These variables are stored in the “data segment.” The data segment is a fixed-size area in memory set aside for these variables. The data segment is subdivided into two parts, one for initialized variables and another for uninitialized variables.
Variables that are defined inside a function as auto variables (that are not defined with the keyword static) come into existence when the program begins executing the block of code (delimited by curly braces {}) containing them, and they cease to exist when the program leaves that block of code. Variables that are the arguments to functions exist only during the call to that function.

These variables are stored on the “stack.”
The stack is an area of memory that starts out small and grows automatically up to some predefined limit.
In DOS and other systems without virtual memory, the limit is set either when the program is compiled or when it begins executing.

In UNIX and other systems with virtual memory, the limit is set by the system,
and it is usually so large that it can be ignored by the programmer. For a discussion on what virtual memory is, see II.3.

The third and final area doesn’t actually store variables but can be used to store data pointed to by variables. Pointer variables that are assigned to the result of a call to the malloc() function contain the address of a dynamically allocated area of memory. This memory is in an area called the “heap.” The heap is another area that starts out small and grows, but it grows only when the programmer explicitly calls malloc() or other memory allocation functions, such as calloc(). The heap can share a memory segment with either the data segment or the stack, or it can have its own segment. It all depends on the compiler options and operating system.

The heap, like the stack, has a limit on how much it can grow, and the same rules apply as to how
that limit is determined.
Reference:
I.1: What is a local block?
II.2: Do variables need to be initialized?
II.3: What is page thrashing?
VII.20: What is the stack?
VII.21: What is the heap?


II.2: Do variables need to be initialized?
Answer:
No. All variables should be given a value before they are used, and a good compiler will help you find variables that are used before they are set to a value. Variables need not be initialized, however.Variables defined outside a function or defined inside a function with the static keyword (those defined in the data segment discussed in the preceding FAQ) are already initialized to 0 for you if you do not explicitly initialize them.

Automatic variables are variables defined inside a function or block of code without the static keyword.
These variables have undefined values if you don’t explicitly initialize them. If you don’t initialize an automatic variable, you must make sure you assign to it before using the value.
Space on the heap allocated by calling malloc() contains undefined data as well and must be set to a known value before being used. Space allocated by calling calloc() is set to 0 for you when it is allocated.

Reference:
I.1: What is a local block?
VII.20: What is the stack?
VII.21: What is the heap?

No comments:

Post a Comment