StrukturData 06 Memory Allocation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Mata Kuliah : Algoritma dan Struktur Data

Tahun Ajaran : 2019/2020

Memory Allocation
Tue, Nov 5, 2019

FAKULTAS TEKNOLOGI
INFORMASI
UNIVERSITAS ADVENT INDONESIA
What is Memory Allocation
• Memory allocation is the process of setting aside
sections of memory in a program to be used to store
variables, and instances of structures and classes.
• There are two ways via which memories can be
allocated for storing data:
1. Static allocation
2. Dynamic allocation

FAKULTAS TEKNOLOGI INFORMASI


UNIVERSITAS ADVENT INDONESIA
Compile-time or Static allocation
Compile time allocation or static allocation of
memory: where the memory for named variables is
allocated by the compiler. Exact size and storage must be
known at compile time and for array declaration, the
size has to be constant.

int x, y;
float a[5];

FAKULTAS TEKNOLOGI INFORMASI


UNIVERSITAS ADVENT INDONESIA
Dynamic memory allocation
Runtime allocation or dynamic allocation of
memory: where the memory is allocated at runtime and
the allocation of memory space is done dynamically
within the program run and the memory segment is
known as a heap or the free store. In this case, the exact
space or number of the item does not have to be known
by the compiler in advance. Pointers play a major role in
this case.

FAKULTAS TEKNOLOGI INFORMASI


UNIVERSITAS ADVENT INDONESIA
Dynamic memory allocation
Dynamic allocation requires two criteria:
1. Creating the dynamic space in memory
2. Storing its address in a pointer (so that space can be
accessed)
Memory de-allocation is also a part of this concept
where the "clean-up" of space is done for variables or
other data storage. It is the job of the programmer to de-
allocate dynamically created space. For de-allocating
dynamic memory, we use the delete operator. In other
words, dynamic memory Allocation refers to performing
memory management for dynamic memory allocation
manually.
FAKULTAS TEKNOLOGI INFORMASI
UNIVERSITAS ADVENT INDONESIA
Dynamic memory allocation
The major difference between static and dynamic
memory allocations are:

FAKULTAS TEKNOLOGI INFORMASI


UNIVERSITAS ADVENT INDONESIA
Dynamic memory allocation
C++ defines two unary operators new and delete that
perform the task of allocating and deallocating memory
during runtime. Since these operators (new and delete)
operate upon free store memory (Heap memory), they
are also called free store operator. Pointers provide the
necessary support for dynamic memory allocation
system in C++.

FAKULTAS TEKNOLOGI INFORMASI


UNIVERSITAS ADVENT INDONESIA
Allocation of Memory using new Keyword
• In C++ the new operator is used to allocate memory at
runtime and the memory is allocated in bytes.
The new operator denotes a request for dynamic memory
allocation on the Heap. If sufficient memory is available
then the new operator initializes the memory and returns
the address of the newly allocated and initialized memory
to the pointer variable.
• Syntax:
datatype *pointer_variable = new datatype;
OR
datatype *pointer_variable;
pointer_variable = new datatype;
OR
pointer-variable = new data-type[size];
FAKULTAS TEKNOLOGI INFORMASI
UNIVERSITAS ADVENT INDONESIA
What if enough memory is not available during
runtime?
• If enough memory is not available in the heap to
allocate, the new request indicates failure by throwing
an exception of type std::bad_alloc, unless “nothrow” is
used with the new operator, in which case it returns a
NULL pointer. Therefore, it may be good idea to check
for the pointer variable produced by new before using
it program.
• Example:
int *p = new(nothrow) int;
if (!p) {
cout << "Memory allocation failed\n";
}
FAKULTAS TEKNOLOGI INFORMASI
UNIVERSITAS ADVENT INDONESIA
Deallocation of memory using delete Keyword
• Once heap memory is allocated to a variable or class
object using the new keyword, we can deallocate that
memory space using the delete keyword.
• Example:
delete pointer_variable;
//pointer_variable is the pointer that points
//to the data object created by new.
delete[] pointer_variable;
//To free the dynamically allocated array
//memory pointed by pointer-variable we use
//the following form of delete:

FAKULTAS TEKNOLOGI INFORMASI


UNIVERSITAS ADVENT INDONESIA
Memory allocation using malloc ()
• “malloc” or “memory allocation” method is used to
dynamically allocate a single large block of memory
with the specified size. It returns a pointer of type void
which can be cast into a pointer of any form.
Syntax:
ptr = (cast-type*) malloc(byte-size)

FAKULTAS TEKNOLOGI INFORMASI


UNIVERSITAS ADVENT INDONESIA
Memory allocation using calloc ()
• “calloc” or “contiguous allocation” method is used to
dynamically allocate the specified number of blocks of
memory of the specified type. It initializes each block
with a default value ‘0’.
Syntax:
ptr = (cast-type*)calloc(n, element-size);

FAKULTAS TEKNOLOGI INFORMASI


UNIVERSITAS ADVENT INDONESIA
Deallocating Memory Using Free
• “free” method is used to dynamically de-allocate the memory.
The memory allocated using functions malloc() and calloc() are
not de-allocated on their own. Hence the free() method is used,
whenever the dynamic memory allocation takes place. It helps
to reduce wastage of memory by freeing it.
• Syntax:

free(ptr);

FAKULTAS TEKNOLOGI INFORMASI


UNIVERSITAS ADVENT INDONESIA

You might also like