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

programming for engineers dynamic memory allocation
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

1

Programming for Engineers Dynamic Memory Allocation

ICEN 200 – Spring 2018

  • Prof. Dola Saha
slide-2
SLIDE 2

2

A Running Program’s Memory

Memory Allocation Unused User Stack Shared Libraries Heap Read/Write Data Read-only Code and Data Unused

0x7FFFFFFFFFFF 0x000000000000

Loaded from the executable Created at runtime Created at runtime Shared among processes 47 bits of address space

slide-3
SLIDE 3

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)

slide-4
SLIDE 4

4

How much memory to allocate?

Ø

Sometimes obvious:

Ø

Sometimes not:

§ How will these be used???

  • Will they point to already allocated memory (what weve seen so far)?
  • Will new memory need to be allocated (we havent seen this yet)?

char c; int array[10];

One byte 10 * sizeof(int) (= 40 on CLEAR)

char *c; int *array;

Is this going to point to one character or a string? How big will this array be?

slide-5
SLIDE 5

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.

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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.

slide-8
SLIDE 8

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

  • free(newPtr);

Ø C also provides functions calloc and realloc for creating

and modifying dynamic arrays.

slide-9
SLIDE 9

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)

slide-10
SLIDE 10

10

Dynamic Memory Allocation

slide-11
SLIDE 11

11

Memory Allocation (1)

slide-12
SLIDE 12

12

Memory Allocation (2)

slide-13
SLIDE 13

13

Memory Allocation (3)

slide-14
SLIDE 14

14

Dynamic Memory Allocation with calloc()

slide-15
SLIDE 15

15

Dynamic Memory Allocation with calloc()

slide-16
SLIDE 16

16

Memory Functions

slide-17
SLIDE 17

17

memcpy()

slide-18
SLIDE 18

18

memmove()

slide-19
SLIDE 19

19

memcmp()

slide-20
SLIDE 20

20

memchr()

slide-21
SLIDE 21

21

memset()