The Heap and Structs. CSE 333 Spring Instructor: Justin Hsia

Similar documents
CSE 333. Lecture 4 - malloc, free, struct, typedef. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

CSE 333 Lecture 4 - malloc, free, struct, typedef

Systems Programming and Computer Architecture ( )

Memory and Arrays. CSE 333 Spring Instructor: Justin Hsia

Concurrency and Threads

Server-side Programming

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

CS 11 C track: lecture 5

C++ Constructor Insanity

Pointers, Pointers, Pointers

C++ Inheritance I. CSE 333 Spring Instructor: Justin Hsia

C++ Inheritance II, Casting

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

Pointers, Pointers, Pointers

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

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

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

CS 61C: Great Ideas in Computer Architecture. C Arrays, Strings, More Pointers

Lecture 8 Dynamic Memory Allocation

CSE 333 Lecture 2 - arrays, memory, pointers

Class Information ANNOUCEMENTS

ECE 15B COMPUTER ORGANIZATION

Arrays and Memory Management

CS61C : Machine Structures

Programs in memory. The layout of memory is roughly:

CSE 333 Lecture 3 - arrays, memory, pointers

CS61C : Machine Structures

Review! Lecture 5 C Memory Management !

CS61C : Machine Structures

CS61, Fall 2012 Section 2 Notes

CSC 1600 Memory Layout for Unix Processes"

CS 61c: Great Ideas in Computer Architecture

CMSC 341 Lecture 2 Dynamic Memory and Pointers

EL2310 Scientific Programming

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

Memory Organization. The machine code and data associated with it are in the code segment

Memory Allocation III

CS61C : Machine Structures

CS 61C: Great Ideas in Computer Architecture. Memory Management and Usage

6.S096 Lecture 2 Subtleties of C

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Dynamic Data Structures (II)

Intro to C: Pointers and Arrays

Intermediate Programming, Spring 2017*

Pointers, Arrays, Memory: AKA the cause of those Segfaults

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Memory Allocation III

HW1 due Monday by 9:30am Assignment online, submission details to come

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

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

Dynamic memory allocation (malloc)

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

Introduction to C. Robert Escriva. Cornell CS 4411, August 30, Geared toward programmers

Dynamic Data Structures. CSCI 112: Programming in C

Lectures 13 & 14. memory management

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

Lecture Notes on Memory Management

DAY 3. CS3600, Northeastern University. Alan Mislove

CS 61C: Great Ideas in Computer Architecture C Memory Management, Usage Models

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

