Class Information ANNOUCEMENTS

Similar documents
CS 314 Principles of Programming Languages. Lecture 9

CS 314 Principles of Programming Languages. Lecture 11

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

EL2310 Scientific Programming

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

CS61, Fall 2012 Section 2 Notes

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

CSC 1600 Memory Layout for Unix Processes"

Dynamic Memory Management

Quiz 0 Review Session. October 13th, 2014

Programming Studio #9 ECE 190

Lecture 3: C Programm

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

CS 11 C track: lecture 5

Lecture 8 Dynamic Memory Allocation

CS61C : Machine Structures

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

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Memory Management. CSC215 Lecture

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Dynamic Data Structures. CSCI 112: Programming in C

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

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

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Dynamic Data Structures (II)

Dynamic Memory Management! Goals of this Lecture!

Run-time Environments - 3

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

Dynamic Memory Allocation and Linked Lists

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


Dynamic Memory. R. Inkulu (Dynamic Memory) 1 / 19

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

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

Lecture 2: C Programm

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

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

Variation of Pointers

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Procedural programming with C

CMSC 330: Organization of Programming Languages

Short Notes of CS201

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

ECE 15B COMPUTER ORGANIZATION

PRINCIPLES OF OPERATING SYSTEMS

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

Outline. Lecture 1 C primer What we will cover. If-statements and blocks in Python and C. Operators in Python and C

CMSC 330: Organization of Programming Languages

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

CS201 - Introduction to Programming Glossary By

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Intro to C: Pointers and Arrays

Recitation #11 Malloc Lab. November 7th, 2017

Memory Management. CS449 Fall 2017

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Programming Languages

CA341 - Comparative Programming Languages

CS61C : Machine Structures

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

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Memory management. Johan Montelius KTH

Class Information ANNOUCEMENTS

CMSC 341 Lecture 2 Dynamic Memory and Pointers

Kurt Schmidt. October 30, 2018

Dynamic Memory Management

CSE 124 Discussion (10/3) C/C++ Basics

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

Processes. Johan Montelius KTH

From Java to C. Thanks to Randal E. Bryant and David R. O'Hallaron (Carnegie-Mellon University) for providing the basis for these slides

Today. Dynamic Memory Allocation: Basic Concepts. Dynamic Memory Allocation. Dynamic Memory Allocation. malloc Example. The malloc Package

Intermediate Programming, Spring 2017*

Lectures 5-6: Introduction to C

Pointers and Memory Management

A process. the stack

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

CS558 Programming Languages

Applied Programming and Computer Science, DD2325/appcs14 PODF, Programmering och datalogi för fysiker, DA7011

Design and development of embedded systems for the Internet of Things (IoT) Fabio Angeletti Fabrizio Gattuso

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

Review! Lecture 5 C Memory Management !

Arrays and Pointers. CSC209: Software Tools and Systems Programming (Winter 2019) Furkan Alaca & Paul Vrbik. University of Toronto Mississauga

Computer Programming Unit 3

ENEE 150: Intermediate Programming Concepts for Engineers Spring 2018 Handout #27. Midterm #2 Review

CS61C : Machine Structures

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

Warmup January 9th, What is the value of the following C expression? 8*9 % 10/ 2

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

Run-time Environments

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

CS558 Programming Languages

Run-time Environments

Lectures 13 & 14. memory management

CS61 Section Notes. Section 5 (Fall 2011) Topics to be covered Common Memory Errors Dynamic Memory Allocation Assignment 3: Malloc

C PROGRAMMING Lecture 5. 1st semester

CSC 270 Survey of Programming Languages. What is a Pointer?

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

CS349/SE382 A1 C Programming Tutorial

CS2141 Software Development using C/C++ Debugging

Transcription:

Class Information ANNOUCEMENTS Third homework due TODAY at 11:59pm. Extension? First project has been posted, due Monday October 23, 11:59pm. Midterm exam: Friday, October 27, in class. Don t forget to work on your C and Linux skills (ilab). Final exam: Wednesday, December 20, 4:00-7:00pm. CS 314 fall 17, lecture 12 page 1

