Stream :-
B.Sc. (Hons.) Computer Science
2nd Semester
DEPARTMENT OF COMPUTER SCIENCE AND APPLICATIONS
ATAL BIHARI VAJPAYEE VISHWAVIDYALAYA
KONI, BILASPUR, CHHATTISGARH.
Presented by :-
AKHIL MISHRA.
Presentation Topic :-
Memory Allocation and
Dynamic Memory Allocation
Presented to :-
Mr. JEETENDRA KUMAR GUPTA.
(Assistant professor)
• Memory Allocation
• Static Memory and Dynamic
Memory
• The Stack Memory
• The Heap Memory
• Types of Memory Allocation
• Why we use Dynamic
Memory Allocation?
• Differentiate between Static
Memory Allocation and
Dynamic Memory Allocation
Contents
• How is memory
allocated/deallocated in C
and C++
• New Operator
• Delete Operator
• malloc() function
• calloc() function
• realloc() function
• free() function
Memory Allocation
Process of reserving or providing space
to a variable is called memory
allocation.
For storing the data, memory allocation
can be done in two ways –
 Static Allocation or Compile-time Allocation.
 Dynamic Allocation or Run-time Allocation.
 Static Memory :- It is a constant space allocated by the
operating system during the compile time of a program and it
internally uses stack data structure to manage the static
memory allocation. We can't reallocate the space consumed by
the program until its execution is over.
 Compile-time is the time period when a program code is translated
into a low-level code or machine code, either by a compiler or an
interpreter.
 Dynamic Memory :- It is the memory that can be allocated or
de-allocated by the operating system during the run-time of a
program. It is more efficient than static memory because we
can de-allocate and reuse our memory during the run-time of
our program.
 Runtime refers to the time when the converted code, which is now in
low level or machine code, is executed. In other words, runtime refers
to the time when the code does what it was written to do.
The memory used by a program can be divided further
into four parts :-
1. Run-time Stack (Static Memory)
2. Static Data Memory (for Global and Static Variables)
3. Instructions / Statements (Static Memory)
4. Heap (Dynamic Memory)
Static
Memory
Dynamic
Memory
Run-time
Stack
Static Data
Memory
Instructions
Heap
The Stack memory
 Our operating system allocates a constant
space during compile-time of a program, this
space is known as Stack memory.
 Stack memory is used to hold functions, different
variables, and local statements that exist in the
function definition.
 Stack is a part of the static memory in our system
and it constitutes the majority of our system's
static memory.
The Heap memory
 Heap memory is also known as the dynamic
memory in our system. It can be understood as a large
block of memory that is expandable and
shrinkable during the execution of a program.
 Allocation and De-allocation of memory
blocks during the execution of a program can be done
using new and delete operators in C++ and malloc(),
calloc(), free(), and realloc () functions in C.
 Heap memory can grow as long as we have enough
machine memory available. It is not good from a
programming perspective to completely use the machine
memory to avoid errors, thus we must use the heap
memory carefully.
Types of MemoryAllocation
 Static Memory Allocation in (Compile-time Memory Allocation)
 When memory is allocated at compile-time, it is referred to as Static Memory
Allocation.
 A fixed space is allocated for the local variables, function calls, and local
statements, that can not be changed during the execution of the program.
 We can not allocate or de-allocate a memory block once the execution of the
program starts.
 We can't re-use the static memory while the program is running. As a result, it is
less effective.
 Dynamic Memory Allocation in (Run-time Memory Allocation)
 When memory is allocated or de-allocated during run-time, it is referred to
as Dynamic Memory Allocation.
 A variable space is allocated that can be changed during the execution of the
program.
 We use dynamic/heap memory to allocate and de-allocate a block of memory
during the execution of the program using new and delete operators.
 We can re-use our heap memory during the run-time of our program. As a result, it
