04 Dynamic Memory Allocation
04 Dynamic Memory Allocation
Objectives
• In this chapter you will learn:
– How to define the size of an array dynamically
– How to allocate /deallocate memory in a program
– How to perform dynamic memory management
Pointer Declaration
• General format:
data_type *pointer_name;
• E.g.:
int *numberPtr;
int number = 3;
int *numberPtr = &number; Output:
printf(“%d”, *numberPtr); 3
printf(“%d”, numberPtr); 11110000
printf(“%d”, &*numberPtr); 11110000
printf(“%d”, *&numberPtr); 11110000
return 0;
Memory Allocation
• What is Memory Allocation ?
• Types of memory allocation
– Static
– Dynamic
• Static Memory Allocation – compiler time
• Dynamic Memory Allocation – run time
• We can control the allocation and deallocation
of memory in a program for objects and for
arrays :
– of any built-in or user-defined type.
AS Dept. of Systems & Networking 5
CSNB244 Programming II with C++
continue