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

Similar documents
CS24 Week 2 Lecture 1

In Java we have the keyword null, which is the value of an uninitialized reference type

CS 11 C track: lecture 5

APS105. Malloc and 2D Arrays. Textbook Chapters 6.4, Datatype Size

Memory Management. CSC215 Lecture

LOÏC CAPPANERA. 1. Memory management The variables involved in a C program can be stored either statically or dynamically.

Intermediate Programming, Spring 2017*

Memory (Stack and Heap)

Heap Arrays and Linked Lists. Steven R. Bagley

o Code, executable, and process o Main memory vs. virtual memory

CS64 Week 5 Lecture 1. Kyle Dewey

CS 137 Part 5. Pointers, Arrays, Malloc, Variable Sized Arrays, Vectors. October 25th, 2017

CA31-1K DIS. Pointers. TA: You Lu

Dynamic Data Structures. CSCI 112: Programming in C

POINTER AND ARRAY SUNU WIBIRAMA

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Dynamic Memory: Alignment and Fragmentation

Subject: Fundamental of Computer Programming 2068

CSC 1600 Memory Layout for Unix Processes"

CS 222: Pointers and Manual Memory Management

Kurt Schmidt. October 30, 2018

Week 6 Part 1. Kyle Dewey. Monday, July 30, 12

Heap Arrays. Steven R. Bagley

Dynamic memory allocation

Memory Allocation in C C Programming and Software Tools. N.C. State Department of Computer Science

CS61C Midterm Review on C & Memory Management

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

Fall 2018 Discussion 2: September 3, 2018

Lecture 8 Dynamic Memory Allocation

Common Misunderstandings from Exam 1 Material

CS61C : Machine Structures

Programming. Pointers, Multi-dimensional Arrays and Memory Management

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Binghamton University. CS-211 Fall Dynamic Memory

Memory Management I. two kinds of memory: stack and heap

CS113: Lecture 9. Topics: Dynamic Allocation. Dynamic Data Structures

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CPSC 213. Introduction to Computer Systems. Winter Session 2017, Term 2. Unit 1c Jan 24, 26, 29, 31, and Feb 2

Dynamic Allocation in C

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Programs in memory. The layout of memory is roughly:

advanced data types (2) typedef. today advanced data types (3) enum. mon 23 sep 2002 defining your own types using typedef

Memory Management. CS449 Fall 2017

Dynamic Memory Allocation

Dynamic memory. EECS 211 Winter 2019

Arrays and Memory Management

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

DAY 3. CS3600, Northeastern University. Alan Mislove

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

Dynamic Memory Allocation and Linked Lists

Review! Lecture 5 C Memory Management !

THE GOOD, BAD AND UGLY ABOUT POINTERS. Problem Solving with Computers-I

CS 31: Intro to Systems Pointers and Memory. Kevin Webb Swarthmore College October 2, 2018

CS61C : Machine Structures

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

CP2 Revision. theme: dynamic datatypes & data structures

Lectures 13 & 14. memory management

Lecture 14 Notes. Brent Edmunds


Structures and Pointers

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

CPSC 213, Winter 2016, Term 2 Final Exam Solution Date: April 19, 2017; Instructor: Mike Feeley and Alan Wagner

CS61C : Machine Structures

CS61C : Machine Structures

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

Dynamically Allocated Memory in C

ECE 15B COMPUTER ORGANIZATION

Dynamic Allocation of Memory

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

My malloc: mylloc and mhysa. Johan Montelius HT2016

Example: Runtime Memory Allocation: Example: Dynamical Memory Allocation: Some Comments: Allocate and free dynamic memory

Memory Leak. C++: Memory Problems. Memory Leak. Memory Leak. Pointer Ownership. Memory Leak

CS61C : Machine Structures

Dynamic Memory Management

CSci 4061 Introduction to Operating Systems. Programs in C/Unix

COMP26120: Linked List in C (2018/19) Lucas Cordeiro

Limitations of the stack

Lecture 7 More Memory Management Slab Allocator. Slab Allocator

Programming Language Implementation

CPSC 213. Introduction to Computer Systems. Instance Variables and Dynamic Allocation. Unit 1c

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Lecture 7: Procedures and Program Execution Preview

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Dynamic memory allocation (malloc)

C Pointers. ENGG1002 Computer Programming and Applica;ons Dr. Hayden Kwok Hay So Week 2. GeGng Hold of Memory. Sta;c: When we define a variable

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

First of all, it is a variable, just like other variables you studied

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Announcements. assign0 due tonight. Labs start this week. No late submissions. Very helpful for assign1

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

Class Information ANNOUCEMENTS

CS 241 Honors Memory

BBM 201 DATA STRUCTURES

Data Representation and Storage

ANITA S SUPER AWESOME RECITATION SLIDES

Lecture 13: Complex Types and Garbage Collection

Actually, C provides another type of variable which allows us to do just that. These are called dynamic variables.

Transcription:

Week 9 Part 1 Kyle Dewey

Overview Dynamic allocation continued Heap versus stack Memory-related bugs Exam #2

Dynamic Allocation

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 // elsewhere void* malloc( size_t numbytes ); void* calloc( size_t num, size_t size ); void* realloc( void* ptr, size_t size );

Recall... Requesting some unknown number of integers at runtime int numtoallocate; scanf( %i, &numtoallocate ); int* nums = malloc(sizeof( int ) * numtoallocate); int nums2[ numtoallocate ]; // ERROR

Multidimensional Arrays Need two separate allocations: Array of columns Each column individually

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;

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;