is highly effective.
Hierarchy of Datatypes
 Datatype
 User-defined datatype
 Structure
 Union
 Class
 Built-in datatypes
 Integer
 Int
 Long
 Void
 Floating-point
 Flaot
 Double
 Derived datatypes
 String
 Array
Why we use Dynamic Memory Allocation?
 There were some drawbacks of stack memory or
static memory allocation
 Like the space allocated for the stack can not be
expanded during the execution of a program
 Or we can't keep variables in the program till the
time we want.
 So, to overcome these drawbacks, we use
the Dynamic Memory Allocation concepts.
Differentiate between Static MemoryAllocation and
Dynamic MemoryAllocation
Static Memory Allocation Dynamic Memory Allocation
• Memory allocation done before
program execution.
• Memory allocation done after
program execution.
• It use stack. • It use heap.
• Stack use for managing static
allocation of memory.
• Heap use for managing dynamic
memory allocation.
• It is less efficient. • It is more efficient.
• Once the memory is allocated,
the memory size can not change.
• When memory is allocated the
memory size can be changed.
• This memory is allocated at
compile time.
• This memory is allocated at run
time.
How is Memory Allocates/De-allocates in C and
C++?
 In C Language, before allocating or de-allocating memory
dynamically (at run-time), we have to
include <stdlib.h> header file to use the library functions
like malloc(), calloc(), realloc() and free().
 In C++ Language, new and delete operators are pre-
defined in the C++ Standard Library and don't require to
include any library file for run-time allocation and de-
allocation of memory blocks. Although we can
use malloc(), calloc(), and other functions in C++ as well
by adding the <stdlib.h> header file because of the
backward compatibility of C++ with C Language.
New Operator
 It allocates memory during execution time.
 New Operator returns a pointer to the address
where the object is stored.
 Syntax:- pointer variable = new datatype
 Eg :- int *a = new int ;
Allocates sufficient memory to hold the
object of datatype int & returns a pointer to
its starting point.
The pointer variable holds the address of
memory space allocated
int *a = new int;
*a = 20;
int *a = new int(20);
Delete Operator
 It is used for releasing memory space when the
object is no longer needed.
 Syntax:- delete pointer variable;
The pointer variable holds the address of
memory space allocated
keyword
 Imp points :-
1. The programmer must take care not to free or delete a pointer
variable that has already been deleted.
2. The programmer must take care not a free or delete a pointer
variable that have ben allocated using a new operator.
3. Overloading of new & delete operator is possible.
4. Null pointer is returned by the new operator, when there is in
sufficient memory available for allocation.
Eg :-
malloc() function
 malloc() stands for memory allocation.
 It reserves a block of memory with the given amount of
bytes.
 The return value is a white pointer to the allocated space
 Therefore the void pointer need to be casted to the
appropriate type as per the requirements
 However, if the space is insufficient, allocation of memory
fails and it returns a NULL pointer.
 All the values at allocated memory are initialised to the
garbage values.
 Syntax:-
ptr = (ptr-type*) malloc(size_in_bytes)
Eg :-
calloc() function
 calloc() stands for continuous allocation.
 It reserves a block of memory with the given amount of
bytes.
 The return value is a white pointer to the allocated space
 Therefore the void pointer need to be casted to the
appropriate type as per the requirements
 However, if the space is insufficient, allocation of memory
fails and it returns a NULL pointer.
 All the values at allocated memory are initialised to 0.
 Syntax:-
ptr = (ptr-type*) calloc(n, size_in_bytes)
Eg :-
realloc() function
 realloc() stands for reallocation.
 If the dynamically allocated memory is
insufficient we can change the size of previously
allocated memory using realloc() function.
 Syntax:-
ptr = (ptr-type*) realloc(ptr, new_size_in_bytes)
Eg :-
free() function
 free() is used to free the allocated memory.
 If the dynamically allocated memory is not
required anymore, we can free it using free()
function.
 This will free the memory being used by the
