1 Function Pointers and Abstract Data Types Professor Jennifer Rexford COS 217.

Slides:



Advertisements
Similar presentations
Numeric Types & Ranges. ASCII Integral Type Numerical Inaccuracies Representational error – Round-off error – Caused by coding a real number as a finite.
Advertisements

Introduction to C Programming
Pointers.
C Language.
Programming and Data Structure
Chapter 17 Templates. Generic Algorithms Algorithms in which the actions or steps are defined, but the data types of the items being manipulated are not.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Names and Bindings.
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter 10.
1 Generics Professor Jennifer Rexford
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Introduction.
CS 201 Functions Debzani Deb.
Using Templates Object-Oriented Programming Using C++ Second Edition 11.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
CS100A, Fall 1997, Lectures 221 CS100A, Fall 1997 Lecture 22, Tuesday 18 November Introduction To C Goal: Acquire a reading knowledge of basic C. Concepts:
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
1 Variables, Pointers, and Arrays Professor Jennifer Rexford COS 217
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
C++ fundamentals.
Arrays and Pointers in C Alan L. Cox
Data Structures 1- Course Syllabus. 2- Introduction about Data Structures.
Review of C++ Programming Part II Sheng-Fang Huang.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Review C++ exception handling mechanism Try-throw-catch block How does it work What is exception specification? What if a exception is not caught?
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
IT253: Computer Organization Lecture 3: Memory and Bit Operations Tonga Institute of Higher Education.
Object Oriented Programming Elhanan Borenstein copyrights © Elhanan Borenstein.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
JAVA 0. HAFTA Algorithms FOURTH EDITION Robert Sedgewick and Kevin Wayne Princeton University.
Data Types. Data types Data type tells the type of data, that you are going to store in memory. It gives the information to compiler that how much memory.
Week 14 - Monday.  What did we talk about last time?  Introduction to C++  Input and output  Functions  Overloadable  Default parameters  Pass.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Chapter 11: Pointers Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 11 Pointers.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
Introduction to Programming
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CS 261 – Data Structures Introduction to C Programming.
1 Using Templates COSC 1567 C++ Programming Lecture 10.
Generic lists Vassilis Athitsos. Problems With Textbook Interface? Suppose that we fix the first problem, and we can have multiple stacks. Can we have.
Slide 1 Chapter 5 Arrays. Slide 2 Learning Objectives  Introduction to Arrays  Declaring and referencing arrays  For-loops and arrays  Arrays in memory.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
Pointers & References. Pointers Pointer arithmetic Pointers and arrays Pointer-related typedef’s Pointers and const References.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
1 Applied Arrays Lists and Strings Chapter 12 2 Applying What You Learn Searching through arrays efficiently Sorting arrays Using character arrays as.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1 Memory, Arrays & Pointers. Memory 2 int main() { char c; int i,j; double x; cijx.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
1 Function Pointers and Abstract Data Types CS 217.
The C++ Data Types Fundamental Data Types
Types CSCE 314 Spring 2016.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Introduction to Abstract Data Types
Introduction to Data Structure
CS150 Introduction to Computer Science 1
Java Programming Language
Chapter 9: Pointers and String
Chapter 9: Pointers and String
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

1 Function Pointers and Abstract Data Types Professor Jennifer Rexford COS 217

2 Goals of Today’s Lecture Data types  Type conversion  Working around the limitations of types Function pointers  Sorting an array of integers, or of strings  Sorting an array of any type –Void pointers and casting –Pointers to functions Abstract Data Types  Making “array” an ADT

3 Data Types Ultimately, all data is represented as bits  Usually manipulated at the level of bytes or words  Can be interpreted by machine code instructions A type is attribute of a piece of data  Tells the computer something about the data  Constraints on the data –What values it can take –What operations may be performed Ultimately determines what machine code is run  Compiler generates machine code appropriate to the type, to interpret the bits (or bytes) as intended

4 Type Conversion Changing an entity of one data type to another  For more efficient storage (values from more limited set) –E.g., int from getchar() stored later as a char –E.g., double as an int when no fractional part  To enable operations not otherwise possible –E.g., square root of an integer input Implicit vs. explicit conversion  Coercion: implicit conversion by the compiler –E.g., sqrt(2) implicitly sqrt((double) 2)  Casting: explicit conversion specified by programmer –E.g, (int) f to extract integer portion of f

5 Casting: Explicit Conversions Sometimes useful to make conversion explicit  Documentation: making implicit type conversions explicit –E.g., getting the integer part of a floating-point number –Done by int_part = (int) float_number;  Control: overrule compiler by forcing conversions –E.g., getting fractional part of a floating-point number –Done by frac_part = f – (int) f;

6 Writing Software for Various Types But, sometimes types get in our way  Limiting the generality of a function we write  So that it can only run for data of a single type Void pointers as a way out  Store and manipulate data indirectly, using pointers  Manipulating the addresses where data are stored  … rather than the data directly But, sometimes type-specific operations are needed  E.g., comparison operator (“>” for int, strcmp() for strings) Function pointers are a way out  Rather than putting type-specific code in the function  Pass a pointer to another function for type-specific actions  … and simply call the function where needed

7 Sorting an Array of Integers Example problem  Input: array v of n integers  Output: array in sorted order, from smallest to largest Many ways to sort, but three common aspects  Comparison between any two elements  Exchange to reverse the order of two elements  Algorithm making comparisons and exchanges till done Simple approach  Go one by one through the n array elements  By the end of step i, get i th smallest value in element i –Compare element i with all elements after it –Swap values if the i th element is larger

8 Integer Sorting Example v[0] > v[1]? Yes, swap 9627v[0] > v[2]? 9627v[0] > v[3]? 9627v[1] > v[2]? 9627v[1] > v[3]? Yes, swap 9726 …

9 Integer Sorting Function void sort(int *v, int n) { int i, j; for (i = 0; i < n; i++) { for (j = i+1; j < n; j++) { if (v[i] > v[j]) { int swap = v[i]; v[i] = v[j]; v[j] = swap; } comparison swap

10 Sorting an Array of Strings Data types are different  Array elements are char*  Swap variable is char* Comparison operator is different  The greater-than (“>”) sign does not work  Need to use strcmp() function instead “the” “quick” “brown” “fox” “the” “quick” “brown” “fox”

11 String Sorting Function void sort(char *v[], int n) { int i, j; for (i = 0; i < n; i++) { for (j = i+1; j < n; j++) { if (strcmp(v[i], v[j]) > 0) { char* swap = v[i]; v[i] = v[j]; v[j] = swap; }

12 Creating a Generic Function Generic function  A single sort() function that works for all data types C’s notion of data types is getting in our way  We need to accept parameters in any type –sort(int *v, int n) is only good for integer arrays –sort(char *v[], int n) is only good for string arrays  We need to have local variables of any type –int swap is only good for swapping integers –char* swap is only good for swapping strings Different types need different comparison operators  Greater-than sign (“ > ”) is only good for numerical types  strcmp() is only good for strings  We need to be able to tell sort() what comparison function to use

13 Generalizing: Void Pointers Generic pointers are the same as any other pointer  Except they point to a variable with no specific type  Example: void *datap = “CS217”; Difference:  Regular pointers: compilers “know” what they point to  void pointers: compilers “don’t know” what they point to Common uses:  Abstract data types supporting polymorphism*  Pass pointer to function that could be any of several types Memory 0x x xFFFFFFFF `C``S``2``1` `7``\0` datap * Allowing the same definitions to be used with different types of data

14 Void Pointers in Sort Function parameters  Input: array of pointers to some unknown type Local swap variable  Pointer to some unknown type But, what about the comparison step?  Need to be able to pass a function to sort void *swap = v[i]; v[i] = v[j]; v[j] = swap; void sort(void *v[], int n)

15 Generic Sort Function void sort(void *v[], int n, int (*compare)(void *datap1, void *datap2)) { int i, j; for (i = 0; i < n; i++) { for (j = i+1; j < n; j++) { if ((*compare)(v[i], v[j]) > 0) { void *swap = v[i]; v[i] = v[j]; v[j] = swap; } compare is a pointer to a function that has two void* arguments and returns an int, and (*compare) is the function.

16 Using Generic Sort With String #include #include “sort.h” int main(void) { char* w[4] = {“the”, “quick”, “brown”, “fox”}; sort((void **) w, 4, (int (*)(void*,void*)) strcmp); … } pointer to a function

17 Using Generic Sort With Integers #include #include “sort.h” int CompareInts(void *datap1, void *datap2) { int *intp1 = (int *) datap1; int *intp2 = (int *) datap2; return (*intp1 - *intp2); } int main(void) { int* w[4]; w[0] = malloc(sizeof(int)); *(w[0]) = 7; … sort((void **) w, 4, (int (*)(void*,void*))CompareInts); … } pointer to a function

18 Making “Array” an ADT Arrays in C are error prone  Access elements before the array starts (e.g., v[-1] )  Access elements past the end of array (e.g., v[n] )  Modify the variable that keeps track of size (e.g., n ) Protect programmers with an array ADT  Create and delete an array  Get the current length  Read an array element  Append, replace, remove  Sort

19 Array ADT: Interface typedef struct Array *Array_T; Array_T Array_new(void); void Array_free(Array_T array); int Array_getLength(Array_T array); void *Array_getData(Array_T array, int index); void Array_append(Array_T array, void *datap); void Array_replace(Array_T array, int index, void *datap); void Array_remove(Array_T array, int index); void Array_sort(Array_T array, int (*compare)(void *datap1, void *datap2)); array.h client does not know implementation

20 Client Using Array ADT: Strings #include “array.h” #include int main(void) { Array_T array; int i; array = Array_new(); Array_append(array, (void *) “COS217”); Array_append(array, (void *) “IS”); Array_append(array, (void *) “FUN”); for (i = 0; i < Array_getLength(array); i++) { char *str = (char *) Array_getData(array, i); printf(str); } Array_free(array); return 0; }

21 Client Using Array ADT: Integers #include “array.h” #include int main(void) { Array_T array; int one=1, two=2, three=3; int i; array = Array_new(); Array_append(array, (void *) &one); Array_append(array, (void *) &two); Array_append(array, (void *) &three); for (i = 0; i < Array_getLength(array); i++) { int *datap = (int *) Array_getData(array, i); printf(“%d “, *datap); } Array_free(array); return 0; }

22 Array ADT Implementation #include “array.h” enum {MAX_ELEMENTS = 128}; struct Array { void *elements[MAX_ELEMENTS]; int num_elements; }; Array_T Array_new(void) { Array_T array = malloc(sizeof(struct Array)); array->num_elements = 0; return array; } void Array_free(Array_T array) { free(array); }

23 Array ADT Implementation (Cont) int Array_getLength(Array_T array) { return array->num_elements; } void *Array_getData(Array_T array, int index) { return array->elements[index]; } void Array_append(Array_T array, void *datap) { int index = array->num_elements; array->elements[index] = datap; array->num_elements++; } void Array_replace(Array_T array, int index, void *datap) { array->elements[index] = datap; }

24 Array ADT Implementation (Cont.) void Array_insert(Array_T array, int index, void *datap) { int i; /* Shift elements to the right to make room for new entry */ for (i = array->num_elements; i > index; i--) array->elements[i] = array->elements[i-1]; /* Add the new element in the now-free location */ array->elements[index] = str; array->num_elements++; } void Array_remove(Array_T array, int index) { int i; /* Shift elements to the left to overwrite freed spot */ for (i = index+1; i num_elements; i++) array->elements[i-1] = array->elements[i]; array->num_elements--; }

25 Array ADT Implementation (Cont.) void Array_sort(Array_T array, int (*compare)(void *datap1, void *datap2)) { int i, j; for (i = 0; i num_elements; i++) { for (j = i+1; j num_elements; j++) { if ((*compare)(array->elements[i], array->elements[j]) > 0) { void *swap = array->elements[i]; array->elements[i] = array->elements[j]; array->elements[j] = swap; }

26 Summary Module supporting operations on single data structure  Interface declares operations, not data structure  Interface provides access to simple, complete set of operations  Interface provides flexibility and extensibility Trick is providing functionality AND generality  Take advantage of features of programming language  Such as void pointers and function pointers Advantages  Provide complete set of commonly used functions (re-use)  Implementation is hidden from client (encapsulation)  Can use for multiple types (polymorphism)