Principles of Programming Languages Topic: Imperative Programming II Professor Lou Steinberg Fall 2004

Similar documents
Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

arrays C Programming Language - Arrays

Sources: On the Web: Slides will be available on:

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

The C Programming Language course syllabus associate level

C Dynamic Data Structures. University of Texas at Austin CS310H - Computer Organization Spring 2010 Don Fussell

C++ Programming Language

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

C++FA 5.1 PRACTICE MID-TERM EXAM

10CS35: Data Structures Using C

CP Lab 2: Writing programs for simple arithmetic problems

Chapter 13 Storage classes

Semantic Analysis: Types and Type Checking

Moving from CS 61A Scheme to CS 61B Java

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

1 Abstract Data Types Information Hiding

Introduction to Data Structures

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

ECS 165B: Database System Implementa6on Lecture 2

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Memory Allocation. Static Allocation. Dynamic Allocation. Memory Management. Dynamic Allocation. Dynamic Storage Allocation

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

PES Institute of Technology-BSC QUESTION BANK

C Interview Questions

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Lecture 12 Doubly Linked Lists (with Recursion)

C++FA 3.1 OPTIMIZING C++

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

Habanero Extreme Scale Software Research Project

Chapter 5 Names, Bindings, Type Checking, and Scopes

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Lecture 10: Dynamic Memory Allocation 1: Into the jaws of malloc()

ADTs,, Arrays, Linked Lists

Lecture Data Types and Types of a Language

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

LINKED DATA STRUCTURES

5 Arrays and Pointers

Object Oriented Software Design II

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

Introduction to Programming II Winter, 2014 Assignment 2

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

Stacks. Linear data structures

FEEG Applied Programming 5 - Tutorial Session

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

DATA STRUCTURES USING C

7.1 Our Current Model

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Informatica e Sistemi in Tempo Reale

Data Structure with C

Stack Allocation. Run-Time Data Structures. Static Structures

Parameter Passing in Pascal

The programming language C. sws1 1

Chapter 2: Elements of Java

Introduction to Java

Simple C Programs. Goals for this Lecture. Help you learn about:

C++ Overloading, Constructors, Assignment operator

Tutorial on C Language Programming

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Coding Rules. Encoding the type of a function into the name (so-called Hungarian notation) is forbidden - it only confuses the programmer.

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Common Data Structures

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

Unit Write iterative and recursive C functions to find the greatest common divisor of two integers. [6]

Threads Scheduling on Linux Operating Systems

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

El Dorado Union High School District Educational Services

7th Marathon of Parallel Programming WSCAD-SSC/SBAC-PAD-2012

Java Interview Questions and Answers

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Crash Course in Java

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

Illustration 1: Diagram of program function and data flow

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

8.5. <summary> Cppcheck addons Using Cppcheck addons Where to find some Cppcheck addons

Java from a C perspective. Plan

CORBA Programming with TAOX11. The C++11 CORBA Implementation

Example 1/24. Example

SQLITE C/C++ TUTORIAL

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Functions and Parameter Passing

Applying Clang Static Analyzer to Linux Kernel

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

The Advantages of Dan Grossman CSE303 Spring 2005, Lecture 25

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

6.S096 Lecture 1 Introduction to C

Virtuozzo Virtualization SDK

Transcription:

Principles of Programming Languages Topic: Imperative Programming II Professor Lou Steinberg Fall 2004 CS 314, LS, BR, LTM: Imperative Programming 1

Review Imperative programming: based on underlying machine: RAM, thread of control variable: a memory location assignment L-values and R-values Pointers * and & #INCLUDE #DEFINE STACKSIZE = 10; printf CS 314, LS, BR, LTM: Imperative Programming 2

L- and R-values int a; int [10] b; int *p 12 a &a b[2] b *b p *p L-value illegal address of variable a illegal address of 3rd element of b illegal illegal address of p contents of p R-value 12 contents of variable a address of variable a contents of 3rd element of b address of 1st element of b contents of 1st element of b contents of p contents of variable that p points to CS 314, LS, BR, LTM: Imperative Programming 3