With Structs Works the same way struct Foo {int x; int y;; struct Foo* f = malloc( sizeof( struct Foo ) ); f->x = 10; f->y = f->x;

With Structs Works the same way struct Foo {int x; int y;; int x; struct Foo** fs = calloc( NUM_STRUCTS, sizeof( struct Foo* ) ); for(x = 0; x < NUM_STRUCTS; x++ ){ fs[ x ] = malloc( sizeof( struct Foo ) );

Example We want to calculate the sample standard deviation of some input numbers This formula is below:

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!

stddev.c

Heap vs. Stack

Recall... Say we have multiple function calls int foo() { int x = 7; return x * 2; int bar() { int y = 13; return y + foo(); printf( %i, bar() ); int baz() { int z = 24; return z * 3;

Recall... How is this allocated in memory? int foo() { int x = 7; return x * 2; int bar() { int y = 13; return y + foo(); printf( %i, bar() ); int baz() { int z = 24; return z * 3;

One Possibility int foo() { int x = 7; Address return x * 2; x 0 int bar() { int y = 13; return y + foo(); int baz() { int z = 24; return z * 3; y z 1 2

Pros Address Simple Fast x y 0 1 z 2

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

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

Stack int foo() { int x = 7; return x * 2; int bar() { printf( %i, bar() ); int y = 13; return y + foo(); Address int baz() { int z = 24; return z * 3;

Stack int foo() { int x = 7; return x * 2; int bar() { printf( %i, bar() ); int y = 13; return y + foo(); Address int baz() { int z = 24; return z * 3;

Stack int foo() { int x = 7; return x * 2; int bar() { printf( %i, bar() ); int y = 13; return y + foo(); Address int baz() { int z = 24; return z * 3;

Stack int foo() { int x = 7; return x * 2; int bar() { printf( %i, bar() ); int y = 13; return y + foo(); Address int baz() { int z = 24; return z * 3; y TOS

Stack int foo() { int x = 7; return x * 2; int bar() { printf( %i, bar() ); int y = 13; return y + foo(); Address int baz() { int z = 24; return z * 3; y TOS

Stack int foo() { int x = 7; return x * 2; int bar() { printf( %i, bar() ); int y = 13; return y + foo(); Address int baz() { int z = 24; return z * 3; y TOS

Stack int foo() { int x = 7; return x * 2; int bar() { printf( %i, bar() ); int y = 13; return y + foo(); Address int baz() { int z = 24; x TOS return z * 3; y TOS - 1

Stack int foo() { int x = 7; return x * 2; int bar() { printf( %i, bar() ); int y = 13; return y + foo(); Address int baz() { int z = 24; x TOS return z * 3; y TOS - 1

Stack int foo() { int x = 7; return x * 2; int bar() { printf( %i, bar() ); int y = 13; return y + foo(); Address int baz() { int z = 24; return z * 3; y TOS

Stack int foo() { int x = 7; return x * 2; int bar() { printf( %i, bar() ); int y = 13; return y + foo(); Address int baz() { int z = 24; return z * 3;

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

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); fact( 5 );

Recursion int fact( int n ) { if ( n == 0 ) { return 1; else { return n * fact(n-1); fact( 5 ); fact(5) n: 5 TOS

Recursion int fact( int n ) { if ( n == 0 ) { return 1; else { return n * fact(n-1); fact( 5 ); fact(4) n: 4 TOS fact(5) n: 5

Recursion int fact( int n ) { if ( n == 0 ) { return 1; else { return n * fact(n-1); fact(3) n: 3 TOS fact( 5 ); fact(4) n: 4 fact(5) n: 5

Recursion int fact( int n ) { if ( n == 0 ) { return 1; else { return n * fact(n-1); fact(2) n: 2 TOS fact(3) n: 3 fact( 5 ); fact(4) n: 4 fact(5) n: 5

Recursion int fact( int n ) { if ( n == 0 ) { return 1; else { return n * fact(n-1); fact(1) n: 1 fact(2) n: 2 TOS fact(3) n: 3 fact( 5 ); fact(4) n: 4 fact(5) n: 5

Recursion int fact( int n ) { if ( n == 0 ) { return 1; else { return n * fact(n-1); fact(0) n: 0 fact(1) n: 1 fact(2) n: 2 TOS fact(3) n: 3 fact( 5 ); fact(4) n: 4 fact(5) n: 5

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)

Question Is there anything odd with this code? int* dosomething() { int x; return &x; int w; int* p = dosomething(); *p = 5;

Question Continued int* dosomething() { int x; return &x; int w; int* p = dosomething(); *p = 5; w TOS

Question Continued int* dosomething() { int x; return &x; int w; int* p = dosomething(); *p = 5; w TOS

Question Continued int* dosomething() { int x; return &x; int w; int* p = dosomething(); *p = 5; w TOS

Question Continued int* dosomething() { int x; return &x; x TOS int w; w TOS - 1 int* p = dosomething(); *p = 5;

Question Continued int* dosomething() { int x; return &x; x TOS int w; w TOS - 1 int* p = dosomething(); *p = 5;

Question Continued int* dosomething() { int x; return &x; int w; int* p = dosomething(); *p = 5; w TOS

Question Continued int* dosomething() { int x; return &x; int w; int* p = dosomething(); *p = 5; w TOS Once TOS is updated, w s address is the same as x s address

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

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

Heap Allocation Reconsider the code from before: int* dosomething() { // int x; // return &x; return malloc( sizeof( int ) ); int w; int* p = dosomething(); *p = 5;

Heap Allocation This code is perfectly fine There is nothing that will be automatically reclaimed Can pass pointers any which way

Heap Allocation Bugs

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 ) );

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;

Double Free We called free on something that was already freed void doublefree() { int* p = malloc( sizeof( int ) ); free( p ); free( p );

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

Exam #2

Statistics A s: 8 Average: 78 Min: 52 Max: 97 B s: 12 C s: 15 D s: 4 F s: 2