CSE 333 Systems Programming Hal Perkins Winter 2016 Bonus Lecture– Function Pointers and Objects in C.

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
Advertisements

Polymorphism, Virtual Methods and Abstract Classes.
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #17, March 12, 2007 Procedure Abstraction, Name Spaces, Scoping Rules, Activation Records,
OOP in C. Cfront Original C++ compiler was just a pre- processor –generated C code which could be compiled with an existing C Compiler.
Approaches to Typing Programming Languages Robert Dewar.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Intro to Generic Programming Templates and Vectors.
Abstract classes and Interfaces. Abstract classes.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Lecture 22 Miscellaneous Topics 4 + Memory Allocation.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
CSC 142 O 1 CSC 142 Java More About Inheritance & Interfaces [Reading: chapter 13]
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Inheritence Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
Programming Languages by Ravi Sethi Chapter 6: Groupings of Data and Operations.
C# F 1 CSC 298 Object Oriented Programming (Part 1)
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
11/12/2015© Hal Perkins & UW CSEL-1 CSE P 501 – Compilers Code Shape II – Objects & Classes Hal Perkins Winter 2008.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
Spring 2014Jim Hogg - UW - CSE - P501L-1 CSE P501 – Compiler Construction Object layout Field access What is this ? Object creation - new Method calls.
CSE 425: Control Abstraction I Functions vs. Procedures It is useful to differentiate functions vs. procedures –Procedures have side effects but usually.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Object-Oriented Programming Chapter Chapter
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
ISBN Object-Oriented Programming Chapter Chapter
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Chapter -6 Polymorphism
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 10 – C: the heap and manual memory management.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 12 – C: structs, linked lists, and casts.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Interfaces, Mixins, & Multiple Inheritance CSE 413 Autumn 2008 Credit: Dan Grossman, CSE341, Sp08.
Overview of C++ Polymorphism
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
1 Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
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.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CSE 332: C++ Overloading Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
LECTURE 19 Subroutines and Parameter Passing. ABSTRACTION Recall: Abstraction is the process by which we can hide larger or more complex code fragments.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Windows Programming Lecture 03. Pointers and Arrays.
Design issues for Object-Oriented Languages
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Section 11: Comparing Java and C
Learning Objectives Pointers as dada members
Names and Attributes Names are a key programming language feature
CSE 374 Programming Concepts & Tools
CSE 374 Programming Concepts & Tools
Code Shape II – Objects & Classes Hal Perkins Autumn 2011
Code Shape II – Objects & Classes Hal Perkins Autumn 2005
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Code Shape II – Objects & Classes Hal Perkins Summer 2004
Java Programming Language
A simple function.
Lecture 10 Concepts of Programming Languages
Code Shape II – Objects & Classes Hal Perkins Autumn 2009
Activation records Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section
Computer Science II for Majors
Exam Topics Hal Perkins Winter 2008
Presentation transcript:

CSE 333 Systems Programming Hal Perkins Winter 2016 Bonus Lecture– Function Pointers and Objects in C

Reminders Project due tomorrow night, 11 pm Final exam Wed., 2:30-4:20 –Review & wrapup in section tomorrow –Last-minute Q&A Tue. 4:30, GUG 218 –Topic list + old exams on the web Biased towards stuff since the midterm, but everything is fair game 2

Agenda Function pointers in C/C++ (review/reminder) Objects in C – what is “this” anyway? Objects in C – virtual functions / dynamic dispatch 3

Function pointers (reminder) “Pointers to code” are almost as useful as “pointers to data”. (But the syntax is painful in C.) (Somewhat silly) example: void app_arr(int len, int * arr, int (*f)(int)) { for(int k = 0; k < len; k++) arr[k] = (*f)(arr[k]); } int twox(int i) { return 2*i; } int sqr(int i) { return i*i; } void twoXarr(int len, int* arr) {app_arr(len,arr,&twox);} void sqr_arr(int len, int* arr) { app_arr(len,arr,&sqr); } 4

C function-pointer syntax C syntax: painful and confusing. Rough idea: The compiler “knows” what is code and what is a pointer to code, so you can write less than we did on the last slide: arr[k] = (*f)(arr[k]);  arr[k] = f(arr[k]); app_arr(len,arr,&twox);  app_arr(len,arr,twox); A function pointer in C/C++ is just the address of the first instruction of the function body Typedefs make function-pointer declarations less painful Examples: Compute integral with (pointer to) function to integrate and bounds as parameters (int1.c, int2.c) 5

Objects in C++ What is an object? –Simplest answer: a collection of data and functions (methods) to provide behavior –Methods can reference instance variables as simple names if unambiguous, or as this->name (always) –see thing1.cc, thing2.cc Only non-virtual (static dispatch) for now 6

So what is “this” anyway? In C++ this is a pointer to the current object when a member function is called If the object has type T, “this” has type T* But how does it really work? There are no “this” pointers in the x86-64 instruction set… Answer: the compiler translates member functions to ordinary x86-64 code, and adds an implicit, hidden “this” parameter to every member function definition and call 7

Source-level view What you write: int getX() { return x_ } void setX(int x) { x_ = x; } … n= t1.getX(); t2->setX(333); What you really get: getX(Thing *this) { return this->x_ } void setX(Thing *this, int x) { this->x_ = x; } … n= t1.getX(&t1); t2->setX(t2, 333); 8 See thing.c

What is an object, really? Methods (behavior, functions) + state (instance vars) Actual representation (per object) –pointer to class vtable –state (instance vars) Vtables –One per class –Pointers to all virtual methods for that class (either inherited or overridden/added by class) Virtual function call – indirect through vtable Non-virtual function call – resolved using static type of variable that references the object 9

Compiling obj.m(arguments) 1.Determine (static) type of obj from variable declaration or expression type. Call it T. 2.Verify that type T has a suitable method m with correct number and types of parameters. –If more than one such method use overloading rules to pick correct one. Reject as ambiguous if no unique “best” match. 3.Generate function call –If method m is not virtual, call T::m –If method m is virtual, call m indirectly via vtable pointer in obj (obj->vtbl->m(args)) 10

Examples widget.cc – C++ code with class, derived class, and mix of virtual and non-virtual functions widget.c – same program in C with explicit vtables (structs with pointers to functiosn) and vtable pointers in objects 11