Question
Sir,
I have following questions in C++
1.What is Stack and heap ? What is the use (2 separate) of it?
2.When i declare a global object where it is kept? Stack or heap? How compiler knows about
that? Only with new operator?
Answer
1.What is Stack and heap ? What is the use (2 separate) of it?
------------------------------------------------------------------------
The stack and heap are two separate memory areas used for data storage in main memory in a
C++ program.
Stack storage is automatically managed by the code and is used for data local to a function
call - note I say function call not function. Note that C/C++ call this automatic storage. The
stack works by allocating a new block of storage for each function call and relinquishing this
storage on function return. Processors usually have support for managing stack data - usually
in the form of specific registers to point to current stack top (and maybe a base pointer to the
whole stack frame for a function call) and instructions to push and pop data on and off of the
stack.
The stack is so named because of the data structure it uses - a stack or LIFO (last in first out).
Stacks are allocated on a per thread of execution basis and are fundamental to most if not all
implementations of threads, tasks and other types of units of execution.Operating systems
often have a separate kernel mode stack they use internally.
Although the operation of the stack has dynamic properties the compiler has enough
information to calculate what storage is required by each function call, and so has static
properties as well. Hence I suppose the term automatic rather than dynamic to prevent
confusion with fully dynamic storage.
A heap on the other hand is an area where the programmer has to manually manage allocation
and deallocation of storage by making specific allocation and deallocation requests (new and
delete in C++) and is used for dynamic storage (i.e. storage requested at runtime). The
memory used by a heap is usually obtained directly from the operating system, although
C/C++ runtime libraries may try to improve performance by requesting big blocks from the
operating system and dividing them up for small allocations. The heap is sometimes called
the free store in C and C++.
Note that heap is also a data structure. However whether a free store is really implemented as
a heap structure is an implementation issue.
2.When i declare a global object where it is kept? Stack or heap?
------------------------------------------------------------------------------------
Neither.
Such data is stored in the static storage area. Static storage is fixed for the whole program
duration. The compiler and linker have enough information to calculate the required storage
requirements at program build time and this is mapped into memory at program load time -
often directly from the executable file. Hence static objects exist for the whole duration of the
program - although those requiring complex (i.e. dynamic) initialisation will not be initialised
until some specific point in the program execution. Simple static initialisation (e.g.
initialising a static int to some value such as 123) can be done at build time and this value just
copied from the executable file into memory at program startup.
2a/ How compiler knows about that? Only with new operator?
-------------------------------------------------------------------
i) In a function definition static storage is requested using the static keyword. Otherwise an
object has automatic (stack) storage. Unlike local automatic objects, local static objects' state
survives across function calls but a local static object cannot be accessed outside of the
function (or smaller) scope in which it is defined.
ii) Outside of any function definition _any_ object definition defines an object having static
storage.
If such a definition is prefixed with the static keyword then it is private to the source file in
which it is defined (or more accurately to the translation unit in which it is defined. A
translation unit is approximately what is compiled by the compiler proper after
preprocessing). Such private static objects are only visible to code following their definition
until the end of the file. Such private static objects are said to have internal linkage.
If such a definition is not prefixed with the static keyword then it has external linkage and is
visible as a symbol to the linker, and therefore to other source files (translation units). Such
objects _must_ obey the one definition rule across the whole program and not just within the
source code file (translation unit) in which they are defined. Only _one_ translation unit can
define (i.e. cause storage to be reserved for) an external static object. All other uses must use
a declaration by using the extern keyword before the type and name of the global object.
So to summarise:
C and C++ have three types of storage:
1/ Static storage for function-local and internal and external global objects that have storage
allocated for the whole of the program duration. Complex static objects requiring dynamic
initialisation may not be fully initialised (created) until some later point (if at all) in a
programs execution - but once fully created they exist until destroyed during program
termination.
2/ Automatic storage on the stack. Such objects apply only to objects defined locally to a
function. They exist until the function returns or for a shorter time if they have a smaller
scope within the function. Automatic storage is the default storage class for local function
objects.
Stacks are allocated on a per unit of execution basis.
3/ Dynamic storage on the free store or heap is managed at runtime (i.e. dynamically) via
explicit calls or operators - e.g. new and delete. Objects allocated from the free store exist
until explicitly released (freed, deleted, deallocated, etc...).
In addition:
4/ The terms static and dynamic when used in the context of C++ programs often mean "at
compile / link time" (static) and "at run time" (dynamic).
5/ A function or object (variable) definition reserves storage for that entity. A declaration
merely states that such an entity exists. Such entities can only be _defined_ once in a
program. However they can be declared to exist as many times as required. A function
declaration is sometimes called a function prototype (from C). Most definitions in C and C++
are also declarations. Only global objects with external linkage can have declarations in C
and C++, all other such statements are definitions (and declarations).
Examples:
---------
extern int GlobalInt; // declaration of a static 'global' object defined elsewhere
int GlobalInt = 12; // definition of a static 'global' object having external linkage
static int PrivateStatic = 10; // definition of a static 'global' object having internal linkage
void SomeFunction // function declaration or prototype; function defined
elsewhere
( int n
, long & value
);
void SomeFunction // function definition:
( int n
, long & value // arguments are stack based having automatic storage
)
{
if ( 0==n ) // n for this call different from n of other calls
{
return;
}
static int callDepth(0); // Define static local variable. Only created once.
// State (value) of callDepth survives across function calls.
++callDepth;
std::cout << "At call depth: "
<< callDepth
<< 'n'
;
long newValue = value * n; // Define automatic object stored on function call stack
frame
SomeFunction( n-1, newValue ); // Recursive function call, gets a new stack frame
value = newValue;
--callDepth;
} // n, value, newValue for this call destroyed here;
// callDepth static so continues to exist with its current state
int main()
{
long * pValue = new long; // Dynamically create a new long object on the free store at
// runtime and return a pointer to it. Note the created object
// is default constructed in this case, which for built in types
// equates to being uninitialised.
*pValue = 1;
SomeFunction(10, *pValue);
std::cout << "Result of SomeFunction(10, 1) is " << *pValue << std::endl;
delete pValue; // Objects dynamically created exist until explicitly deleted.
// Note in this case we could probably get away with forgetting
// to delete the object pointed to by pValue as the next
// operation is to terminate the program execution and most
// modern desktop and server operating systems will be able to
// release all such process resources. However this is not true
// in the general case even for dynamically allocated objects
// that should be deleted as here just before program
// termination. Some resources held by such objects (of user
// defined types) may not be automatically released on program
// termination and some execution environments (e.g. embedded
// environments) may not do clean up for us.
}
// Static objects notionally destroyed here: after or during
// return from main but before program termination.
// This includes PrivateStatic, GlobalInt and callDepth
Hope this is of use and please ask further questions to clarify anything you do not understand
as trying to take all this in at once could be a bit overwhelming and I do not know what you
do or do not know already. Hopefully this will at least act as a starting point for your
enlightenment of this subject and something we can both refer to if you require further help.

