Design and Analysis of Algorithms 214.
Introduction to the C Programming Language.
Dynamic Memory Allocation, Quick sort
Algorithm.
U.U.Samantha Rajapaksha
B.Sc.(Eng.)
Sri Lanka Institute of Information Technology.
[email protected] 0112301904
DAA 214 Sri Lanka Institute of Information Technology.
Dynamic memory allocation
In computer science, dynamic memory allocation is the allocation of
memory storage for use in a computer program during the runtime of
that program.
It is a way of distributing ownership of limited memory resources
among many pieces of data and code.
Importantly, the amount of memory allocated is determined by the
program at the time of allocation and need not be known in advance.
A dynamic allocation exists until it is explicitly released, either by the
programmer or by a garbage collector; this is notably different from
automatic and static memory allocation, which require advance
knowledge of the required amount of memory and have a fixed
duration.
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.
malloc
In computing, malloc is a subroutine provided in the
C programming language's and C++ programming
language's standard library for performing dynamic
memory allocation.
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.
Process in Memory
Command-line arguments and environment variables argc, argv, environment
stack activation records for funct
heap malloc family
Uninitialized static data
Initialized static data
Program text
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.
Dynamic memory allocation
Up to now, any variables, including pointers, that we’ve created
have been static:
They exist only while the module in which they’ve been
created is still executing
They disappear automatically upon exit from the module
We can create entities such as ints, chars, arrays and
complex data structures that will persist beyond exit from the
module that built them.
Dynamically allocated variables are created an area of
memory known as the heap.
Heap memory is only accessible through pointers.
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.
Dynamic memory allocation in C
The malloc function is the basic function used to allocate
memory on the heap in C.
Requires the use of pointer variables, and of one of the memory
allocation functions from <stdlib.h>
malloc: the most commonly used memory allocation function
void * malloc( size_t size );
Locates size consecutive bytes of free memory (memory that
is not currently in use) in the heap, and returns a generic
pointer to the block of memory
Returns NULL instead if size bytes can’t be found
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.
Dynamic memory allocation in C
malloc returns a void pointer (void *), which indicates that it is a
pointer to a region of unknown data type.
Value returned by malloc is a generic pointer, and must be cast
to the specific type of pointer the user intended to create
int * a;
a = (int *) (malloc( sizeof( int ) ) );
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.
Dynamic memory allocation in C
Memory allocated via malloc is persistent: it will continue to exist
until the program terminates or the memory is explicitly
deallocated by the programmer (that is, the block is said to be
"freed"). This is achieved by use of the free function.
Programmer is responsible for recycling any dynamically
allocated memory that is no longer needed
Must use the free( ) function from <stdlib.h>
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.
Exercise.
Modify the previous program (Exercise 06) with quick
sort algorithm to sort the elements of the array.
Procedure QUICKSORT (A,p,r)
1 if p < r
2 then q ← PARTITION(A,p,r)
3 QUICKSORT (A,p,q-1)
4 QUICKSORT (A,q+1,r)
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.
PARTITION(A, p, r)
1 x ← A[r]
2i←p-1
3 for j ← p to r - 1
4 do if A[j] ≤ x
5 then i ← i + 1
6 exchange A[i] ↔ A[j]
7 exchange A[i + 1] ↔ A[r]
8 return i + 1
DAA 214 Lab 02 Sri Lanka Institute of Information Technology.