1 / 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. When list grows we need to allocate more memory space to the list to accommodate additional data items.

jaunie
Download Presentation

Dynamic memory allocation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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.

  4. Memory allocation process • Memory allocation process associated with a C program stack heap permanent storage area • 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.

  5. Allocating a block of memory: malloc • The mallocfunction 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. Address of first byte 10 bytes of space • The mallocallocates 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 • callocallocates 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 before free(ptr); 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 mallocand then modify the same to store a larger string.

  11. Dynamically allocating Multidimensional arrays • it's straightforward to call mallocto 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, intnrows, intncolumns){} • 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

More Related