More Related Content

PDF
Memory Management with Java and C++
PPT
Memory Management In C++
PDF
Memory Management C++ (Peeling operator new() and delete())
PPTX
What's New in C++ 11/14?
PPTX
Smart pointers
PDF
Smart Pointers in C++
PPTX
Dynamic memory allocation in c++
PPTX
C++11 - STL Additions
Memory Management with Java and C++
Memory Management In C++
Memory Management C++ (Peeling operator new() and delete())
What's New in C++ 11/14?
Smart pointers
Smart Pointers in C++
Dynamic memory allocation in c++
C++11 - STL Additions

What's hot (20)

PPTX
C++11 Multithreading - Futures
PDF
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
PPTX
PPT
data Structure Lecture 1
PDF
Writing Node.js Bindings - General Principles - Gabriel Schulhof
PPTX
classes & objects in cpp overview
PPTX
The Style of C++ 11
PDF
Uncommon Design Patterns
PPT
Link list
PDF
C++11: Rvalue References, Move Semantics, Perfect Forwarding
PPT
What's New in C++ 11?
PDF
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
PDF
The STL
PPT
friends functionToshu
PDF
Recursion to iteration automation.
PDF
C++aptitude questions and answers
PPT
STL ALGORITHMS
PPTX
PPTX
Алексей Кутумов, Вектор с нуля
PPTX
Advanced JavaScript
C++11 Multithreading - Futures
[C++] The Curiously Recurring Template Pattern: Static Polymorphsim and Expre...
data Structure Lecture 1
Writing Node.js Bindings - General Principles - Gabriel Schulhof
classes & objects in cpp overview
The Style of C++ 11
Uncommon Design Patterns
Link list
C++11: Rvalue References, Move Semantics, Perfect Forwarding
What's New in C++ 11?
Александр Гранин, Функциональная 'Жизнь': параллельные клеточные автоматы и к...
The STL
friends functionToshu
Recursion to iteration automation.
C++aptitude questions and answers
STL ALGORITHMS
Алексей Кутумов, Вектор с нуля
Advanced JavaScript
Ad