Run-time storage organization Typical memory layout Logical Address Space C o d e S t a t i c H e a p free memory S t a c k low high The classical scheme allows both stack and heap maximal freedom code and static may be separate or intermingled Will talk about this in more detail in a later lecture! CS 314 fall 17, lecture 12 page 2

Basic Comparison (incomplete!) C Java Basic types: Primitive types: int, double, char int, double, char, boolean Pointer (to a value) Reference (to an object) Aggregates: Aggregates: array, struct array, object (class) Control flow: Control flow if-else, switch, while, if-else, switch, while, break, continue, for, return, goto break, continue, for, return Logic operators: Logic operators:, &&,!, &&,! Logical comparisons: Logical comparisons: ==,!= ==,!= Numeric comparisons: Numeric comparisons: <>, <=, >= <>, <=, >= string as char * array String as an object CS 314 fall 17, lecture 12 page 3

Compile and Run a C program test.c: #include <stdio.h> int main(void) { int x, y; printf("first number:\n"); scanf("%d", &x); printf("second number:\n"); scanf("%d", &y); printf("%d+%d = %d\n", x, y, x+y); printf("%d-%d = %d\n", x, y, x-y); printf("%d*%d = %d\n", x, y, x*y); } return 0; gcc test.c: calls the GNU C compiler, and generates executable a.out./a.out runs the executable gcc -o run test.c compiles program, and generates executable run gcc -g test.c generates a.out with debugging info gdb a.out run debugger on a.out; online documentation man gdb CS 314 fall 17, lecture 12 page 4

Compile and Run a C program > gcc test.c > a.out First number: 4 Second number: 12 4+12 = 16 4-12 = -8 4*12 = 48 > START PROGRAMMING IN C NOW! CS 314 fall 17, lecture 12 page 5

Debugging C programs rhea% gdb a.out (gdb) list 1 #include <stdio.h> 2 int main(void) 3 { 4 int x, y; 5 printf("first number:\n"); scanf("%d", &x); 6 printf("second number:\n"); scanf("%d", &y); 7 printf("%d+%d = %d\n", x, y, x+y); (gdb) break 7 Breakpoint 1 at 0x1052c: file test.c, line 7. (gdb) run Starting program: /.../a.out First number: 4 Second number: 12 Breakpoint 1, main () at test.c:7 7 printf("%d+%d = %d\n", x, y, x+y); (gdb) print x $1 = 4 (gdb) print y $2 = 12 (gdb) cont Continuing. 4+12 = 16 4-12 = -8 4*12 = 48 Program exited normally. (gdb) quit CS 314 fall 17, lecture 12 page 6

Pointers in C Pointer: Variable whose R-values (content) is the L-value (address) of a variable address-of operator & dereference ( content-of ) operator int *p, x; p x p = &x; *p = 5; p x 5 x = 12; p x 12 CS 314 fall 17, lecture 12 page 7

Pointers in C Pointers can point to pointer variables (multi-level pointers) int *p, x; int **r; r p x p = &x; *p = 5; r p x 5 r = &p; **r = 10; r p x 10 CS 314 fall 17, lecture 12 page 8

Pointers in C vs. References in Java C Java Need explicit dereference operator are implicitly dereferenced Can mutate R-value of pointer Cannot mutate R-value through pointer arithmetic p=p+1 Casting means type conversion Casting just satisfies the type checker; no type conversion Special relation to arrays No special relation to arrays CS 314 fall 17, lecture 12 page 9

Example: Singly-linked list #include <stdio.h> #include <stdlib.h> /* TYPE DEFINITION */ typedef struct cell listcell; struct cell { int num; listcell *next; }; /* GLOBAL VARIABLES */ listcell *head, *new_cell, *current_cell; head new_cell current_cell num num num next next next CS 314 fall 17, lecture 12 page 10

