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

What is RDBMS?

In our crucial programming world, RDBMS is stand for Relational Database Management Systems is based on the Relational Model that maintain data records and index in tables. Generally, the term "Relations" are maintained in RDMS.  In a relational database,store the data into collection of tables, which might be related by common fields (database table columns). RDBMS also provide relational operators to manipulate the data stored into the database tables. Most RDBMS use SQL as database query language. There are several objects are included in RDBMS like ,Indexes,Synonyms,Tables,Views Clusters,Constraints,Database links,Database triggers. Most popular RDBMS  are Oracle, MS-SqlServer, MySql,DB2. etc.

What are the difference between DDL, DML , DCL and TCL commands?

Database Command Types DDL Data Definition Language  (DDL) statements are used to define the database structure or schema.  -           CREATE - to create objects in the database -           ALTER - alters the structure of the database -           DROP - delete objects from the database -           TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed -           COMMENT - add comments to the data dictionary -           RENAME - rename an object DML Data Manipulation Language  (DML) statements are used for managing data in database. DML commands are not auto-committed. It means changes made by DML command are not permanent to d...

Identify and Delete duplicate Records in SQL Server

      S cenario: I have accumulated data into SQL Server database from three different servers (oracle, O2 and oracle) for a financial reporting. After data accumulated , we found more duplicate rows; business requirement was to remove all duplicate rows because of final data will be import to another system. For this purpose, I want to write script to find out duplicate rows from my table. Finally, I want to delete only duplicate rows not original row. Original row means if i have total 2 rows those same or duplicate, so I want to delete only one row that is duplicate. Let’s go............................ Step 1: Create a student Table below Script: CREATE TABLE [Students] (     [ID] [int] NULL,     [Name] [varchar] ( 50 ) NULL,     [address] [varchar] ( 50 ) NULL,     [contact] [varchar] ( 50 ) NULL,     [email] [varchar] ( 50 ) NULL ...