Viewers also liked (14)

DOC
45ος διεθνής διαγωνισμός αλληλογραφίας νέων 2016
PPTX
Emails week 3
PPTX
Android operating system
PDF
Трудове навчання 6 клас Сидоренко 2014 (для хлопців) от Freegdz.com
PPTX
Media evaluation question 2
PDF
wilder_vita_2016_links
PPTX
‫Object Oriented Programming_Lecture 3
PDF
Biologiya 9-klas-matyash-shabatura-2009
PPTX
C++ memory leak detection
PPTX
Java and SOA for beginners
PPTX
Evaluation Q4
PPTX
number system full discription in bangla
45ος διεθνής διαγωνισμός αλληλογραφίας νέων 2016
Emails week 3
Android operating system
Трудове навчання 6 клас Сидоренко 2014 (для хлопців) от Freegdz.com
Media evaluation question 2
wilder_vita_2016_links
‫Object Oriented Programming_Lecture 3
Biologiya 9-klas-matyash-shabatura-2009
C++ memory leak detection
Java and SOA for beginners
Evaluation Q4
number system full discription in bangla
Ad

Similar to Memory management in c++ (20)

PDF
Memory management in C++
PPT
Object Lifetime In C C++
PPTX
Storage Class Specifiers in C++
DOCX
New microsoft office word document (2)
PPT
Stack and heap allocation
PDF
Static Keyword Static is a keyword in C++ used to give special chara.pdf
PDF
intro_to_cpp_namespace_robotics_corner.pdf
PDF
Function in C++
DOCX
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)
PPT
C++tutorial
PPT
General Talk on Pointers
PDF
(5) cpp dynamic memory_arrays_and_c-strings
PDF
220 runtime environments
PDF
C++ questions And Answer
PDF
C++ Interview Question And Answer
PPT
Pointer
PDF
Classes and object
DOCX
Arrry structure Stacks in data structure
Memory management in C++
Object Lifetime In C C++
Storage Class Specifiers in C++
New microsoft office word document (2)
Stack and heap allocation
Static Keyword Static is a keyword in C++ used to give special chara.pdf
intro_to_cpp_namespace_robotics_corner.pdf
Function in C++
Chapter 5: Names, Bindings and Scopes (review Questions and Problem Set)
C++tutorial
General Talk on Pointers
(5) cpp dynamic memory_arrays_and_c-strings
220 runtime environments
C++ questions And Answer
C++ Interview Question And Answer
Pointer
Classes and object
Arrry structure Stacks in data structure

Recently uploaded (20)

