13
+ Dynamic memory allocation

+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

Embed Size (px)

Citation preview

Page 1: + Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

+

Dynamic memory allocation

Page 2: + Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

+Introduction

We often face situations in programming where the data is dynamics in nature.

Consider a list of customers. When list grows we need to allocate more memory space to the list to accommodate additional data items.

Such situation can be handled by using dynamic memory management.

Page 3: + Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

+Dynamics memory allocation C language requires the number of elements in an array

to be specified at compile time. But if we wrong with initial guess of number of elements? It may cause failure of the program (not enough elements) It can was memory space (to many unused elements)

The solution is to allocate memory at run time (dynamic memory allocation).

C does not have such functionality, but there four library routines that allows to allocate memory dynamically.

Function

Task

malloc Allocate request size of bytes and return a pointer to the first byte

calloc Same as malloc but initialize memory to zero

free Frees previously allocated space

realloc Modifies the sizes of previously allocated space

Page 4: + Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

+Memory allocation process

Memory allocation process associated with a C program

The program instruction, global and static variables are stored in permanent storage area.

Local variables are stored in stack.

The free memory is called the heap. The size is changing, it is possible to encounter memory “overflow” during dynamic allocation process.

Local variables

Free memory

Global variables

C program instruction

stack

heap

permanent storage area

Page 5: + Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

+Allocating a block of memory: malloc

The malloc function reserves a block of memory of specified size and return a pointer of type void.

ptr = (cast-type *) malloc(byte-size);

x = (int *) malloc(100 * sizeof(int));

cptr = (char *) malloc(10);

On successful execution, a memory space will be allocated.

cptrAddress

of first byte 10 bytes of

space The malloc allocates a block of contiguous bytes.

Page 6: + Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

+Example The program uses a table of integers whose size will be

specified interactively at run time.

Page 7: + Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

+Allocating multiple blocks of memory: calloc

calloc allocates multiple blocks of storage, each of the same size, and then sets all bytes to zero.

The general form of calloc is

ptr = (cast-type *) calloc (n, elem-size);

If there is not enough space, a NULL pointer is returned.

Page 8: + Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

+Releasing the used space: free Compile-time storage of a variable is allocated and released

by the system in accordance with its storage class.

With the dynamics run-time allocation, it is our responsibility to release the space when it is not required. The release of storage space becomes important when the storage is limited.

When we no longer need the data we stored in a block memory, we may release that block of memory for future use

free

free

free

free

free

occupied free

free

free

free

free

free

free

free

free

free

free

free

free(ptr);before

after

Page 9: + Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

+Altering the size of a block: realloc

It likely that we discover later: The previously allocated memory is not sufficient (add more

space) The memory allocated is much larger that necessary (reduce

space)

We can changed allocated memory with the function realloc.

ptr = malloc(size);

ptr = realloc(ptr, newsize);

The newsize maybe large or smaller than the size.

Page 10: + Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

+Example The program stores a character string in a block of memory

space created by malloc and then modify the same to store a larger string.

Page 11: + Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

+Dynamically allocating Multidimensional arrays

it's straightforward to call malloc to allocate a block of memory which can simulate an array, but with a size which we get to pick at run-time.

Can we do the same sort of thing to simulate multidimensional arrays?

We want to simulate an array of pointers, but we don't know how many rows there will be, either, so we'll have to simulate that array (of pointers) with another pointer, and this will be a pointer to a pointer.

Page 12: + Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

+ Example

Page 13: + Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers

+Dynamically allocating Multidimensional arrays If a program uses simulated, dynamically allocated multidimensional

arrays, it becomes possible to write ``heterogeneous'' functions which don't have to know (at compile time) how big the ``arrays'' are. One function can operate on ``arrays'' of various sizes and shapes.

func(int **array, int nrows, int ncolumns){}

Example

To free one of these dynamically allocated multidimensional ``arrays,'' we must remember to free each of the chunks of memory that we've allocated