programming for engineers dynamic memory allocation
play

Programming for Engineers Dynamic Memory Allocation ICEN 200 - PowerPoint PPT Presentation

Programming for Engineers Dynamic Memory Allocation ICEN 200 Spring 2018 Prof. Dola Saha 1 A Running Programs Memory 0x7FFFFFFFFFFF Unused Created at runtime User Stack 47 bits of address space Shared among processes Shared


  1. Programming for Engineers Dynamic Memory Allocation ICEN 200 – Spring 2018 Prof. Dola Saha 1

  2. A Running Program’s Memory 0x7FFFFFFFFFFF Unused Created at runtime User Stack 47 bits of address space Shared among processes Shared Libraries Created at runtime Heap Read/Write Data Loaded from the executable Read-only Code and Data Unused 0x000000000000 Memory Allocation 2

  3. Allocation Ø For all data, memory must be allocated § Allocated = memory space reserved Ø Two questions: § When do we know the size to allocate? § When do we allocate? Ø Two possible answers for each: § Compile-time ( static ) § Run-time ( dynamic ) 3

  4. How much memory to allocate? Sometimes obvious: Ø One byte char c; int array[10]; 10 * sizeof(int) (= 40 on CLEAR) Sometimes not: Ø Is this going to point to one character or a string? char *c; int *array; How big will this array be? § How will these be used??? Will they point to already allocated memory (what we � ve seen so far)? o Will new memory need to be allocated (we haven � t seen this yet)? o 4

  5. Dynamic Memory Allocation Ø Creating and maintaining dynamic data structures requires dynamic memory allocation—the ability for a program to obtain more memory space at execution time to hold new nodes, and to release space no longer needed . Ø Functions malloc and free, and operator sizeof, are essential to dynamic memory allocation. 5

  6. Memory Use Ø heap § region of memory in which function malloc dynamically allocates blocks of storage Ø stack § region of memory in which function data areas are allocated and reclaimed 6

  7. malloc() Ø void * malloc (size_t size) Ø Input: number of bytes to be allocated Ø Output: a pointer of type void * (pointer to void) to the allocated memory. Ø A void * pointer may be assigned to a variable of any pointer type. Ø Example: newPtr = malloc( sizeof(int)); Ø The allocated memory is not initialized. Ø If no memory is available, malloc returns NULL. 7

  8. free() Ø Function free deallocates memory—i.e., the memory is returned to the system so that it can be reallocated in the future. Ø To free memory dynamically allocated by the preceding malloc call, use the statement o free(newPtr); Ø C also provides functions calloc and realloc for creating and modifying dynamic arrays . 8

  9. calloc() and realloc() Ø calloc() § Allocates memory and cleares it to 0. § void * calloc (size_t count, size_t eltsize) Ø realloc() § Make a block previously allocated by malloc larger or smaller, possibly by copying it to a new location. § void *realloc (void * addr , size_t size ) 9

  10. Dynamic Memory Allocation 10

  11. Memory Allocation (1) 11

  12. Memory Allocation (2) 12

  13. Memory Allocation (3) 13

  14. Dynamic Memory Allocation with calloc() 14

  15. Dynamic Memory Allocation with calloc() 15

  16. Memory Functions 16

  17. memcpy() 17

  18. memmove() 18

  19. memcmp() 19

  20. memchr() 20

  21. memset() 21

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend