Abstraction, Modularity, Interfaces and Pointers Original slides by Noah Mendelsohn, including content from Mark Sheldon, Noah Daniels, Norman Ramsey COMP.

Slides:



Advertisements
Similar presentations
1
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 1 The Study of Body Function Image PowerPoint
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
Properties Use, share, or modify this drill on mathematic properties. There is too much material for a single class, so you’ll have to select for your.
UNITED NATIONS Shipment Details Report – January 2006.
Chapter 3: Linked List Tutor: Angie Hui
Writing Pseudocode And Making a Flow Chart A Number Guessing Game
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 5: Repetition and Loop Statements Problem Solving & Program.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
1 RA I Sub-Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Casablanca, Morocco, 20 – 22 December 2005 Status of observing programmes in RA I.
Properties of Real Numbers CommutativeAssociativeDistributive Identity + × Inverse + ×
Exit a Customer Chapter 8. Exit a Customer 8-2 Objectives Perform exit summary process consisting of the following steps: Review service records Close.
Custom Statutory Programs Chapter 3. Customary Statutory Programs and Titles 3-2 Objectives Add Local Statutory Programs Create Customer Application For.
FACTORING ax2 + bx + c Think “unfoil” Work down, Show all steps.
Mike Scott University of Texas at Austin
1 Click here to End Presentation Software: Installation and Updates Internet Download CD release NACIS Updates.
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Chapter 7: Arrays In this chapter, you will learn about
1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure Definitions 10.3Initializing Structures 10.4Accessing.
The Racing Game of Knowledge Continue Questions – push on trees
Turing Machines.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
PP Test Review Sections 6-1 to 6-6
Chapter 17 Linked Lists.
Chapter 24 Lists, Stacks, and Queues
Modern Programming Languages, 2nd ed.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Bright Futures Guidelines Priorities and Screening Tables
1 Symbol Tables Chapter Sedgewick. 2 Symbol Tables Searching Searching is a fundamental element of many computational tasks looking up a name.
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
1 RA III - Regional Training Seminar on CLIMAT&CLIMAT TEMP Reporting Buenos Aires, Argentina, 25 – 27 October 2006 Status of observing programmes in RA.
Basel-ICU-Journal Challenge18/20/ Basel-ICU-Journal Challenge8/20/2014.
1..
CONTROL VISION Set-up. Step 1 Step 2 Step 3 Step 5 Step 4.
© 2012 National Heart Foundation of Australia. Slide 2.
Procedures. 2 Procedure Definition A procedure is a mechanism for abstracting a group of related operations into a single operation that can be used repeatedly.
While Loop Lesson CS1313 Spring while Loop Outline 1.while Loop Outline 2.while Loop Example #1 3.while Loop Example #2 4.while Loop Example #3.
Note to the teacher: Was 28. A. to B. you C. said D. on Note to the teacher: Make this slide correct answer be C and sound to be “said”. to said you on.
Solving the eValue Rubik’s cube
Analyzing Genes and Genomes
Lilian Blot CORE ELEMENTS SELECTION & FUNCTIONS Lecture 3 Autumn 2014 TPOP 1.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Pointers and Arrays Chapter 12
Essential Cell Biology
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
Testing Hypotheses About Proportions
Copyright 2013 – Noah Mendelsohn Compiling C Programs Noah Mendelsohn Tufts University Web:
Immunobiology: The Immune System in Health & Disease Sixth Edition
Energy Generation in Mitochondria and Chlorplasts
Insertion Sort Introduction to Algorithms Insertion Sort CSE 680 Prof. Roger Crawfis.
Murach’s OS/390 and z/OS JCLChapter 16, Slide 1 © 2002, Mike Murach & Associates, Inc.
Techniques for proving programs with pointers A. Tikhomirov.
User Defined Functions Lesson 1 CS1313 Fall User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough #1.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
Data Structures Using C++ 2E
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Original slides by Noah Mendelsohn Tufts University
More on Abstract Data Types Noah Mendelsohn Tufts University Web: COMP 40: Machine.
More on Abstract Data Types Noah Mendelsohn and Mark Sheldon Tufts University Web:
Presentation transcript:

Abstraction, Modularity, Interfaces and Pointers Original slides by Noah Mendelsohn, including content from Mark Sheldon, Noah Daniels, Norman Ramsey COMP 40: Machine Structure and Assembly Language Programming

© 2010 Noah Mendelsohn We will weave together a story about  Abstraction, modularity, reuse, clean interfaces, information hiding  C Language Pointers Is it obvious these are related at all?

© 2010 Noah Mendelsohn Abstract and Concrete Types

© 2010 Noah Mendelsohn Software structures model real world objects and concepts  Integers  Students  Bank statements  Photographic images  Sound recordings  Etc. 4 These things aren’t bits They don’t live in computers, but…

© 2010 Noah Mendelsohn Software structures model real world objects and concepts  Integers  Students  Bank statements  Photographic images  Sound recordings  Etc. 5 We build data structures that model them

© 2010 Noah Mendelsohn Collections are especially interesting  Arrays of Integer temperature measurements  Queues of Students waiting to register for classes  Sorted lists of Bank statements  Etc. 6

© 2010 Noah Mendelsohn 7 Today we will focus on good ways of modeling Abstract Data Types like lists, sets, queues, etc. Doing that, we’ll learn a lot about programming with pointers and types in C

© 2010 Noah Mendelsohn Key principles to watch for  Abstraction –Refers to the act of “pulling away from” something so that unimportant details are hidden” 1  Modularity & clean interfaces –Each module “does one thing well” 2  Information hiding –Modules keep details of their internals secret –This makes them more modular: why?  Generalization –When practical, each module or service should be as generally useful as possible 8 1 Prof. Mark Sheldon – COMP 11 Big IdeasCOMP 11 Big Ideas 2 Ken Thompson, co-inventor of Unix – The Unix PhilosophyThe Unix Philosophy

© 2010 Noah Mendelsohn What are the benefits of software like this?  Easier to use  Easier to reuse  Easier to replace  Easier to reason about correctness  Easer to test  Easier to maintain  Etc., etc. 9

© 2010 Noah Mendelsohn Abstraction Clean Interfaces Information Hiding

© 2010 Noah Mendelsohn Interfaces (Recall picture from Comp 11) 11 Interface Implementation Client E.g. your fgroups program E.g. implementation of Hanson Table_T E.g. methods in Hanson table.h

© 2010 Noah Mendelsohn Interfaces 12 Interface Implementation Client Many different programs can re-use the interface and the implementation!

© 2010 Noah Mendelsohn Interfaces 13 Interface Implementation Client The implementation chooses a representation … NOT visible to the client

© 2010 Noah Mendelsohn Interfaces 14 Interface Implementation Client We can build different implementations without the client knowing Why reimplement? One might be small, one might be fast, etc. Bug fixes We might figure out a better way next year

© 2010 Noah Mendelsohn Information Hiding

© 2010 Noah Mendelsohn Interfaces 16 Interface Implementation Client To make this work we must… …hide details of the implementation from the client!

© 2010 Noah Mendelsohn Interfaces 17 Interface Implementation Client To make this work we must… …hide details of the client from the implementation!

© 2010 Noah Mendelsohn Interfaces 18 Interface Implementation Client Build interfaces that cleanly model the service or abstract data…

© 2010 Noah Mendelsohn 19 In your COMP 40 design documents, we expect you to carefully observe these distinctions… …your description of an interface should never mention implementation details …your description of use of an interface should never mention implementation details.

© 2010 Noah Mendelsohn Using Pointers to Represent Objects and Keep Secrets

© 2010 Noah Mendelsohn Most pointer declarations include type of the referent 21 #include int main(int argc, char *argv[]) { (void)argc; (void)argv; int x = 3; int y; int *ptr; ptr = &x; y = *ptr; printf("y = %d\n", y); exit(EXIT_SUCCESS); } Pointer to an integer

© 2010 Noah Mendelsohn 22 #include int main(int argc, char *argv[]) { (void)argc; (void)argv; int x = 3; int y; void *ptr; ptr = &x; y = *ptr; printf("y = %d\n", y); exit(EXIT_SUCCESS); } Void pointers are to unknown type void * Compiler knows it’s a pointer. Compiler does not know what type it points to.

© 2010 Noah Mendelsohn Void pointers are to unknown type 23 #include int main(int argc, char *argv[]) { (void)argc; (void)argv; int x = 3; int y; void *ptr; ptr = &x; y = *ptr; printf("y = %d\n", y); exit(EXIT_SUCCESS); } Compile error!! Can’t assign value of unknown type (*ptr) to integer y. void * Compiler knows it’s a pointer. Compiler does not know what type it points to.

© 2010 Noah Mendelsohn But we can tell the compiler the type 24 #include int main(int argc, char *argv[]) { (void)argc; (void)argv; int x = 3; int y; void *ptr; ptr = &x; y = *((int *)ptr); printf("y = %d\n", y); exit(EXIT_SUCCESS); } FIXED! We can use a cast (in this case ((int *)ptr) to tell the compiler the type it should assume

© 2010 Noah Mendelsohn Void pointers let us do some very important things!  Make our code more general – work with any type of data  Keep secrets 25 But why would we want to keep secrets?

© 2010 Noah Mendelsohn Interfaces 26 Interface Implementation Client E.g. your fgroups program E.g. implementation of Hanson Table_T E.g. methods in Hanson table.h My client isn’t supposed to see internals of Hanson’s table, but Hanson needs to give us something….what can we use?

