week 9 part 1 - computer science | ucsb computer sciencekyledewey/cs16/week9/wee… ·  ·...

58
Week 9 Part 1 Kyle Dewey Tuesday, August 28, 12

Upload: truongnguyet

Post on 13-Apr-2018

218 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Week 9 Part 1Kyle Dewey

Tuesday, August 28, 12

Page 2: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Overview

• Dynamic allocation continued

• Heap versus stack

• Memory-related bugs

• Exam #2

Tuesday, August 28, 12

Page 3: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Dynamic Allocation

Tuesday, August 28, 12

Page 4: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Recall...

• Dynamic memory allocation allows us to request memory on the fly

• A way to use only what you need

// size_t is an integral defined// elsewherevoid* malloc( size_t numBytes );void* calloc( size_t num, size_t size );void* realloc( void* ptr, size_t size );

Tuesday, August 28, 12

Page 5: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Recall...

• Requesting some unknown number of integers at runtime

int numToAllocate;scanf( “%i”, &numToAllocate );int* nums = malloc(sizeof( int ) * numToAllocate);int nums2[ numToAllocate ]; // ERROR

Tuesday, August 28, 12

Page 6: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Multidimensional Arrays

• Need two separate allocations:

• Array of columns

• Each column individually

Tuesday, August 28, 12

Page 7: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Example• Function that makes a matrix with a given

number of rows and columns, all initialized to 0

int** makeMatrix( int rows, int cols ){ int** retval = calloc(rows, sizeof(int*)); int row; for( row = 0; row < rows; row++ ) { retval[ row ] = calloc(cols, sizeof(int)); } return retval;}Tuesday, August 28, 12

Page 8: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Question

• What differs here?

int** makeMatrix( int rows, int cols ){ int** retval = calloc(rows, sizeof(int*)); int* temp = calloc(cols, sizeof(int)); int row; for( row = 0; row < rows; row++ ) { retval[ row ] = temp; } return retval;}Tuesday, August 28, 12

Page 9: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

With Structs

• Works the same way

struct Foo {int x; int y;};int main() { struct Foo* f = malloc( sizeof( struct Foo ) ); f->x = 10; f->y = f->x; return 0;}

Tuesday, August 28, 12

Page 10: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

With Structs• Works the same way

struct Foo {int x; int y;};int main() { int x; struct Foo** fs = calloc( NUM_STRUCTS, sizeof( struct Foo* ) ); for(x = 0; x < NUM_STRUCTS; x++ ){ fs[ x ] = malloc( sizeof( struct Foo ) ); } return 0;}

Tuesday, August 28, 12

Page 11: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Example

• We want to calculate the sample standard deviation of some input numbers

• This formula is below:

Tuesday, August 28, 12

Page 12: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Problems

• We need the average in order to calculate it, so we need to keep the numbers around

• Can’t simply calculate as we go

• Don’t know how many numbers we need to read in

• Solution: Dynamic memory allocation!

Tuesday, August 28, 12

Page 13: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

stddev.c

Tuesday, August 28, 12

Page 14: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Heap vs. Stack

Tuesday, August 28, 12

Page 15: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Recall...• Say we have multiple function calls

int foo() { int x = 7; return x * 2;}int bar() { int y = 13; return y + foo();}int baz() { int z = 24; return z * 3;}

int main() { printf( “%i”, bar() ); return 0;}

Tuesday, August 28, 12

Page 16: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Recall...• How is this allocated in memory?

int foo() { int x = 7; return x * 2;}int bar() { int y = 13; return y + foo();}int baz() { int z = 24; return z * 3;}

int main() { printf( “%i”, bar() ); return 0;}

Tuesday, August 28, 12

Page 17: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

One Possibilityint foo() { int x = 7; return x * 2;}int bar() { int y = 13; return y + foo();}int baz() { int z = 24; return z * 3;}

x

y

z

Address

0

1

2

Tuesday, August 28, 12

Page 18: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Pros

• Simple

• Fast

x

y

z

Address

0

1

2

Tuesday, August 28, 12

Page 19: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Cons• Wasteful (z is allocated but is unused in

this code)

• Does not generally allow for recursion

int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact(n-1); }}

n

Address

0

Tuesday, August 28, 12

Page 20: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Another Possibility

• Use a stack

• As we call functions and declare variables, they get added to the stack

• As function calls end and variables are no longer alive, they get removed from the stack