PPTX
Wireless sensor networks (WSN) SRM unit 2
PDF
Mechanics of materials week 2 rajeshwari
PPTX
Cisco Network Behaviour dibuywvdsvdtdstydsdsa
PDF
SEH5E Unveiled: Enhancements and Key Takeaways for Certification Success
PDF
MLpara ingenieira CIVIL, meca Y AMBIENTAL
PPTX
Agentic Artificial Intelligence (Agentic AI).pptx
PDF
Micro 4 New.ppt.pdf a servay of cells and microorganism
PDF
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
PDF
VSL-Strand-Post-tensioning-Systems-Technical-Catalogue_2019-01.pdf
PDF
Computer System Architecture 3rd Edition-M Morris Mano.pdf
PDF
VTU IOT LAB MANUAL (BCS701) Computer science and Engineering
PPTX
Micro1New.ppt.pptx the mai themes of micfrobiology
PDF
Designing Fault-Tolerant Architectures for Resilient Oracle Cloud ERP and HCM...
PDF
AIGA 012_04 Cleaning of equipment for oxygen service_reformat Jan 12.pdf
PPTX
Design ,Art Across Digital Realities and eXtended Reality
PDF
August -2025_Top10 Read_Articles_ijait.pdf
PDF
Beginners-Guide-to-Artificial-Intelligence.pdf
PPTX
CT Generations and Image Reconstruction methods
PDF
electrical machines course file-anna university
PPT
Programmable Logic Controller PLC and Industrial Automation
Wireless sensor networks (WSN) SRM unit 2
Mechanics of materials week 2 rajeshwari
Cisco Network Behaviour dibuywvdsvdtdstydsdsa
SEH5E Unveiled: Enhancements and Key Takeaways for Certification Success
MLpara ingenieira CIVIL, meca Y AMBIENTAL
Agentic Artificial Intelligence (Agentic AI).pptx
Micro 4 New.ppt.pdf a servay of cells and microorganism
UEFA_Carbon_Footprint_Calculator_Methology_2.0.pdf
VSL-Strand-Post-tensioning-Systems-Technical-Catalogue_2019-01.pdf
Computer System Architecture 3rd Edition-M Morris Mano.pdf
VTU IOT LAB MANUAL (BCS701) Computer science and Engineering
Micro1New.ppt.pptx the mai themes of micfrobiology
Designing Fault-Tolerant Architectures for Resilient Oracle Cloud ERP and HCM...
AIGA 012_04 Cleaning of equipment for oxygen service_reformat Jan 12.pdf
Design ,Art Across Digital Realities and eXtended Reality
August -2025_Top10 Read_Articles_ijait.pdf
Beginners-Guide-to-Artificial-Intelligence.pdf
CT Generations and Image Reconstruction methods
electrical machines course file-anna university
Programmable Logic Controller PLC and Industrial Automation