© 2010 Noah Mendelsohn Interfaces 27 Interface Implementation Client E.g. your fgroups program E.g. implementation of Hanson Table_T E.g. methods in Hanson table.h My_table = Table_new(… … …) Q. What can Table_new return? I will show you two ways of doing this A.A pointer to data you can’t look at You will use this pointer to identify the new table

© 2010 Noah Mendelsohn Using void * pointers 28 /* Declare the type Table_t to be a (void *) */ typedef void *Table_t; /* Define Table_new to return one of those (void *) */ Table_t Table_new(… … … …); This is a pretty good solution: Client can’t see details of what’s in Hanson’s structure When Hanson gets the pointer back, he can assign it to a Table_T and use it

© 2010 Noah Mendelsohn Client doesn’t know secret implementation 29 Interface Implementation Client my_table = Table_new(… … …) Struct Table_t { …data declartions… } struct Table_T *table_ptr; void *table_ptr;

© 2010 Noah Mendelsohn Using void * pointers 30 /* Declare the type Table_t to be a (void *) */ typedef void *Table_t; /* Define Table_new to return a (void *) */ Table_t Table_new(… … … …); This is a pretty good solution: Client can’t see details of what’s in Hanson’s structure When Hanson gets the pointer back, he can assign it to a Table_T and use it But it doesn’t catch this mistake in the client… myTable = List_new(…); Table_put(myTable, … … …)

© 2010 Noah Mendelsohn We can do better with incomplete structures 31 typedef struct Table_T *Table_t; Tricky…we’re exposing the structure’s name, but not it’s contents…C calls this an incomplete structure. You cannot declare a variable with an incomplete type. You can declare a variable that’s a pointer to an incomplete type! Declare the type Table_t to be a pointer to a struct whose contents are unspecified:

© 2010 Noah Mendelsohn We can do better with incomplete structures 32 typedef struct Table_T *Table_t; /* Table_new returns ptr to incomplete struct */ Table_t Table_new(… … … …); Tricky…we’re exposing the structure’s name, but not it’s contents…C calls this an incomplete structure.

© 2010 Noah Mendelsohn Client doesn’t know secret implementation 33 Interface Implementation Client my_table = Table_new(… … …) Struct Table_t { …data declartions… } struct Table_T *table_ptr; struct Table_t *table_ptr; Client has incomplete declaration of the struct

© 2010 Noah Mendelsohn We can do better with incomplete structures 34 typedef struct Table_T *Table_t; Table_t Table_new(… … … …); This is a better solution: Client can’t see details of what’s in Hanson’s structure When Hanson gets the pointer back, it just works It does catch this mistake…you should figure out why! myTable = List_new(…); Table_put(myTable, … … …)

© 2010 Noah Mendelsohn Generalization

© 2010 Noah Mendelsohn You’ve already seen some generalization 36 int square3() { return 3 * 3; } We don’t do this int square(int n) { return n * n; } We do this! Generalize over input value Can we generalize over the type of information?

© 2010 Noah Mendelsohn We need to generalize over types 37 List_of_students_new(…); List_of_cars_new(…); List_of_bank_stmts_new(…); We don’t want this List_new(…); We want this! How do we declare the input to List_push()? (after all, its type could be anything) Can we generalize over the type of information?

© 2010 Noah Mendelsohn Void * allows us to generalize over types 38 void List_push(List_T list, void *x); Hanson’s declaration for List_push The list will remember a pointer to anything. struct Car {char *brand; int weight;}; typedef struct Car Car; List_T mylist = List_list(NULL); Car *retrieved_car; Car mycar = {"ford", 2000}; mylist = List_push(mylist, &mycar); mylist = List_pop(mylist, (void **)&retrieved_car); List_free(&mylist); Any pointer can be passed to a pointer-to-void parameter

© 2010 Noah Mendelsohn Void * allows us to generalize over types 39 void List_push(List_T list, void *x); Hanson’s declaration for List_push struct Car {char *brand; int weight;}; typedef struct Car Car; List_T mylist = List_list(NULL); Car *retrieved_car; Car mycar = {"ford", 2000}; mylist = List_push(mylist, &mycar); mylist = List_pop(mylist, (void **)&retrieved_car); List_free(&mylist); IMPORTANT: Retrieved_car_p is already a pointer. Why do we have to pass the address of retrieved_car_p?

© 2010 Noah Mendelsohn Void * allows us to generalize over types 40 void List_push(List_T list, void *x); Hanson’s declaration for List_push struct Car {char *brand; int weight;}; typedef struct Car Car; List_T mylist = List_list(NULL); Car *retrieved_car; Car mycar = {"ford", 2000}; mylist = List_push(mylist, &mycar); mylist = List_pop(mylist, (void **)&retrieved_car); List_free(&mylist); This generalization is known as universal polymorphism

© 2010 Noah Mendelsohn Review  Abstraction, modularity, reuse, clean interfaces, information hiding  C Language Pointers It should be clear now that these are related