Excerpt from List in Java public class List extends Object { protected Object element; protected List sublist; /** * Create an new List, initially empty. */ public List() { element = null; sublist = null; } //cons operation public List(Object newelement,list oldlist){ element = newelement; sublist = oldlist; } } CS 314, LS, BR, LTM: Imperative Programming 4

How List building works? List p = new List(); List q = new List( a,p); a List( b,q); b a CS 314, LS, BR, LTM: Imperative Programming 5

list2.c /*sample program to write a linear linked list of integers built like in Java, adding new elements on the front*/ #include<stdio.h> /*this makes these definitions and variables global*/ void *malloc(); typedef struct cell listcell; struct cell{ }; int num; listcell *next; listcell *head, *ele, *p; CS 314, LS, BR, LTM: Imperative Programming 6

cast list2.c, cont. void main(void) { int j; /*create first node in list*/ head = (listcell *) malloc(sizeof (listcell)); head->next = NULL; Allocates heap storage /*now create entries in list of numbers from 1 to 10*/ head->num = 1; for (j=2; j<11; j++) { ele = (listcell *) malloc(sizeof (listcell)); ele->num = j; ele->next =head; /***/ head = ele; } /*now traverse the list and print the elements*/ for (p=head; p!=null; p=p->next) printf("%d ",p->num); printf("\n"); } (*head).next is same as head->next CS 314, LS, BR, LTM: Imperative Programming 7

head->num = 1; head ele 1 2 1 list2.c for (j=2; j<11; j++) { ele=/* &(new listcell*/ ele->num = j; ele->next =head; /***/ head head = ele; } at /***/ in 1st iteration ele 3 head 2 1 at /***/ in 2nd iteration CS 314, LS, BR, LTM: Imperative Programming 8

list2.c output scherzo!c> gcc list2.c scherzo!c>./a.out 10 9 8 7 6 5 4 3 2 1 scherzo!c> CS 314, LS, BR, LTM: Imperative Programming 9

Review: Stack vs Heap Procedure activations, statically allocated local variables, parameter values Lifetime same as block in which variables are declared Stack frame with each invocation of procedure Dynamically allocated data structures, whose size may not be known in advance Lifetime extends beyond block in which they are created Must be explicitly freed or garbage collected CS 314, LS, BR, LTM: Imperative Programming 10

Heap Storage void *malloc (size_t n) returns pointer to block of contiguous storage of n bytes (chars), if possible if not enough memory left for allocation, malloc returns a NULL pointer So you ALWAYS have to check the return value to allocate storage of a different type requires sending malloc the proper amount of bytes needed and casting the return pointer value appropriately head = (listcell *) malloc(sizeof (listcell)); CS 314, LS, BR, LTM: Imperative Programming 11

listwithfree.c /*sample program to write a linear linked list of integers built like in Java, adding new elements on the front*/ #include<stdio.h> /*this makes these definitions and variables global*/ void *malloc(); void free(); typedef struct cell listcell; struct cell{ }; int num; listcell *next; listcell *head, *ele, *p; Almost same declarations as list2.c CS 314, LS, BR, LTM: Imperative Programming 12

listwithfree.c, cont. main(void) { int j; /*create first node in list*/ head = (listcell *) malloc(sizeof (listcell)); /*now create other entries in list of numbers from 1 to 10*/ head->num = 1; for (j=2; j<11; j++) { ele = (listcell *) malloc(sizeof (listcell)); ele->num = j; ele->next =head; /***/ head = ele; } printf(" values in the list: "); /*now traverse the list and print the elements*/ for (p=head; p!=null; p=p->next) printf("%d ",p->num); printf("\n"); } Same building of the list and printing it out as list2.c CS 314, LS, BR, LTM: Imperative Programming 13

listwithfree.c, cont. /*now delete the first 2 elements of the list and free their storage */ ele = head->next; /*1*/ free (head); /* free 1st list element storage*/ head = ele; ele = head->next; /*2*/ free (head); /* free 2nd list element storage*/ head = ele; /*3*/ /*now traverse the list and print the elements*/ printf(" values in list after two free's: "); for (p=head; p!=null; p=p->next) printf("%d ",p->num); printf("\n"); } CS 314, LS, BR, LTM: Imperative Programming 14

Trace After ele = head->next; /*1*/ ele head 10 9 8... head ele After free (head); head = ele; ele = head->next; /*2*/ 9 8... CS 314, LS, BR, LTM: Imperative Programming 15

