0% found this document useful (0 votes)
26 views

Dynamic Memory Allocation (I)

Dynamic memory allocation is the process of assigning memory space during runtime rather than at compile time. Memory is divided into code, data, and stack segments by the operating system, with an additional heap segment provided for dynamic allocation. Variables allocated in the heap using operators like new can change in size or be deleted during runtime using delete. Dynamic arrays can also be allocated in the heap using new[] and deleted using delete[]. Common mistakes include freeing memory that has already been freed, accessing freed memory, or not allocating memory before use.

Uploaded by

Anshuman021
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

Dynamic Memory Allocation (I)

Dynamic memory allocation is the process of assigning memory space during runtime rather than at compile time. Memory is divided into code, data, and stack segments by the operating system, with an additional heap segment provided for dynamic allocation. Variables allocated in the heap using operators like new can change in size or be deleted during runtime using delete. Dynamic arrays can also be allocated in the heap using new[] and deleted using delete[]. Common mistakes include freeing memory that has already been freed, accessing freed memory, or not allocating memory before use.

Uploaded by

Anshuman021
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Dynamic Memory Allocation

Memory Segments in C++


Memory is divided in certain segments:

Code Segment
Stores application code
Data Segment

Holds global data


Stack Segment

Used to contain local variable and other temporary information


In addition to these, the operating system provides and extra
amount of memory called Heap.
What is a dynamic memory
allocation?

Dynamic memory allocation is the process


of assigning the memory space during the
execution time or the run time.
About dynamic memory allocation

Stored in heap, outside the stack and data segments

May change size during runtime or may disappear for good

If no explicit allocation of memory is done, it never exists


double * dp;

Tells the compiler to have a pointer dp that will hold the
starting address of a variable of type double in heap memory
Syntax for Dynamic Variable
z
(contd...)
 To create the dynamic variable new is used.

     (Syntax) :- dp = new double;

 Will create a dynamic variable of type double and makes dp pointer point to
its starting address

 It is always a good practice to initialize dynamic variables.

-                    *dp = a or dp = new double(a);


Visual Demonstraition
Example:
        int * p; // declares a pointer p
        p = new int;  // dynamically allocate an int for loading
the address in p
        double * d;  // declares a pointer d
        d = new double;  // dynamically allocate a double
and loading the address in p
Deleting Dynamic Variable


 delete operator is used to delete the memory

allocated for the dynamic variable in heap

(Syntax):-    delete dp;

- Will delete 8 bytes of the memory allocated for the double variable


NOTE: the dp pointer will not be erased as it is created in code/data
segment and can be point some other dynamically allocated memory
location
Allocating dynamic arrays


C++ also provides the possibility to allocate an entire array in the heap

- int *table;

- table = new int[100];
Assigns 100 * sizeof(int) = 100 * 4 = 400 bytes of memory and assign the starting address to
table pointer


Arrays defined in heap are similar to the ones defined in data and code
segments and can be accessed using [ ]

For (int i = 0; i < 100; i++)
- table[i] = 0; will initialize all the array items to zero
Deleting dynamic array


Again delete operator is used
- [ ] is used will delete operator to tell the compiler that you are deleting arrays not
variables


(Syntax):- delete [ ] table;

will delete not only the memory location pointed by table but all
the items in the array
Common mistakes made in dynamic allocation


Freeing an already freed variable
char *str = new char [100];
delete [] str;
delete [] str; // error


Freeing a dynamic variable not assigned yet
char *str;
delete str; // error

Accessing or assigning value to the freed variable
int *str = new int[100];
delete [] str;
str[10] = 90; // error
Dynamic Memory Allocation for Objects

Output
Code
Example:

Code

Output
z
Thank You!

By Aryan Choudhary
Roll No.-28
BCA-3A

You might also like