program in the heap
 Syntax:-
Free(ptr);
Eg :-
THANK YOU!

Memory Allocation & Direct Memory Allocation in C & C++ Language PPT

  • 1.
    Stream :- B.Sc. (Hons.)Computer Science 2nd Semester DEPARTMENT OF COMPUTER SCIENCE AND APPLICATIONS ATAL BIHARI VAJPAYEE VISHWAVIDYALAYA KONI, BILASPUR, CHHATTISGARH. Presented by :- AKHIL MISHRA. Presentation Topic :- Memory Allocation and Dynamic Memory Allocation Presented to :- Mr. JEETENDRA KUMAR GUPTA. (Assistant professor)
  • 2.
    • Memory Allocation •Static Memory and Dynamic Memory • The Stack Memory • The Heap Memory • Types of Memory Allocation • Why we use Dynamic Memory Allocation? • Differentiate between Static Memory Allocation and Dynamic Memory Allocation Contents • How is memory allocated/deallocated in C and C++ • New Operator • Delete Operator • malloc() function • calloc() function • realloc() function • free() function
  • 3.
    Memory Allocation Process ofreserving or providing space to a variable is called memory allocation. For storing the data, memory allocation can be done in two ways –  Static Allocation or Compile-time Allocation.  Dynamic Allocation or Run-time Allocation.
  • 4.
     Static Memory:- It is a constant space allocated by the operating system during the compile time of a program and it internally uses stack data structure to manage the static memory allocation. We can't reallocate the space consumed by the program until its execution is over.  Compile-time is the time period when a program code is translated into a low-level code or machine code, either by a compiler or an interpreter.  Dynamic Memory :- It is the memory that can be allocated or de-allocated by the operating system during the run-time of a program. It is more efficient than static memory because we can de-allocate and reuse our memory during the run-time of our program.  Runtime refers to the time when the converted code, which is now in low level or machine code, is executed. In other words, runtime refers to the time when the code does what it was written to do.
  • 5.
    The memory usedby a program can be divided further into four parts :- 1. Run-time Stack (Static Memory) 2. Static Data Memory (for Global and Static Variables) 3. Instructions / Statements (Static Memory) 4. Heap (Dynamic Memory) Static Memory Dynamic Memory Run-time Stack Static Data Memory Instructions Heap
  • 6.
    The Stack memory Our operating system allocates a constant space during compile-time of a program, this space is known as Stack memory.  Stack memory is used to hold functions, different variables, and local statements that exist in the function definition.  Stack is a part of the static memory in our system and it constitutes the majority of our system's static memory.
  • 7.
    The Heap memory Heap memory is also known as the dynamic memory in our system. It can be understood as a large block of memory that is expandable and shrinkable during the execution of a program.  Allocation and De-allocation of memory blocks during the execution of a program can be done using new and delete operators in C++ and malloc(), calloc(), free(), and realloc () functions in C.  Heap memory can grow as long as we have enough machine memory available. It is not good from a programming perspective to completely use the machine memory to avoid errors, thus we must use the heap memory carefully.
  • 8.
    Types of MemoryAllocation Static Memory Allocation in (Compile-time Memory Allocation)  When memory is allocated at compile-time, it is referred to as Static Memory Allocation.  A fixed space is allocated for the local variables, function calls, and local statements, that can not be changed during the execution of the program.  We can not allocate or de-allocate a memory block once the execution of the program starts.  We can't re-use the static memory while the program is running. As a result, it is less effective.  Dynamic Memory Allocation in (Run-time Memory Allocation)  When memory is allocated or de-allocated during run-time, it is referred to as Dynamic Memory Allocation.  A variable space is allocated that can be changed during the execution of the program.  We use dynamic/heap memory to allocate and de-allocate a block of memory during the execution of the program using new and delete operators.  We can re-use our heap memory during the run-time of our program. As a result, it is highly effective.
  • 9.
    Hierarchy of Datatypes Datatype  User-defined datatype  Structure  Union  Class  Built-in datatypes  Integer  Int  Long  Void  Floating-point  Flaot  Double  Derived datatypes  String  Array
  • 10.
    Why we useDynamic Memory Allocation?  There were some drawbacks of stack memory or static memory allocation  Like the space allocated for the stack can not be expanded during the execution of a program  Or we can't keep variables in the program till the time we want.  So, to overcome these drawbacks, we use the Dynamic Memory Allocation concepts.
  • 11.
    Differentiate between StaticMemoryAllocation and Dynamic MemoryAllocation Static Memory Allocation Dynamic Memory Allocation • Memory allocation done before program execution. • Memory allocation done after program execution. • It use stack. • It use heap. • Stack use for managing static allocation of memory. • Heap use for managing dynamic memory allocation. • It is less efficient. • It is more efficient. • Once the memory is allocated, the memory size can not change. • When memory is allocated the memory size can be changed. • This memory is allocated at compile time. • This memory is allocated at run time.
  • 12.
    How is MemoryAllocates/De-allocates in C and C++?  In C Language, before allocating or de-allocating memory dynamically (at run-time), we have to include <stdlib.h> header file to use the library functions like malloc(), calloc(), realloc() and free().  In C++ Language, new and delete operators are pre- defined in the C++ Standard Library and don't require to include any library file for run-time allocation and de- allocation of memory blocks. Although we can use malloc(), calloc(), and other functions in C++ as well by adding the <stdlib.h> header file because of the backward compatibility of C++ with C Language.
  • 13.
    New Operator  Itallocates memory during execution time.  New Operator returns a pointer to the address where the object is stored.  Syntax:- pointer variable = new datatype  Eg :- int *a = new int ; Allocates sufficient memory to hold the object of datatype int & returns a pointer to its starting point. The pointer variable holds the address of memory space allocated int *a = new int; *a = 20; int *a = new int(20);
  • 14.
    Delete Operator  Itis used for releasing memory space when the object is no longer needed.  Syntax:- delete pointer variable; The pointer variable holds the address of memory space allocated keyword  Imp points :- 1. The programmer must take care not to free or delete a pointer variable that has already been deleted. 2. The programmer must take care not a free or delete a pointer variable that have ben allocated using a new operator. 3. Overloading of new & delete operator is possible. 4. Null pointer is returned by the new operator, when there is in sufficient memory available for allocation.
  • 15.
  • 16.
    malloc() function  malloc()stands for memory allocation.  It reserves a block of memory with the given amount of bytes.  The return value is a white pointer to the allocated space  Therefore the void pointer need to be casted to the appropriate type as per the requirements  However, if the space is insufficient, allocation of memory fails and it returns a NULL pointer.  All the values at allocated memory are initialised to the garbage values.  Syntax:- ptr = (ptr-type*) malloc(size_in_bytes)
  • 17.
  • 18.
    calloc() function  calloc()stands for continuous allocation.  It reserves a block of memory with the given amount of bytes.  The return value is a white pointer to the allocated space  Therefore the void pointer need to be casted to the appropriate type as per the requirements  However, if the space is insufficient, allocation of memory fails and it returns a NULL pointer.  All the values at allocated memory are initialised to 0.  Syntax:- ptr = (ptr-type*) calloc(n, size_in_bytes)
  • 19.
  • 20.
    realloc() function  realloc()stands for reallocation.  If the dynamically allocated memory is insufficient we can change the size of previously allocated memory using realloc() function.  Syntax:- ptr = (ptr-type*) realloc(ptr, new_size_in_bytes)
  • 21.
  • 22.
    free() function  free()is used to free the allocated memory.  If the dynamically allocated memory is not required anymore, we can free it using free() function.  This will free the memory being used by the program in the heap  Syntax:- Free(ptr);
  • 23.
  • 24.