listwithfree.c, cont. head ele 9 8... ele head After 8... free (head); head = ele; /*3*/ /* output 221 remus!c> a.out values in the list: 10 9 8 7 6 5 4 3 2 1 values in list after two free's: 8 7 6 5 4 3 2 1 222 remus!c> */ CS 314, LS, BR, LTM: Imperative Programming 16

Functions Prototype: int mult(int, int); int square(int n); often in a *.h or header file used by compiler for type checking Definition: int square(int n) {return n*n;} Invocation: int b = 3; c = square(b); (formal) parameter argument CS 314, LS, BR, LTM: Imperative Programming 17

Functions Parameter Passing uses Call by Value Value of actual argument is copied into formal parameter Example: void swap(int a,int b) {int temp; temp=a; a=b; b=temp;}... int x=3; int y=5; swap(x,y); printf( %d %d,x,y); --> 3 5!?! Why? CS 314, LS, BR, LTM: Imperative Programming 18

Functions To get the effect of Call by Reference use pointers Example: void swap(int *a, int *b) {int temp; temp=*a; *a=*b; *b=temp;}... int x=3; int y=5; swap( &x, &y ); printf( %d %d,x,y); --> 5 3 CS 314, LS, BR, LTM: Imperative Programming 19

scanf int i; double x; scanf( %i %lf, &i, &x); remus> man scanf CS 314, LS, BR, LTM: Imperative Programming 20

Structures Structure: Aggregate object with named fields Declaration: struct EMPLOYEE { int age; double payrate; } joe, mary; struct EMPLOYEE bill = {35, 65000.00}; Accessed as: joe.age = 45; bill.payrate *= 1.1; CS 314, LS, BR, LTM: Imperative Programming 21

Structures Structure may contain actual fields, as opposed to references struct STACK { int top; int contents[5]; } stack1; struct STACK { int top; int *contents; } stack1; CS 314, LS, BR, LTM: Imperative Programming 22

Structures Defining new types: typedef float Celsius; typedef float Fahrenheit; enum {BROWN, BLOND} hishair, herhair; typedef enum {BLACK, BROWN, RED, BLOND, GRAY} HairColors; CS 314, LS, BR, LTM: Imperative Programming 23

Structures Defining new structure types: typedef struct { float x-coord; float y-coord; } Point; struct POLYGON { Point vertices[10]; int N; } p; CS 314, LS, BR, LTM: Imperative Programming 24

Structures Pointers to Structures: typedef struct { int age; double payrate; } Employee; Employee joe, *maryp; Accessed as: (*maryp).payrate = 75000.00; (*maryp).payrate += 5000.00; maryp->payrate += 5000.00; CS 314, LS, BR, LTM: Imperative Programming 25

Recursive Structures This is legal: typedef struct NODE { int data; struct NODE *next; } Cell; These are not legal: typedef struct NODE { int data; struct NODE next; } Cell; typedef struct { int data; Cell *next; } Cell; CS 314, LS, BR, LTM: Imperative Programming 26

Memory Allocation Remember the Stack and the Heap? Parameters and local variables can be allocated on the stack, as part of the stack frame: int n, *pn; pn = &n; But consider: Employee *maryp; If you want to create the Employee structure that is pointed to by maryp, where does it go? On the Heap! CS 314, LS, BR, LTM: Imperative Programming 27

Memory Allocation This is similar to the storage of lists in Scheme, except that you have to do it yourself! Standard allocation procedure: Employee *maryp; maryp = (Employee *) malloc( sizeof(employee) ); Note: malloc returns the NULL pointer if it cannot find enough space. To release memory: free( maryp ); CS 314, LS, BR, LTM: Imperative Programming 28

head Example: list.c ele 1 head 2 1 at /***/ in 1st iteration ele 3 head 2 1 at /***/ in 2nd iteration CS 314, LS, BR, LTM: Imperative Programming 29

Example: listwithfree.c After ele = head->next; /*1*/ ele head 10 9 8... head ele After free (head); head = ele; ele = head->next; /*2*/ 9 8... CS 314, LS, BR, LTM: Imperative Programming 30

Example: listwithfree.c head ele 9 8... ele head After 8... free (head); head = ele; /*3*/ /* output 59 scherzo!c> a.out 10 9 8 7 6 5 4 3 2 1 8 7 6 5 4 3 2 1 60 scherzo!c> */ CS 314, LS, BR, LTM: Imperative Programming 31

