Docsity

Memory Allocation - Introduction to Computer Systems - Lecture Slides, Slides of Computer System Design and Architecture

Partial preview of the text

Download Memory Allocation - Introduction to Computer Systems - Lecture Slides and more Slides Computer System Design and Architecture in PDF only on Docsity!

Memory Allocation

Objectives

Be able to recognize the differences between

static and dynamic memory allocation

Be able to use malloc() and free() to manage

dynamic memory in your programs

Be able to analyze programs for memory

management related bugs

Process Memory

Unused

User Stack

Shared Libraries

Heap

Read/Write Data

Read-only Code and Data

Unused

0x7FFFFFFFFFFF

0x

Loaded from the executable

Created at runtime

Created at runtime

Shared among processes

47 bits of address space

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 )

malloc()

Allocate memory dynamically

 Pass a size (number of bytes to allocate)

  • Finds unused memory that is large enough to hold the

specified number of bytes and reserves it

 Returns a void * that points to the allocated

memory

  • No typecast is needed for pointer assignment

 Essentially equivalent to new in Java and C++

#include <stdlib.h>

int *array = malloc(num_items * sizeof(int));

Won’t continually remind you of this

Using malloc()

i and array are interchangeable

 Arrays  pointers to the initial (0th) array element

 i could point to an array, as well

 May change over the course of the program

Allocated memory is not initialized!

 calloc zeroes allocated memory (otherwise, same

as malloc ; details to come in lab)

int *i;

int *array;

i = malloc(sizeof(int));

array = malloc(num_items * sizeof(int));

*i = 3;

array[3] = 5;

Statically allocates space for 2 pointers Dynamically allocates space for data

When to Allocate?

Static time

 Typically global

variables:

 Only one copy ever

exists, so can allocate at

compile-time

Dynamic time

 Typically local variables:

 One copy exists for each

call – may be unbounded

# of calls, so can’t

allocate at compile-time

int value; int main(void) { … }

int f(…) { int value; … } int main(void) { … }

When to Allocate?

Static time

 Some local variables:

int f(…) { static int value; … } int main(void) { … }

Confusingly, local static

has nothing to do with

global static!

One copy exists for

all calls – allocated

at compile-time

Deallocation

Space allocated via declaration (entering

scope) is deallocated when exiting scope

 Can’t refer to y or array outside of f() , so their

space is deallocated upon return

… f(void) { int y; int array[10]; … }

Deallocation

malloc() allocates memory explicitly

 Must also deallocate it explicitly (using free() )!

 Not automatically deallocated (garbage collected)

as in Python, Scheme, and Java

 Forgetting to deallocate leads to memory leaks &

running out of memory

Must not use a freed pointer unless reassigned

or reallocated

int *a = malloc(num_items * sizeof(int)); … free(a); … a = malloc(2 * num_items * sizeof(int));

Back to create_date

Date * create_date3(int month, int day, int year) { Date *d;

d->month = month; d->day = day; d->year = year;

return (d); }

Date * create_date3(int month, int day, int year) { Date *d;

d = malloc(sizeof(Date));

d->month = month; d->day = day; d->year = year;

return (d); }

Pitfall

void foo(void) { Date *today;

today = create_date3(9, 1, 2005);

/ use “today” / ...

return; }

Memory is still allocated for “today”!

Will never be deallocated (calling

function doesn’t even know about it)

Potential problem: memory

allocation is performed in

this function (may not

know its implementation)

Common Memory Management Mistakes

What’s Wrong With This Code?

Consider j = *f()

Leads to referencing deallocated memory

 Never return a pointer to a local variable!

Behavior depends on allocation pattern

 Space not reallocated (unlikely)  works

 Space reallocated  very unpredictable

*int make_array(…) { int array[10]; … return (array); }

*int f(…) { int i; … return (&i); }