Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers."— Presentation transcript:

1 + Dynamic memory allocation

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

3 + 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. FunctionTask mallocAllocate request size of bytes and return a pointer to the first byte callocSame as malloc but initialize memory to zero freeFrees previously allocated space reallocModifies the sizes of previously allocated space

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

5 + 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. cptr Address of first byte 10 bytes of space The malloc allocates a block of contiguous bytes.

6 + Example The program uses a table of integers whose size will be specified interactively at run time.

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

8 + 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 occupiedfree free(ptr); before after

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

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

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

12 + Example

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


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

Similar presentations


Ads by Google