Example: Singly-linked list int main (void) { int j; /* CREATE FIRST LIST ELEMENT */ head = (listcell *) malloc(sizeof(listcell)); head->num = 1; head->next = NULL; /* CREATE 9 MORE ELEMENTS */ for (j=2; j<=10; j++) { new_cell = (listcell *) malloc(sizeof(listcell)); new_cell->num = j; new_cell->next = head; head = new_cell; } /* PRINT ALL ELEMENTS */ for (current_cell = head; current_cell!= NULL; current_cell = current_cell->next) printf("%d ", current_cell->num); } printf("\n"); CS 314 fall 17, lecture 12 page 11

Example: Singly-linked list int main (void) { int j; /* CREATE FIRST LIST ELEMENT */ head = (listcell *) malloc(sizeof(listcell)); head->num = 1; head->next = NULL; /* CREATE 9 MORE ELEMENTS */ for (j=2; j<=10; j++) { new_cell = (listcell *) malloc(sizeof(listcell)); new_cell->num = j; new_cell->next = head; head = new_cell; } /* *** HERE *** */ head new_cell current_cell num num num 10 9 2 next next... next num 1 next NULL CS 314 fall 17, lecture 12 page 12

Example: Singly-linked list What is the output of the program Where do the cell objects get allocated? CS 314 fall 17, lecture 12 page 13

Review: Stack vs. Heap Stack: Procedure activations, statically allocated local variables, parameter values Lifetime same as subroutine in which variables are declared Stack frame is pushed with each invocation of a subroutine, and poped after subroutine exit Heap: Dynamically allocated data structures, whose size may not be known in advance Lifetime extends beyond subroutine in which they are created Must be explicitly freed or garbage collected CS 314 fall 17, lecture 12 page 14

Maintaining Free List allocate: contiguous block of memory; remove space from free list (here: singly-linked list). free: return to free list after coalescing with adjacent free storage (if possible); may initiate compaction. free space pointer ALLOCATE free space pointer FREE free space pointer free space pointer CS 314 fall 17, lecture 12 page 15

Heap Storage void * malloc(size t n) (defined in stdlib.h) returns pointer to block of contiguous storage of n bytes on the heap, if possible returns NULL pointer if not enough memory is available you should check for ==NULL after each malloc NOTE: we didn t do this in the example! to allocate storage of a desired type, call malloc with the needed size in bytes, and then cast the return pointer to the desired type head = (listcell *) malloc(sizeof(listcell)); void free(void *ptr) (defined in stdlib.h) data structure that ptr points to is released, i.e., returned to the free memory and may be (partially) reused by a subsequent malloc. CS 314 fall 17, lecture 12 page 16

Problems with Explicit Control of Heap Dangling references Storage pointed to is freed (put on free list), but there are still pointer values (or reference) that allow access the freed storage Able to access storage whose values are not meaningful Garbage Objects in heap that cannot be accessed by the program any more Example int *x, *y; x = (int *) malloc(sizeof(int)); y = (int *) malloc(sizeof(int)); x = y; Memory leaks Failure to release (reclaim) memory storage builds up over time CS 314 fall 17, lecture 12 page 17

Example: Singly-linked list Let s deallocate, i.e., free all list elements. #include "list.h" /* GLOBAL VARIABLES */ listcell *head, *new_cell, *current_cell; int main (void) { int j; /* CREATE FIRST LIST ELEMENT */... /* CREATE 9 MORE ELEMENTS */... /* DEALLOCATE LIST */ for (current_cell = head; current_cell!= NULL; current_cell = current_cell->next) free(current_cell); }... } Does this work? CS 314 fall 17, lecture 12 page 18

Next Lecture Things to do: Keep working on the project! Read Scott: Chap. 3.1-3.4; 8.3 ; ALSU Chap. 7.1-7.3 Next time: More about pointers Dynamic vs. static scoping Runtime environment access links and control links management CS 314 fall 17, lecture 12 page 19