C for Java Programmers 1. Last Week. Overview of the differences between C and Java. The C language (keywords, types, functies, etc.

CS61C Midterm Review on C & Memory Management

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

Memory Allocation. Static Allocation. Dynamic Allocation. Dynamic Storage Allocation. CS 414: Operating Systems Spring 2008

CS61C : Machine Structures

Systems Programming and Computer Architecture ( )

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

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

CSCI-1200 Data Structures Spring 2016 Lecture 6 Pointers & Dynamic Memory

The output: The address of i is 0xbf85416c. The address of main is 0x80483e4. arrays.c. 1 #include <stdio.h> 3 int main(int argc, char **argv) 4 {

Memory Allocation III

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

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

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

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

More C Pointer Dangers

CS 31: Intro to Systems Pointers and Memory. Martin Gagne Swarthmore College February 16, 2016

CS61C : Machine Structures

Reference slides! C Strings! A string in C is just an array of characters.!!!char string[] = "abc";! How do you tell how long a string is?!

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

CS61C : Machine Structures

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

Dynamic Memory Allocation

CS61C : Machine Structures

Vector and Free Store (Pointers and Memory Allocation)

Number Review. Lecture #3 More C intro, C Strings, Arrays, & Malloc Variables. Clarification about counting down

377 Student Guide to C++

Lecture Notes on Memory Management

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

Memory Allocation I. CSE 351 Autumn Instructor: Justin Hsia

IP Addresses, DNS. CSE 333 Spring Instructor: Justin Hsia

CS 314 Principles of Programming Languages. Lecture 11

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

a data type is Types

Motivation was to facilitate development of systems software, especially OS development.

Pointers and Memory Management

Dynamic Allocation in C

Kurt Schmidt. October 30, 2018

Reference slides! Garcia, Fall 2011 UCB! CS61C L04 Introduction to C (pt 2) (1)!

SYSC 2006 C Winter 2012

Transcription:

The Heap and Structs CSE 333 Spring 2018 Instructor: Justin Hsia Teaching Assistants: Danny Allen Dennis Shao Eddie Huang Kevin Bi Jack Xu Matthew Neldam Michael Poulain Renshu Gu Robby Marver Waylon Huang Wei Lin

Administrivia Piazza has a search bar use it before you post! And make sure you name your posts descriptively so others can find them! Exercise 3 out today and due Wednesday morning We highly recommend doing the extra exercises that are at the end of each lecture Also, Google for C pointer exercises and do as many as you can get your hands on You MUST master pointers quickly, or you ll have trouble the rest of the course (including hw1) 2

Administrivia hw0 due tonight before 11:59 pm (and 0 seconds) If your clock says 11:59, then it s late! You really, really don t want to use late day tokens for hw0 Git: add/commit/push, then tag with hw0-final, then push tag Then clone repo somewhere totally different and do git checkout hw0-final and verify that all is well hw1 due Thu, 4/12 You may not modify interfaces (.h files) You might get a merge conflict when pushing hw0 Do a pull, accept the merge (ok to use default message), then do git add/commit/push Suggestion: look at example_program_{ll ht.c for typical usage of lists and hash tables 3

Lecture Outline Heap-allocated Memory malloc() and free() Memory leaks structs and typedef 4

Memory Allocation So Far So far, we have seen two kinds of memory allocation: int counter = 0; // global var counter++; printf("count = %d\n",counter); counter is statically-allocated Allocated when program is loaded Deallocated when program exits int foo(int a) { int x = a + 1; // local var return x; int y = foo(10); // local var printf("y = %d\n",y); a, x, y are automaticallyallocated Allocated when function is called Deallocated when function returns 5

Dynamic Allocation Situations where static and automatic allocation aren t sufficient: We need memory that persists across multiple function calls but not the whole lifetime of the program We need more memory than can fit on the Stack We need memory whose size is not known in advance to the caller // this is pseudo-c code char* ReadFile(char* filename) { int size = GetFileSize(filename); char* buffer = AllocateMem(size); ReadFileIntoBuffer(filename, buffer); return buffer; 6

Dynamic Allocation What we want is dynamically-allocated memory Your program explicitly requests a new block of memory The language allocates it at runtime, perhaps with help from OS Dynamically-allocated memory persists until either: Your code explicitly deallocated it (manual memory management) A garbage collector collects it (automatic memory management) C requires you to manually manage memory Gives you more control, but causes headaches 7

Aside: NULL NULL is a memory location that is guaranteed to be invalid In C on Linux, NULL is 0x0 and an attempt to dereference NULL causes a segmentation fault Useful as an indicator of an uninitialized (or currently unused) pointer or allocation error It s better to cause a segfault than to allow the corruption of memory! segfault.c int* p = NULL; *p = 1; // causes a segmentation fault 8

malloc() General usage: var = (type*) malloc(size in bytes) malloc allocates a block of memory of the requested size Returns a pointer to the first byte of that memory And returns NULL if the memory allocation failed! You should assume that the memory initially contains garbage You ll typically use sizeof to calculate the size you need // allocate a 10-float array float* arr = (float*) malloc(10*sizeof(float)); if (arr == NULL) { return errcode;... // do stuff with arr 9

calloc() General usage: var = (type*) calloc(num, bytes per element) Like malloc, but also zeros out the block of memory Helpful for shaking out bugs Slightly slower; preferred for non-performance-critical code malloc and calloc are found in stdlib.h // allocate a 10-double array double* arr = (double*) calloc(10, sizeof(double)); if (arr == NULL) { return errcode;... // do stuff with arr 10

free() free(pointer); Usage: free(pointer); Deallocates the memory pointed-to by the pointer Pointer must point to the first byte of heap-allocated memory (i.e. something previously returned by malloc or calloc) Freed memory becomes eligible for future allocation Pointer is unaffected by call to free Defensive programming: can set pointer to NULL after freeing it float* arr = (float*) malloc(10*sizeof(float)); if (arr == NULL) return errcode;... // do stuff with arr free(arr); arr = NULL; // OPTIONAL 11

The Heap The Heap is a large pool of unused memory that is used for dynamically-allocated data 0xFF FF OS kernel [protected] Stack malloc allocates chunks of data in the Heap; free deallocates those chunks malloc maintains bookkeeping data in the Heap to track allocated blocks Lab 5 from 351! Shared Libraries Heap (malloc/free) Read/Write Segment.data,.bss Read-Only Segment.text,.rodata 0x00 00 12

Heap and Stack Example arraycopy.c #include <stdlib.h> int* copy(int a[], int size) { int i, *a2; a2 = malloc(size*sizeof(int)); if (a2 == NULL) return NULL; main Note: Arrow points to next instruction. OS kernel [protected] nums ncopy Stack for (i = 0; i < size; i++) a2[i] = a[i]; return a2; int nums[4] = {1, 2, 3, 4; int* ncopy = copy(nums, 4); //.. do stuff with the array.. free(ncopy); Heap (malloc/free) Read/Write Segment Read-Only Segment (main, copy) 13

Heap and Stack Example arraycopy.c #include <stdlib.h> int* copy(int a[], int size) { int i, *a2; a2 = malloc(size*sizeof(int)); if (a2 == NULL) return NULL; main OS kernel [protected] Stack nums 1 2 3 4 ncopy for (i = 0; i < size; i++) a2[i] = a[i]; return a2; int nums[4] = {1, 2, 3, 4; int* ncopy = copy(nums, 4); //.. do stuff with the array.. free(ncopy); Heap (malloc/free) Read/Write Segment Read-Only Segment (main, copy) 14

Heap and Stack Example arraycopy.c #include <stdlib.h> int* copy(int a[], int size) { int i, *a2; a2 = malloc(size*sizeof(int)); if (a2 == NULL) return NULL; for (i = 0; i < size; i++) a2[i] = a[i]; main copy OS kernel [protected] Stack nums 1 2 3 4 ncopy a size 4 i a2 return a2; int nums[4] = {1, 2, 3, 4; int* ncopy = copy(nums, 4); //.. do stuff with the array.. free(ncopy); Heap (malloc/free) Read/Write Segment Read-Only Segment (main, copy) 15

Heap and Stack Example arraycopy.c #include <stdlib.h> int* copy(int a[], int size) { int i, *a2; a2 = malloc(size*sizeof(int)); if (a2 == NULL) return NULL; for (i = 0; i < size; i++) a2[i] = a[i]; OS kernel [protected] Stack main copy malloc nums 1 2 3 4 ncopy a size 4 i a2 return a2; int nums[4] = {1, 2, 3, 4; int* ncopy = copy(nums, 4); //.. do stuff with the array.. free(ncopy); Heap (malloc/free) Read/Write Segment Read-Only Segment (main, copy) 16

Heap and Stack Example arraycopy.c #include <stdlib.h> int* copy(int a[], int size) { int i, *a2; a2 = malloc(size*sizeof(int)); if (a2 == NULL) return NULL; for (i = 0; i < size; i++) a2[i] = a[i]; main copy OS kernel [protected] Stack nums 1 2 3 4 ncopy a size 4 i a2 return a2; int nums[4] = {1, 2, 3, 4; int* ncopy = copy(nums, 4); //.. do stuff with the array.. free(ncopy); Heap (malloc/free) Read/Write Segment Read-Only Segment (main, copy) 17

Heap and Stack Example arraycopy.c #include <stdlib.h> int* copy(int a[], int size) { int i, *a2; a2 = malloc(size*sizeof(int)); if (a2 == NULL) return NULL; for (i = 0; i < size; i++) a2[i] = a[i]; main copy OS kernel [protected] Stack nums 1 2 3 4 ncopy a size 4 i 0 a2 return a2; int nums[4] = {1, 2, 3, 4; int* ncopy = copy(nums, 4); //.. do stuff with the array.. free(ncopy); Heap (malloc/free) Read/Write Segment Read-Only Segment (main, copy) 18

Heap and Stack Example arraycopy.c #include <stdlib.h> int* copy(int a[], int size) { int i, *a2; a2 = malloc(size*sizeof(int)); if (a2 == NULL) return NULL; for (i = 0; i < size; i++) a2[i] = a[i]; return a2; int nums[4] = {1, 2, 3, 4; int* ncopy = copy(nums, 4); //.. do stuff with the array.. free(ncopy); main copy OS kernel [protected] Stack nums 1 2 3 4 ncopy a size 4 i 4 a2 1 2 3 4 Heap (malloc/free) Read/Write Segment Read-Only Segment (main, copy) 19

Heap and Stack Example arraycopy.c #include <stdlib.h> int* copy(int a[], int size) { int i, *a2; a2 = malloc(size*sizeof(int)); if (a2 == NULL) return NULL; main OS kernel [protected] Stack nums 1 2 3 4 ncopy for (i = 0; i < size; i++) a2[i] = a[i]; return a2; int nums[4] = {1, 2, 3, 4; int* ncopy = copy(nums, 4); //.. do stuff with the array.. free(ncopy); 1 2 3 4 Heap (malloc/free) Read/Write Segment Read-Only Segment (main, copy) 20

Heap and Stack Example arraycopy.c #include <stdlib.h> int* copy(int a[], int size) { int i, *a2; a2 = malloc(size*sizeof(int)); if (a2 == NULL) return NULL; main OS kernel [protected] Stack nums 1 2 3 4 ncopy for (i = 0; i < size; i++) a2[i] = a[i]; return a2; int nums[4] = {1, 2, 3, 4; int* ncopy = copy(nums, 4); //.. do stuff with the array.. free(ncopy); 1 2 3 4 Heap (malloc/free) Read/Write Segment Read-Only Segment (main, copy) 21

Heap and Stack Example arraycopy.c #include <stdlib.h> int* copy(int a[], int size) { int i, *a2; a2 = malloc(size*sizeof(int)); if (a2 == NULL) return NULL; main free OS kernel [protected] Stack nums 1 2 3 4 ncopy for (i = 0; i < size; i++) a2[i] = a[i]; return a2; int nums[4] = {1, 2, 3, 4; int* ncopy = copy(nums, 4); //.. do stuff with the array.. free(ncopy); Heap (malloc/free) Read/Write Segment Read-Only Segment (main, copy) 22

Heap and Stack Example arraycopy.c #include <stdlib.h> int* copy(int a[], int size) { int i, *a2; a2 = malloc(size*sizeof(int)); if (a2 == NULL) return NULL; main OS kernel [protected] Stack nums 1 2 3 4 ncopy for (i = 0; i < size; i++) a2[i] = a[i]; return a2; int nums[4] = {1, 2, 3, 4; int* ncopy = copy(nums, 4); //.. do stuff with the array.. free(ncopy); Heap (malloc/free) Read/Write Segment Read-Only Segment (main, copy) 23

Peer Instruction Question Which line below is first guaranteed to cause an error? Vote at http://pollev.com/justinh A. Line 1 B. Line 4 C. Line 6 D. Line 7 E. We re lost 1 2 3 4 5 6 7 #include <stdio.h> #include <stdlib.h> int a[2]; int* b = malloc(2*sizeof(int)); int* c; a[2] = 5; b[0] += 2; c = b+3; free(&(a[0])); free(b); free(b); b[0] = 5; 24

Memory Corruption There are all sorts of ways to corrupt memory in C #include <stdio.h> #include <stdlib.h> int a[2]; int* b = malloc(2*sizeof(int)); int* c; a[2] = 5; // assign past the end of an array b[0] += 2; // assume malloc zeros out memory c = b+3; // mess up your pointer arithmetic free(&(a[0])); // free something not malloc'ed free(b); free(b); // double-free the same block b[0] = 5; // use a freed pointer memcorrupt.c // any many more! 25

Memory Leak A memory leak occurs when code fails to deallocate dynamically-allocated memory that is no longer used e.g. forget to free malloc-ed block, lose/change pointer to malloc-ed block Implication: program s VM footprint will keep growing This might be OK for short-lived program, since memory deallocated when program ends Usually has bad repercussions for long-lived programs Might slow down over time (e.g. lead to VM thrashing) Might exhaust all available memory and crash Other programs might get starved of memory 26

Lecture Outline Heap-allocated Memory malloc() and free() Memory leaks structs and typedef 27

Structured Data A struct is a C datatype that contains a set of fields Similar to a Java class, but with no methods or constructors Useful for defining new structured types of data Act similarly to primitive variables Generic declaration: struct tagname { type1 name1;... typen namen; ; // the following defines a new // structured datatype called // a "struct Point" struct Point { float x, y; ; // declare and initialize a // struct Point variable struct Point origin = {0.0,0.0; 28

Using structs Use. to refer to a field in a struct Use -> to refer to a field from a struct pointer Dereferences pointer first, then accesses field struct Point { float x, y; ; struct Point p1 = {0.0, 0.0; // p1 is stack allocated struct Point* p1_ptr = &p1; p1.x = 1.0; p1_ptr->y = 2.0; // equivalent to (*p1_ptr).y = 2.0; simplestruct.c 29

Copy by Assignment You can assign the value of a struct from a struct of the same type this copies the entire contents! #include <stdio.h> struct Point { float x, y; ; struct Point p1 = {0.0, 2.0; struct Point p2 = {4.0, 6.0; printf("p1: {%f,%f p2: {%f,%f\n", p1.x, p1.y, p2.x, p2.y); p2 = p1; printf("p1: {%f,%f p2: {%f,%f\n", p1.x, p1.y, p2.x, p2.y); structassign.c 30

typedef typedef type name; Generic format: typedef type name; Allows you to define new data type names/synonyms Both type and name are usable and refer to the same type Be careful with pointers * before name is part of type! // make "superlong" a synonym for "unsigned long long" typedef unsigned long long superlong; // make "str" a synonym for "char*" typedef char *str; // make "Point" a synonym for "struct point_st {... // make "PointPtr" a synonym for "struct point_st*" typedef struct point_st { superlong x; superlong y; Point, *PointPtr; // similar syntax to "int n, *p;" Point origin = {0, 0; 31

Dynamically-allocated Structs You can malloc and free structs, just like other data type sizeof is particularly helpful here // a complex number is a + bi typedef struct complex_st { double real; // real component double imag; // imaginary component Complex, *ComplexPtr; // note that ComplexPtr is equivalent to Complex* ComplexPtr AllocComplex(double real, double imag) { Complex* retval = (Complex*) malloc(sizeof(complex)); if (retval!= NULL) { retval->real = real; retval->imag = imag; return retval; complexstruct.c 32

Structs as Arguments Structs are passed by value, like everything else in C Entire struct is copied where? To manipulate a struct argument, pass a pointer instead typedef struct point_st { int x, y; Point, *PointPtr; void DoubleXBroken(Point p) { p.x *= 2; void DoubleXWorks(PointPtr p) { p->x *= 2; Point a = {1,1; DoubleXBroken(a); printf("(%d,%d)\n", a.x, a.y); // prints: (, ) DoubleXWorks(&a); printf("(%d,%d)\n", a.x, a.y); // prints: (, ) 33

Returning Structs Exact method of return depends on calling conventions Often in %rax and %rdx for small structs Often returned in memory for larger structs // a complex number is a + bi typedef struct complex_st { double real; // real component double imag; // imaginary component Complex, *ComplexPtr; Complex MultiplyComplex(Complex x, Complex y) { Complex retval; retval.real = (x.real * y.real) - (x.imag * y.imag); retval.imag = (x.imag * y.real) - (x.real * y.imag); return retval; // returns a copy of retval complexstruct.c 34

Pass Copy of Struct or Pointer? Value passed: passing a pointer is cheaper and takes less space unless struct is small Field access: indirect accesses through pointers are a bit more expensive and can be harder for compiler to optimize For small stucts (like struct complex_st), passing a copy of the struct can be faster and often preferred; for large structs use pointers 35

Extra Exercise #1 Write a program that defines: A new structured type Point Represent it with floats for the x and y coordinates A new structured type Rectangle Assume its sides are parallel to the x-axis and y-axis Represent it with the bottom-left and top-right Points A function that computes and returns the area of a Rectangle A function that tests whether a Point is inside of a Rectangle 36

Extra Exercise #2 Implement AllocSet() and FreeSet() AllocSet() needs to use malloc twice: once to allocate a new ComplexSet and once to allocate the points field inside it FreeSet() needs to use free twice typedef struct complex_st { double real; // real component double imag; // imaginary component Complex; typedef struct complex_set_st { double num_points_in_set; Complex* points; // an array of Complex ComplexSet; ComplexSet* AllocSet(Complex c_arr[], int size); void FreeSet(ComplexSet* set); 37