• Do everything relative to the top of the stack

Tuesday, August 28, 12

Page 21: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Stackint foo() { int x = 7; return x * 2;}int bar() { int y = 13; return y + foo();}int baz() { int z = 24; return z * 3;}

int main() { printf( “%i”, bar() ); return 0;}

Address

Tuesday, August 28, 12

Page 22: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Stackint foo() { int x = 7; return x * 2;}int bar() { int y = 13; return y + foo();}int baz() { int z = 24; return z * 3;}

int main() { printf( “%i”, bar() ); return 0;}

Address

Tuesday, August 28, 12

Page 23: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Stackint foo() { int x = 7; return x * 2;}int bar() { int y = 13; return y + foo();}int baz() { int z = 24; return z * 3;}

int main() { printf( “%i”, bar() ); return 0;}

Address

Tuesday, August 28, 12

Page 24: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Stackint foo() { int x = 7; return x * 2;}int bar() { int y = 13; return y + foo();}int baz() { int z = 24; return z * 3;}

int main() { printf( “%i”, bar() ); return 0;}

Address

y TOS

Tuesday, August 28, 12

Page 25: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Stackint foo() { int x = 7; return x * 2;}int bar() { int y = 13; return y + foo();}int baz() { int z = 24; return z * 3;}

int main() { printf( “%i”, bar() ); return 0;}

Address

y TOS

Tuesday, August 28, 12

Page 26: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Stackint foo() { int x = 7; return x * 2;}int bar() { int y = 13; return y + foo();}int baz() { int z = 24; return z * 3;}

int main() { printf( “%i”, bar() ); return 0;}

Address

y TOS

Tuesday, August 28, 12

Page 27: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Stackint foo() { int x = 7; return x * 2;}int bar() { int y = 13; return y + foo();}int baz() { int z = 24; return z * 3;}

int main() { printf( “%i”, bar() ); return 0;}

Address

y TOS - 1

x TOS

Tuesday, August 28, 12

Page 28: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Stackint foo() { int x = 7; return x * 2;}int bar() { int y = 13; return y + foo();}int baz() { int z = 24; return z * 3;}

int main() { printf( “%i”, bar() ); return 0;}

Address

y TOS - 1

x TOS

Tuesday, August 28, 12

Page 29: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Stackint foo() { int x = 7; return x * 2;}int bar() { int y = 13; return y + foo();}int baz() { int z = 24; return z * 3;}

int main() { printf( “%i”, bar() ); return 0;}

Address

y TOS

Tuesday, August 28, 12

Page 30: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Stackint foo() { int x = 7; return x * 2;}int bar() { int y = 13; return y + foo();}int baz() { int z = 24; return z * 3;}

int main() { printf( “%i”, bar() ); return 0;}

Address

Tuesday, August 28, 12

Page 31: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Notice

• The function baz was never called, and the variable z was thusly never put on the stack

• At all points, only exact as much memory as was needed was used

Tuesday, August 28, 12

Page 32: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Recursion• Since stacks grow dynamically and allocate

on the fly, we can have recursion

int fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact(n-1); }}int main() { fact( 5 ); return 0;}

Tuesday, August 28, 12

Page 33: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Recursionint fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact(n-1); }}int main() { fact( 5 ); return 0;}

fact(5)n: 5 TOS

Tuesday, August 28, 12

Page 34: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Recursionint fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact(n-1); }}int main() { fact( 5 ); return 0;}

fact(5)n: 5

fact(4)n: 4 TOS

Tuesday, August 28, 12

Page 35: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Recursionint fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact(n-1); }}int main() { fact( 5 ); return 0;}

fact(5)n: 5

fact(4)n: 4

fact(3)n: 3 TOS

Tuesday, August 28, 12

Page 36: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Recursionint fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact(n-1); }}int main() { fact( 5 ); return 0;}

fact(5)n: 5

fact(4)n: 4

fact(3)n: 3

fact(2)n: 2 TOS

Tuesday, August 28, 12

Page 37: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Recursionint fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact(n-1); }}int main() { fact( 5 ); return 0;}

fact(5)n: 5

fact(4)n: 4

fact(3)n: 3

fact(2)n: 2

fact(1)n: 1 TOS

Tuesday, August 28, 12

Page 38: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Recursionint fact( int n ) { if ( n == 0 ) { return 1; } else { return n * fact(n-1); }}int main() { fact( 5 ); return 0;}

