We are discussing about memory management, how the memory of the computer is organized for a running program. Ok lets go to discuss :-
When a program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment, and heap segment.
The text segment: sometimes also called the code segment is where the compiled code of the program itself resides.The remaining two areas of system memory is where storage may be allocated by the compiler for data storage. Two areas are called Stack and Heap
When a program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment, and heap segment.
The text segment: sometimes also called the code segment is where the compiled code of the program itself resides.The remaining two areas of system memory is where storage may be allocated by the compiler for data storage. Two areas are called Stack and Heap
Stack: Stack is a data structure in memory used for storing items in last in first out manner. The stack contains local variables and the call stack. In C#, Value types variable are stored directly on the stack.
The advantage of using the stack to store variables, is that memory is managed . we don't have to allocate memory by hand, or free it once we don't need it any more. What's more, because the CPU organizes stack memory so efficiently, reading from and writing to stack variables is very fast.
The scope of memory allocated on the ‘stack’ is the scope of the enclosing block. They get destroyed when the enclosing block is finished. Thats why they are called ‘auto’ objects.
Example:
{
int i;
} // block ends, i gets destroyed
Heap: The heap or managed heap is a data structure in memory where all objects-reference types are stored. When an object is instantiated, the object is stored on the heap as a block of data containing its data members. Then the memory address of the block is stored in a reference variable.
Example:
The scope of memory allocated on the ‘heap’ is different. Objects get allocated with new (or malloc) and get destroyed when a corresponding delete (or free) is executed. Blocks do no longer influence the scope of these objects.
Example:
int* pJ;
{
int* pI = new int [20];
int* pI = new int [20];
// Note that 2 things are happening here:
// 1.) An int array is allocated on the free store (aka heap).
// This array will be there until a corresponding delete [] is
// executed
// 2.) A variable pI is created. Since pI was not dynamically created
// with new or malloc, it is an auto object (created on the stack)
// Thus the enclosing block determines its lifetime
// 1.) An int array is allocated on the free store (aka heap).
// This array will be there until a corresponding delete [] is
// executed
// 2.) A variable pI is created. Since pI was not dynamically created
// with new or malloc, it is an auto object (created on the stack)
// Thus the enclosing block determines its lifetime
pJ = pI;
} // block ends. pI gets destroyed, but not so the int array. It
// will stay there in memory until a delete is done.
// will stay there in memory until a delete is done.
delete [] pJ; // now the memory in the free store (aka heap) gets released.
Comments
Post a Comment