Functions:"" Breaking"large"computing"tasks"to" smaller"ones"

Similar documents
Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

C'Programming' data'separate'from'methods/functions' Low'memory'overhead'compared'to'Java'

CS61C : Machine Structures

ECE 15B COMPUTER ORGANIZATION

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

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

CS61C : Machine Structures

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

Lecture 2: C Programm

How about them A s!! Go Oaktown!! CS61C - Machine Structures. Lecture 4 C Structures Memory Management Dan Garcia.

CS61C : Machine Structures

Lectures 13 & 14. memory management

Lecture 3: C Programm

Operating Systems 2INC0 C course Pointer Advanced. Dr. Ir. Ion Barosan

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

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

review Pointers and arrays are virtually same C knows how to increment pointers C is an efficient language, with little protection

CS61C : Machine Structures

Lecture 4: Outline. Arrays. I. Pointers II. III. Pointer arithmetic IV. Strings

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

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

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?!

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

Lecture 8: Pointer Arithmetic (review) Endianness Functions and pointers

Intermediate Programming, Spring 2017*

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

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

CS61C : Machine Structures

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

CS24 Week 2 Lecture 1

Class Information ANNOUCEMENTS

However, in C we can group related variables together into something called a struct.

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

Data Representation and Storage. Some definitions (in C)

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

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

Character Strings. String-copy Example

6.S096: Introduction to C/C++

Kurt Schmidt. October 30, 2018

speller.c dictionary contains valid words, one per line 1. calls load on the dictionary file

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

REFERENCES, POINTERS AND STRUCTS

C Programming. Course Outline. C Programming. Code: MBD101. Duration: 10 Hours. Prerequisites:

Lectures 5-6: Introduction to C

Run-time Environments - 3

COMPSCI 210 Part II Data Structure

Structures, Unions Alignment, Padding, Bit Fields Access, Initialization Compound Literals Opaque Structures Summary. Structures

Dynamic Memory: Alignment and Fragmentation

C Review. MaxMSP Developers Workshop Summer 2009 CNMAT

EL2310 Scientific Programming

Pointers. Part VI. 1) Introduction. 2) Declaring Pointer Variables. 3) Using Pointers. 4) Pointer Arithmetic. 5) Pointers and Arrays

EM108 Software Development for Engineers

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

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

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

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

CSC 1600 Memory Layout for Unix Processes"

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

CS240: Programming in C

Intro to C: Pointers and Arrays

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

Dynamic Memory Allocation and Linked Lists

COSC345 Software Engineering. The Heap And Dynamic Memory Allocation

CS 61C: Great Ideas in Computer Architecture. (Brief) Review Lecture

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

Fundamentals of Programming

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

Heap Arrays. Steven R. Bagley

Cpt S 122 Data Structures. Data Structures

Section Notes - Week 1 (9/17)

CS 61c: Great Ideas in Computer Architecture

SYSC 2006 C Winter 2012

Data Representation and Storage

Quiz 0 Review Session. October 13th, 2014

Code Generation. Dragon: Ch (Just part of it) Holub: Ch 6.

Outline. Briefly review the last class Pointers and Structs Memory allocation Linked lists

CS61, Fall 2012 Section 2 Notes

CA341 - Comparative Programming Languages

PROGRAMMAZIONE I A.A. 2017/2018

A brief introduction to C programming for Java programmers

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

a data type is Types

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

Low-Level C Programming. Memory map Pointers Arrays Structures

We would like to find the value of the right-hand side of this equation. We know that

Singly linked lists in C.

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

CS 11 C track: lecture 5