fact(5)n: 5

fact(4)n: 4

fact(3)n: 3

fact(2)n: 2

fact(1)n: 1

fact(0)n: 0 TOS

Tuesday, August 28, 12

Page 39: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Stack Pros/Cons

• Only memory that is required is allocated

• Can have recursion

• Slower (everything now relative to the top of the stack instead of absolute)

Tuesday, August 28, 12

Page 40: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Question

• Is there anything odd with this code?

int* doSomething() { int x; return &x;}int main() { int w; int* p = doSomething(); *p = 5; return 0;}

Tuesday, August 28, 12

Page 41: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Question Continuedint* doSomething() { int x; return &x;}int main() { int w; int* p = doSomething(); *p = 5; return 0;}

w TOS

Tuesday, August 28, 12

Page 42: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Question Continuedint* doSomething() { int x; return &x;}int main() { int w; int* p = doSomething(); *p = 5; return 0;}

w TOS

Tuesday, August 28, 12

Page 43: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Question Continuedint* doSomething() { int x; return &x;}int main() { int w; int* p = doSomething(); *p = 5; return 0;}

w TOS

Tuesday, August 28, 12

Page 44: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Question Continuedint* doSomething() { int x; return &x;}int main() { int w; int* p = doSomething(); *p = 5; return 0;}

x TOS

w TOS - 1

Tuesday, August 28, 12

Page 45: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Question Continuedint* doSomething() { int x; return &x;}int main() { int w; int* p = doSomething(); *p = 5; return 0;}

x TOS

w TOS - 1

Tuesday, August 28, 12

Page 46: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Question Continuedint* doSomething() { int x; return &x;}int main() { int w; int* p = doSomething(); *p = 5; return 0;}

w TOS

Tuesday, August 28, 12

Page 47: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Question Continuedint* doSomething() { int x; return &x;}int main() { int w; int* p = doSomething(); *p = 5; return 0;}

w TOS

Once TOS is updated, w’saddress is the same as x’s

address

Tuesday, August 28, 12

Page 48: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

In General

• Stack allocation is done automatically

• Pointers to places on the stack are ok, as long as you make sure the address will always be alive

• Pointers to the stack can be safely passed as function parameters, but not safely returned

Tuesday, August 28, 12

Page 49: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Heap

• Dynamic memory allocation allocates from a section of memory known as the heap

• Completely manual allocation

• Allocate when you need it

• Deallocate when you don’t

• The computer assumes you know what you’re doing

Tuesday, August 28, 12

Page 50: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Heap Allocation• Reconsider the code from before:

int* doSomething() {// int x;// return &x; return malloc( sizeof( int ) );}int main() { int w; int* p = doSomething(); *p = 5; return 0;}

Tuesday, August 28, 12

Page 51: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Heap Allocation

• This code is perfectly fine

• There is nothing that will be automatically reclaimed

• Can pass pointers any which way

Tuesday, August 28, 12

Page 52: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Heap Allocation Bugs

Tuesday, August 28, 12

Page 53: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Memory Leak

• Allocated memory is not freed after it is needed (wasted)

• Generally, no pointers exist to the portion allocated, and so it can never be reclaimed

• Why do we need a pointer?

void makeLeak() { malloc( sizeof( int ) );}

Tuesday, August 28, 12

Page 54: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Dangling Pointer• We keep a pointer to something that has

been freed

• That memory can do just about anything after it is freed

int* dangle() { int* p = malloc( sizeof( int ) ); free( p ); *p = 5; return p;}

Tuesday, August 28, 12

Page 55: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Double Free

• We called free on something that was already freed

void doubleFree() { int* p = malloc( sizeof( int ) ); free( p ); free( p );}

Tuesday, August 28, 12

Page 56: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

On Memory Bugs

• Determining when something can be freed is generally difficult

• Theoretically impossible to determine precisely in general

• Try to use stack allocation as much as possible

Tuesday, August 28, 12

Page 57: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Exam #2

Tuesday, August 28, 12

Page 58: Week 9 Part 1 - Computer Science | UCSB Computer Sciencekyledewey/cs16/week9/wee… ·  · 2012-08-28• A way to use only what you need // size_t is an integral defined // elsewhere

Statistics

• Average: 78

• Min: 52

• Max: 97

• A’s: 8

• B’s: 12

• C’s: 15

• D’s: 4

• F’s: 2

Tuesday, August 28, 12