Compare: List in Java public class List extends Object { protected Object element; protected List sublist; /** * Create a new List, initially empty. */ public List() { element = null; sublist = null; } /* cons operation */ public List(Object newelement, List oldlist){ element = newelement; sublist = oldlist; }...} CS 314, LS, BR, LTM: Imperative Programming 32

Compare: List in Java List p = new List(); List q = new List( a,p); a List( b,q); b a CS 314, LS, BR, LTM: Imperative Programming 33

Pointers vs.. References Cell and Cell* are different Explicit dereference operator Pointers needed for recursive definitions Casting results in type conversion: int x = (int) (&w); Memory management is usually performed by user Everything is a reference Dereference is implicit Recursive definitions are automatic Casting just satisfies the type checker Memory management is usually automatic CS 314, LS, BR, LTM: Imperative Programming 34

Pointers to Functions Syntax: int foo(int x){ } int (*pf)(int x); pf = &foo; Motivation: Polymorphic Processing e.g., sorting an array of integers in increasing vs. decreasing order e.g., sorting an array of intgers vs. an array of floats vs. an array of Employee structs vs.... How can we do this? CS 314, LS, BR, LTM: Imperative Programming 35

Pointers to Functions Suppose Cell is defined as before, but the data field could be an int, a char, a struct, or... Use a callback function: Cell * searchlist(cell *c, void *value, } int (*compare)(void *, void *)) { for ( ; c!=null; c=c->next){ if ((*compare)(&(c->data), value)) break;} return c; CS 314, LS, BR, LTM: Imperative Programming 36

Arrays Array: Typed collection of values indexed by nonnegative integers. Type and size must be known at compile time, except in the case of formal parameters: int a[20]; void sort(int b[], int n); Arrays in C are one-dimensional: piece chess[8][8]; Strings are arrays of characters: char msg[21] = Enter your password: ; CS 314, LS, BR, LTM: Imperative Programming 37

Arrays and Pointers An array name is considered to be a pointer to its first element. Thus, given the declarations: int A[20], *ip; A is a pointer to A[0] ip = A; and ip = &A[0]; have the same meaning Array subscripting is defined in terms of pointer arithmetic *(A + 3) and A[3] have the same meaning Array names are not variables, but constants A++ and A = ip are illegal CS 314, LS, BR, LTM: Imperative Programming 38

Arrays and Pointers Pointer Arithmetic: int j = 5; int *k = &j; (*k+2) means ((*k)+2) : legal R-value, illegal L-value (k+2) : legal R-value, legal (but not necessarily meaningful) L-value *(k+2) : legal R-value, illegal L-value General Rule: 1024 k Given declaration T *ptr; (ptr + k) points to location ptr + k sizeof(t) 5 j 1024 1032 CS 314, LS, BR, LTM: Imperative Programming 39

Arrays and Pointers Two ways to process the array A[20] with subscripts: int k; for(k=0; k < 20; k++){ A[k] = 0; } with pointers: int *ap; for(ap=a; ap < A+20; ap++){ *ap = 0; } CS 314, LS, BR, LTM: Imperative Programming 40

Strings and Pointers The library package string.h assumes that a string is an array of characters terminated by \0. Thus: char name[5] = Mary ; Example: int strlen(char const *str){ int n=0; for( ; *str!= \0 ; str++) { n+=1;} return n; } CS 314, LS, BR, LTM: Imperative Programming 41

Strings and Pointers Example: char *strcpy(char *dst, char const *src){ char *str = dst; for( ; *src!= \0 ; src++, dst++) {*dst = *src;} *dst = \0 ; return str; } Caution: User must guarantee that dst has been allocated enough memory to store src CS 314, LS, BR, LTM: Imperative Programming 42

Pointers to Functions To search a list of integers using less than, define: int lessthanints(void *a, void *b){ if ( *(int *)a < *(int *)b ) return 1; else return 0; } Then call: Cell *foundit; int k=5; foundit = searchlist(head, &k, lessthanints); CS 314, LS, BR, LTM: Imperative Programming 43

Pointers to Functions To search a list of characters using equals, define: int equalchars(void *a, void *b){ if ( *(char *)a == *(char *)b ) return 1; else return 0; } Then call: Cell *foundit; char ch= a ; foundit = searchlist(head, &ch, equalchars); CS 314, LS, BR, LTM: Imperative Programming 44