Memory management in c++

  • 1. Question Sir, I have following questions in C++ 1.What is Stack and heap ? What is the use (2 separate) of it? 2.When i declare a global object where it is kept? Stack or heap? How compiler knows about that? Only with new operator? Answer 1.What is Stack and heap ? What is the use (2 separate) of it? ------------------------------------------------------------------------ The stack and heap are two separate memory areas used for data storage in main memory in a C++ program. Stack storage is automatically managed by the code and is used for data local to a function call - note I say function call not function. Note that C/C++ call this automatic storage. The stack works by allocating a new block of storage for each function call and relinquishing this storage on function return. Processors usually have support for managing stack data - usually in the form of specific registers to point to current stack top (and maybe a base pointer to the whole stack frame for a function call) and instructions to push and pop data on and off of the stack. The stack is so named because of the data structure it uses - a stack or LIFO (last in first out). Stacks are allocated on a per thread of execution basis and are fundamental to most if not all implementations of threads, tasks and other types of units of execution.Operating systems often have a separate kernel mode stack they use internally. Although the operation of the stack has dynamic properties the compiler has enough information to calculate what storage is required by each function call, and so has static properties as well. Hence I suppose the term automatic rather than dynamic to prevent confusion with fully dynamic storage. A heap on the other hand is an area where the programmer has to manually manage allocation and deallocation of storage by making specific allocation and deallocation requests (new and delete in C++) and is used for dynamic storage (i.e. storage requested at runtime). The memory used by a heap is usually obtained directly from the operating system, although C/C++ runtime libraries may try to improve performance by requesting big blocks from the operating system and dividing them up for small allocations. The heap is sometimes called the free store in C and C++. Note that heap is also a data structure. However whether a free store is really implemented as a heap structure is an implementation issue. 2.When i declare a global object where it is kept? Stack or heap?
  • 2. ------------------------------------------------------------------------------------ Neither. Such data is stored in the static storage area. Static storage is fixed for the whole program duration. The compiler and linker have enough information to calculate the required storage requirements at program build time and this is mapped into memory at program load time - often directly from the executable file. Hence static objects exist for the whole duration of the program - although those requiring complex (i.e. dynamic) initialisation will not be initialised until some specific point in the program execution. Simple static initialisation (e.g. initialising a static int to some value such as 123) can be done at build time and this value just copied from the executable file into memory at program startup. 2a/ How compiler knows about that? Only with new operator? ------------------------------------------------------------------- i) In a function definition static storage is requested using the static keyword. Otherwise an object has automatic (stack) storage. Unlike local automatic objects, local static objects' state survives across function calls but a local static object cannot be accessed outside of the function (or smaller) scope in which it is defined. ii) Outside of any function definition _any_ object definition defines an object having static storage. If such a definition is prefixed with the static keyword then it is private to the source file in which it is defined (or more accurately to the translation unit in which it is defined. A translation unit is approximately what is compiled by the compiler proper after preprocessing). Such private static objects are only visible to code following their definition until the end of the file. Such private static objects are said to have internal linkage. If such a definition is not prefixed with the static keyword then it has external linkage and is visible as a symbol to the linker, and therefore to other source files (translation units). Such objects _must_ obey the one definition rule across the whole program and not just within the source code file (translation unit) in which they are defined. Only _one_ translation unit can define (i.e. cause storage to be reserved for) an external static object. All other uses must use a declaration by using the extern keyword before the type and name of the global object. So to summarise: C and C++ have three types of storage: 1/ Static storage for function-local and internal and external global objects that have storage allocated for the whole of the program duration. Complex static objects requiring dynamic initialisation may not be fully initialised (created) until some later point (if at all) in a programs execution - but once fully created they exist until destroyed during program
  • 3. termination. 2/ Automatic storage on the stack. Such objects apply only to objects defined locally to a function. They exist until the function returns or for a shorter time if they have a smaller scope within the function. Automatic storage is the default storage class for local function objects. Stacks are allocated on a per unit of execution basis. 3/ Dynamic storage on the free store or heap is managed at runtime (i.e. dynamically) via explicit calls or operators - e.g. new and delete. Objects allocated from the free store exist until explicitly released (freed, deleted, deallocated, etc...). In addition: 4/ The terms static and dynamic when used in the context of C++ programs often mean "at compile / link time" (static) and "at run time" (dynamic). 5/ A function or object (variable) definition reserves storage for that entity. A declaration merely states that such an entity exists. Such entities can only be _defined_ once in a program. However they can be declared to exist as many times as required. A function declaration is sometimes called a function prototype (from C). Most definitions in C and C++ are also declarations. Only global objects with external linkage can have declarations in C and C++, all other such statements are definitions (and declarations). Examples: --------- extern int GlobalInt; // declaration of a static 'global' object defined elsewhere int GlobalInt = 12; // definition of a static 'global' object having external linkage static int PrivateStatic = 10; // definition of a static 'global' object having internal linkage void SomeFunction // function declaration or prototype; function defined elsewhere ( int n , long & value ); void SomeFunction // function definition: ( int n , long & value // arguments are stack based having automatic storage )
  • 4. { if ( 0==n ) // n for this call different from n of other calls { return; } static int callDepth(0); // Define static local variable. Only created once. // State (value) of callDepth survives across function calls. ++callDepth; std::cout << "At call depth: " << callDepth << 'n' ; long newValue = value * n; // Define automatic object stored on function call stack frame SomeFunction( n-1, newValue ); // Recursive function call, gets a new stack frame value = newValue; --callDepth; } // n, value, newValue for this call destroyed here; // callDepth static so continues to exist with its current state int main() { long * pValue = new long; // Dynamically create a new long object on the free store at // runtime and return a pointer to it. Note the created object // is default constructed in this case, which for built in types // equates to being uninitialised. *pValue = 1; SomeFunction(10, *pValue); std::cout << "Result of SomeFunction(10, 1) is " << *pValue << std::endl; delete pValue; // Objects dynamically created exist until explicitly deleted. // Note in this case we could probably get away with forgetting // to delete the object pointed to by pValue as the next // operation is to terminate the program execution and most // modern desktop and server operating systems will be able to // release all such process resources. However this is not true // in the general case even for dynamically allocated objects // that should be deleted as here just before program // termination. Some resources held by such objects (of user // defined types) may not be automatically released on program
  • 5. // termination and some execution environments (e.g. embedded // environments) may not do clean up for us. } // Static objects notionally destroyed here: after or during // return from main but before program termination. // This includes PrivateStatic, GlobalInt and callDepth Hope this is of use and please ask further questions to clarify anything you do not understand as trying to take all this in at once could be a bit overwhelming and I do not know what you do or do not know already. Hopefully this will at least act as a starting point for your enlightenment of this subject and something we can both refer to if you require further help.