Skip to main content

What is Stack and Heap

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 segmentstack 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.


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];
     // 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
pJ = pI;
} // block ends. pI gets destroyed, but not so the int array. It
// will stay there in memory until a delete is done.
delete [] pJ; // now the memory in the free store (aka heap) gets released.

Comments

Popular posts from this blog

Database View

View is a database object also called a logical table. it has no psychical existence. It is not like a simple table, but is a virtual or logical table which contains columns and data from different tables (may be one or more tables). A View does not contain any data, it is a set of queries that are applied to one or more tables that is stored within the database as an object. After creating a view from some table(s), it used as a reference of those tables and when executed, it shows only those data which are already mentioned in the query during the creation of the View. Creating view Syntax: ------------------------------------------------- CREATE VIEW [View_Name] AS [ SELECT Statement] ------------------------------------ ------------- CREATE VIEW SampleView As SELECT EmpID, EmpName FROM EmpInfo -------------------------------------------------- Data retrieve from view: SELECT * FROM SampleView WHERE EmpID ='FN0009C1'     View does not modif

Efficiently Paging Through Large Amounts of Data (PageIndex, Page Size) -SQL Server

In this article you learn how to fetch data according PageIndex and PageSize. In web application, it is much more important to increase webform performance, loadbalance. In my development experience, some of table hold large amount of records (more than 2GB) and user need to shows records in GridView. But problem is when we select all records and loads in webforms, webform has crashed. In that case, I will simply solved with Table Variable and using Grid Page Number and Page Size. 1. Create Procedure CREATE PROCEDURE Load_Data_WithPaging @PageIndex AS INT , /*Selected Row Index of selected grid*/ @PageSize AS INT , /*Total page size of selected grid*/ @TotalRecords AS INT OUT /*used for display virtual page no*/ AS BEGIN SET NOCOUNT ON ; DECLARE @FromIndex AS INT = 0 , @ToIndex AS INT = 0 ; SET @FromIndex = (@PageIndex * @PageSize) + 1 ; /*First row no selection*/ SET @ToIndex = ((@PageIndex

What is an Index? Explain Custered Index and Non-Clustered Index

Index Index is a database object, which can be created on one or more columns (16 Max column combination). When creating the index will read the column(s) and forms a relevant data structure to minimize the number of data comparisons. The index will improve the performance of data retrieval and adds some overhead on data modification such as create, delete and modify. So it depends on how much data retrieval can be performed on table versus how much of DML ( Insert , Delete and Update ) operations. clustered index A clustered index is something that reorganizes the way records in the table are physically stored. Therefore a table can have only one clustered index. The leaf nodes of a clustered index contain the data pages, by which I mean the key-value pair in the clustered index has the index key and the actual data value. Also remember, a clustered index will be created on a table by default the moment a primary key is created on the table. A clustered index is so