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

04 Dynamic Memory Allocation

The document discusses dynamic memory allocation in C++ including defining pointer variables, allocating memory using new and deallocating memory using delete, initializing dynamically allocated memory, allocating arrays dynamically using new [], and deallocating dynamically allocated arrays using delete [].

Uploaded by

Adli Syukri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

04 Dynamic Memory Allocation

The document discusses dynamic memory allocation in C++ including defining pointer variables, allocating memory using new and deallocating memory using delete, initializing dynamically allocated memory, allocating arrays dynamically using new [], and deallocating dynamically allocated arrays using delete [].

Uploaded by

Adli Syukri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

CSNB244 Programming II with C++

CSNB244 Programming II with C++

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

• To use dynamic memory allocation you have


to understand and use pointer variables.

AS Dept. of Systems & Networking 2


CSNB244 Programming II with C++

Pointer Declaration
• General format:

data_type *pointer_name;

• E.g.:
int *numberPtr;

• Declares numberptr as a variable that points to an


integer variable.
• Its content is a memory address
AS Dept. of Systems and Networking 3
CSNB244 Programming II with C++

Referencing & Dereferencing


• E.g. Assume that the address of number is
11110000. 11110000
number 3

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;

AS Dept. of Systems and Networking 4


CSNB244 Programming II with C++

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++

Dynamic Memory Management


• How ??
• new
• delete.
• new operator: dynamically allocate (i.e. reserve)
the exact amount of memory required to hold
an object or array at execution time.
• The object or array created in the free store (also
called the heap) - a region of memory assigned
to each program for storing dynamically
allocated objects.
AS Dept. of Systems & Networking 6
CSNB244 Programming II with C++

Dynamic Memory Management


• Once memory is allocated in the free
store, it can be access via the pointer
that operator new returns.
• To return memory to the free store use
the delete operator to deallocate (i.e.
release) it.

AS Dept. of Systems & Networking 7


CSNB244 Programming II with C++

Obtaining Dynamic Memory with new


• Consider the following statement:
Time *timePtr = new Time

• The new operator allocates storage of the proper size for an


object of type Time
– calls the default constructor to initialize the
object and returns a pointer to the type specified
to the right of the new operator (i.e. a Time *).
• If new is unable to find sufficient space in memory for the
object, it indicates that an error occurred by “throwing an
exception.”

AS Dept. of Systems & Networking 8


CSNB244 Programming II with C++

Obtaining Dynamic Memory with


delete
• To destroy a dynamically allocated object, use
the delete operator as follows:
• delete Timeptr;
• This statement first calls the destructor for the
object to which Timeptr points, then
deallocates the memory associated with the
object, returning the memory to the free store.

AS Dept. of Systems & Networking 9


CSNB244 Programming II with C++

continue

AS Dept. of Systems & Networking 10


CSNB244 Programming II with C++

Initializing Dynamic Memory


• an initializer can be provide for a newly created
fundamental-type variable, as in
• double *ptr = new double( 3.14159 );
– Which initializes a newly created double to 3.14159
and assigns the resulting pointer to ptr
• The same syntax can be used to specify a comma-
separated list of arguments to the constructor of
an object.
• Time *timePtr = new Time ( 12,45,0 );
– Which initializes a new Time object to 12:45 PM and
assigns the resulting pointer to timePtr
AS Dept. of Systems & Networking 11
CSNB244 Programming II with C++

Dynamic Allocating Arrays with new []


• the new operator can also be used to allocate arrays dynamically.
• For example, a 10-element integer array can be allocated and
assigned to gradesArray as follows:
• int *gradesArray = new int[ 10 ];
Which declares int pointer gradesArray and assigns to it a
pointer to the first element of a dynamically allocated 10-
element array of ints.
• A dynamically allocated array’s size can be specified using any
non-negative integral expression that can be evaluated at
execution time.
• Also, when allocating an array of objects dynamically, you
cannot pass arguments to each object’s constructor—each object
is initialized by its default constructor.

AS Dept. of Systems & Networking 12


CSNB244 Programming II with C++

Releasing Dynamic Allocated Arrays with delete


[]

• To deallocate a dynamically allocated array,


use the statement
• delete [] ptr;
• If the pointer points to an array of objects, the
statement first calls the destructor for every
object in the array, then deallocates the
memory.
• Using delete on a null pointer (i.e., a
pointer with the value 0) has no effect.
AS Dept. of Systems & Networking 13
CSNB244 Programming II with C++

AS Dept. of Systems & Networking 14

You might also like