Section - Computer Science. int main() {! int a=10,b=20;! printf("a:%d B:%d\n",a,b);! a=(a+b)-(b=a);! printf("a:%d B:%d\n",a,b);!

CSE 333 Midterm Exam 7/27/15 Sample Solution

Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

CSCI 262 Data Structures. Arrays and Pointers. Arrays. Arrays and Pointers 2/6/2018 POINTER ARITHMETIC

CS C Primer. Tyler Szepesi. January 16, 2013


Tutorial 5. Overflow. Data Types. Structures. Call stack. Stack frames. Debugging Tips. CS 136 Winter 2019 Tutorial 5 1

Pointers (1A) Young Won Lim 3/5/18

Introduction to Computer Architecture

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

CSE351 Winter 2016, Final Examination March 16, 2016

Transcription:

Lecture'3'(P a rt1):'''''''' ' 0 3 ' C 'Programm E S C ing'' 1

Functions:"" Breaking"large"computing"tasks"to" smaller"ones" 2

C'Programming' 'Procedural'thought'process' main%()%/*%high%level%outline%*/% {% %%.%.%.% %%get_input(arg1)%/*comment:%step%1%*/% %%perform_step_2(arg2);%% %%perform_step_3();% %%store_result();%/*%print%output%or%store%in%a%file%*/% % }% ' 3

Overview'of'Functions' Functions'make'code'easy'to''! Maintain'! Debug'! Reuse' 4

Functions'Overview' void%foo(int%x,%int%y);%%//declaration" 5

//De5inition" Functions'Overview' void%foo(int%x,%int%y)%{% %%%int%tmp;% %%%tmp=%x;% %%%x=%y;% %%%y=%tmp;%%}%% What'does'foo'do?' tmp' x' foo' y' 6

Stack'Allocation:'Function'local' variables'and'parameters' When'program'execution'starts' Local'variables'of'func()' Other'information'about' func()' Local'variables'of'main()' What'if'main'calls'the'function'func()?' Stack'Pointer' 7

Stack'Allocation:'Function'local' variables'and'parameters' Variables'whose'lifetime'is'the'execution'time'of' function'are'managed'using'the'stack'structure' Local'variables'of'main()' Stack'Pointer' 8

Variables'declared'within'a'function'' A. can'be'used'by'other'functions' ' B. can'only'be'used'within'the'function'' C. continue'to'exist'in'memory'as'long'as'the' program'executes' ' 9

Consider'the'following'function' void%swap(int%x,%int%y);%% //Declaration" void%swap(int%x,%int%y)%{% %%%int%tmp;% %%%tmp=%x;% %%%x=%y;% %%%y=%tmp;%%}%% tmp' x' swap' y' The'swap'function'intends'to'swap'the'value' of'its'inputs.'is'the'logic'correct?' A. Yes' B. No' C. Depends' 10 x' y'

Q:'Are'the'value'of'variables' a 'and' b ' interchanged'after'swap'is'called?' main()%{% %%.%.%.% %%swap(a,%b);% %%.%.%.%.%}%% a' 5' 6' b' ' A. Yes,'because'..' B. No,'because..' ' ' 11

Q:'Which'of'the'following'changes'are'required'to' interchange'the'values'in' a 'and' b 'when'swap'is'called?' {%.%.%.%.% %swap(a,%b);% 5' 6' x' y' a' b' }%% swap' ' A. In'swap,'return'the'values'of' x 'and' y 'to'the'main'function' after'swapping'them' B. Declare' a 'and' b 'as'global'variables,'so'that'they'become' accessible'to'the'swap'routine' C. Pass'the'address'of' a 'and' b 'to'swap'instead'of'their'value' D. Move'the'implementation'in'swap'to'the'main'function'' ' 12

Q:'Which'of'the'following'changes'are'required'to' interchange'the'values'in' a 'and' b 'when'swap'is'called?' {%.%.%.%.% %swap(a,%b);% 5' 6' x' y' a' b' }%% swap' ' A. In'swap,'return'the'values'of' x 'and' y 'to'the'main'function' after'swapping'them' B. Declare' a 'and' b 'as'global'variables,'so'that'they'become' accessible'to'the'swap'routine' C. Pass'the'address'of' a 'and' b 'to'swap'instead'of'their'value' D. Move'the'implementation'in'swap'to'the'main'function'' ' 13

Functions:'Call'by'reference' Q:'What'should'the'modi[ied'swap'function'do?' ' void%swap(int%*x,%int%*y)%{% %%%.%.%.% }%% A. Swap'the'addresses''in' x 'and' y ' B. Swap'the'values'pointed'to'by' x 'and' y ' C. Both'the'above'operations'are'equivalent' ' 14

Functions:'Call'by'reference' Q:'What'should'the'modi[ied'swap'function'do?' ' void%swap(int%*x,%int%*y)%{% %%%.%.%.% }%% A. Swap'the'addresses''in' x 'and' y ' B. Swap'the'values'pointed'to'by' x 'and' y ' C. Both'the'above'operations'are'equivalent' ' 15

void IncrementPtr(int *p){ p = p + 1; } Q: What happens when IncrementPtr(q)is called in the following code: A q int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(q);' 50 60 70 A. The pointer q points to the next element in the array with value 60 B. The pointer q points to the first element in the array with value 50

void IncrementPtr(int *p){ p = p + 1; } Q: What happens when IncrementPtr(q)is called in the following code: A q int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(q);' 50 60 70 A. The pointer q points to the next element in the array with value 60 B. The pointer q points to the first element in the array with value 50

void IncrementPtr(int **p){ p = p + 1; } Q: How should we implement IncrementPtr(),so that q moves by one element when the following code executes? int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(&q);' A q 50 60 70 A. p = p + 1; //The current one is correct B. &p = &p + 1; C. *p= *p + 1; D. *p++; E. p= &p+1;

void IncrementPtr(int **p){ p = p + 1; } Q: How should we implement IncrementPtr(),so that q moves by one element when the following code executes? int A[3] = {50, 60, 70}; int *q = A; IncrementPtr(&q);' A q 50 60 70 A. p = p + 1; //The current one is correct B. &p = &p + 1; C. *p= *p + 1; D. *p++; E. p= &p+1;

Derived"data"types:" Structures"

C structures : Overview A struct is a data structure composed of simpler data types. Like a class in Java/C++ but without methods or inheritance. struct point { int x; int y; } void PrintPoint(struct point p) { printf( (%d,%d), p.x, p.y); }

Pointers to structures The C arrow operator (->) dereferences and extracts a structure field with a single operator. The following are equivalent: struct point *p; printf( x is %d\n, (*p).x); printf( x is %d\n, p->x);

Representation in memory struct p { int y; char x; }; struct p sp; sp' y'(4'bytes)' x'(1byte)' 0x100' 0x104' 0x105'

struct p { char x; int y; }; Struct p sp; Data Alignment Q: How is the above struct laid out in memory?

Alignment Fundamentals Processors do not always access memory in byte sized chunks, instead in 2, 4, 8, even 16 or 32 byte chunks Boundaries at which data objects are stored affects the behavior of read/write operations into memory 0x00' 0x01' 0x02' 0x03' 0x04' 0x05' Programmer's'view' of'memory' 0x00' 0x02' 0x04' Processor s'view'of' memory'

Alignment Fundamentals Consider the task of reading 4 bytes from memory starting at 0x00 for processors that read 1, 2, 4 bytes at a time. How many memory accesses are needed in each case? 0x00' 0x01' 0x02' 0x03' 0x04' 0x05' 0x00' 0x01' 0x02' 0x03' 0x04' 0x05' 0x00' 0x02' 0x04' 0x00' 0x04' Memory' 1'byte'reads' 2'byte'reads' 4'byte'reads'

Alignment Fundamentals Now consider the task of reading 4 bytes from memory starting at 0x01 for processors that read 1, 2, 4 bytes at a time. How many memory accesses are needed in each case? 0x00' 0x01' 0x02' 0x03' 0x04' 0x05' Some processors just would not allow this scenario because it is extra work for the h/w and it affects the performance 0x00' 0x01' 0x02' 0x03' 0x04' 0x05' 0x00' 0x02' 0x04' 0x00' 0x04' Memory' 1'byte'reads' 2'byte'reads' 4'byte'reads' Additional'operations'needed'to'get' required'bytes'

Alignment requirements Data objects should start at memory addresses that are divisible by the size of the data object. short (2 byte) at address divisible by 2 0b _ 0 int (4 byte) at address divisible by 4 0b 00 double (8 byte) at address divisible by 8 0b 000

Alignment requirements Q: How is sp laid out in memory? Compiler takes care of zero padding struct p { char x; int y; }; struct p sp;

Alignment requirements Q: How is sp laid out in memory? Compiler takes care of zero padding struct p { char x; int y; }; struct p sp; sp' 0x100' x'' 0''0''0'' 0x104' y'' 0x108'

How big are structs? Recall C operator sizeof() which gives size in bytes (of type or variable) Q: How big is sizeof(struct p)? struct p { char x; int y; }; A. 5 bytes B. 8 bytes C. 12 bytes

Data' Linked Lists Node'1' Node'2' Node'3'

Linked Lists Node'1' Node'2' Node'3' Data' A'generic'linked'list'has:'! Multiple'data'objects'(structs)'also'called'nodes'! Each'node'linked'to'the'next'node'in'the'list'i.e.'it'knows'the' address'of'the'next'node'! Nodes'located'at'different'memory'locations'(unlike'arrays' where'they'are'contiguous)' Advantages'compared'to'arrays'! Multiple'data'members'(of'different'data'types)'can'be'stored' in'each'node'! Nodes'can'be'easily'inserted'or'removed'from'the'list'without' modifying'the'whole'list'

Let s look at an example of using structures, pointers, malloc(), and free() to implement a linked list of strings. typedef struct Node node; struct Node { char *value; next; }; node' value' Q:What is the data type of the variable next? A. struct Node B. Node C. node D. node * next'

Let s look at an example of using structures, pointers, malloc(), and free() to implement a linked list of strings. typedef struct Node node; struct Node { char *value; next; }; node' value' Q:What is the data type of the variable next? A. struct Node B. Node C. node D. node * next'

Adding a node to the list node *list_add(node* head, char *string) { //Step 1:Create a new node ; return new_node; value=?' } new_node' Q: How should we declare and initialize new_node? next=?' A. node *new_node=(node*) malloc(sizeof(node)); B. node new_node; C. node *new_node=head; D. node *new_node=(node *)malloc(sizeof(head));

Adding a node to the list node *list_add(node* head, char *string) { //Step 1:Create a new node ; return new_node; value=?' } new_node' Q: How should we declare and initialize new_node? next=?' A. node *new_node=(node*) malloc(sizeof(node)); B. node new_node; C. node *new_node=head; D. node *new_node=(node *)malloc(sizeof(head));

Q:What is wrong with step 2? node *list_add(node* head, char *string) { node *new_node=(node*) malloc(sizeof(node)); }

Q:What is wrong with step 2? node *list_add(node* head, char *string) { node *new_node=(node*) malloc(sizeof(node)); //Step 2: Fill in its value strcpy(new_node->value, string); return new_node; } A. Step 2 is correct. B. We should use the operator. instead of -> C. Memory is not allocated for value

Q:What is wrong with step 2? node *list_add(node* head, char *string) { node *new_node=(node*) malloc(sizeof(node)); //Step 2: Fill in its value strcpy(new_node->value, string); return new_node; } A. Step 2 is correct. B. We should use the operator. instead of -> C. Memory is not allocated for value

Draw pointer diagrams node *list_add(node* head, char *string) { node *new_node=(node*) malloc(sizeof(node)); new_node->value = (char*) malloc(strlen(string)+1); strcpy(new_node->value, string); }

What should Step 3 be? node *list_add(node* head, char *string){ node *new_node=(node*) malloc(sizeof(node)); new_node->value = (char*) malloc(strlen(string)+1); strcpy(new_node->value, string); //Step 3: } A. new_node->next =head; B. next=head; C. head=new_node; D. new_node->next =*head;

What should Step 3 be? node *list_add(node* head, char *string){ //Step 1: Create a new node node *new_node=(node*) malloc(sizeof(node)); //Step 2: Fill in its value new_node->value = (char*) malloc(strlen(string)+1); strcpy(new_node->value, string); //Step 3: Link new_node to the head of the list new_node->next =head; return new_node; } new_node' head" A. new_node->next =head; B. next=head; C. head=new_node; D. new_node->next =*head; value ' abc ' next' NULL'

Dangling pointers and memory leaks Dangling reference : Pointer points to a memory location that no longer exists Memory leaks (tardy free) Memory in heap that can no longer be accessed

Q: Which of the following functions returns a pointer to an object that may not exist in memory? int * f1(int num){ int *mem1 =(int *)malloc(num*sizeof(int)); return(mem1);} int * f2(int num){ int mem2[num]; return(mem2);} A. f1 B. f2 C. Both

Q: Which of the following functions returns a pointer to an object that may not exist in memory? int * f1(int num){ int *mem1 =(int *)malloc(num*sizeof(int)); return(mem1);} int * f2(int num){ int mem2[num]; return(mem2);} A. f1 B. f2 because mem2 is a local variable created in the stack C. Both

Which'of'the'following'is'an' example'of'a'dangling'pointer?' A. void'foo(int'bytes)''{' '''''''char'*ch'=(char'*)'malloc(bytes);' ''''''''.'.'.'.' ''''''''free'(ch);' ''''''''.'.'.'.}' ' B.''''int'*'foo(int'bytes)''{' '''''''int'i=14;' ''''''''.'.'.'.' ''''''''return'(&i);' ''''''''}' C.'''char*'foo(int'bytes)''{' '''''''char'*ch'=(char'*)'malloc(bytes);' ''''''''.'.'.'.' ''''''''return'(ch);' ''''''''}''''' D.''A'combination'of'the' above''''''

Which'of'the'following'is'an' example'of'a'dangling'pointer?' A. void'foo(int'bytes)''{' '''''''char'*ch'=(char'*)'malloc(bytes);' ''''''''.'.'.'.' ''''''''free'(ch);' ''''''''.'.'.'.}' ' B.''''int'*'foo(int'bytes)''{' '''''''int'i=14;' ''''''''.'.'.'.' ''''''''return'(&i);' ''''''''}' C.'''char*'foo(int'bytes)''{' '''''''char'*ch'=(char'*)'malloc(bytes);' ''''''''.'.'.'.' ''''''''return'(ch);' ''''''''}''''' D.""A"combination"of" the"above""""""

Memory'Leak' Is'the'following'an'example'for'a'memory'leak?' 'void'foo'(int'bytes)' '{' ''''int'*p'='(int'*)'malloc(bytes);' '''}'

The'C'runtime'environment' 50

What'does'the'executable'contain?' Instructions'or' code ' Data''' ''What'is'data?' ' '" Any'information'that'instructions'operate'on' (objects'in'memory)' 51

C'Runtime'Environment'' Code '(instructions'in'machine'language)' Data '(initialized'and'uninitialized'j'static' allocated)' Both'code'and'data'don t'change'in'size'' Heap '(for'dynamically'allocated'data)' Stack '(for'function'local'variables)' Heap'and'stack'change'in'size'as'the'program' executes' ' 52

C'Runtime'Environment' Code' Initialized'Data' Uninitialized'Data' Heap' Stack' 53

Data'lifetime' Lifetime:'The'interval'between'time'of' creation'and'end'of'existence'of'data' 54

Possible'lifetimes'of'data' 1. Execution'time'of'program' 2. Time'between'explicit'creation'and'explicit' deletion' 3. Execution'time'of'a'function'(time'between' function'call'and'function'return)' ' 55

Declarations'and'de[initions' Scope'and'lifetime'are'often'implicit'but' sometimes'we'have'to'use'speci[ic'keywords:' ' %static%int%a=0;%/*de[ines'lifetime*/' %extern%int%a;%%/*extends'scope'to'multiple' [iles*/' %% 56

Different'types'of'data' What'factor'differentiates'the'following'three' categories 'of'data'in'a'c'program:' Global'and'static'variables' Local'variables' Dynamic'variables' A. Scope' B. Lifetime' C. Representation' 57

Different'types'of'data' What'factor'differentiates'the'following'three' categories 'of'data'in'a'c'program:' Global'and'static'variables' Local'variables' Dynamic'variables' A. Scope' B. Lifetime' C. Representation' 58