QUIZ How do we implement run-time constants and. compile-time constants inside classes?

Similar documents
Ch. 11: References & the Copy-Constructor. - continued -

QUIZ. What is wrong with this code that uses default arguments?

Ch. 3: The C in C++ - Continued -

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

QUIZ. Source:

QUIZ Friends class Y;

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

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

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

QUIZ. What are 3 differences between C and C++ const variables?

QUIZ. How could we disable the automatic creation of copyconstructors

QUIZ How do we implement run-time constants and. compile-time constants inside classes?

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

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

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

CSE 303: Concepts and Tools for Software Development

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

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

MPATE-GE 2618: C Programming for Music Technology. Syllabus

Memory and C++ Pointers

Ch. 12: Operator Overloading

QUIZ. How could we disable the automatic creation of copyconstructors

Why Pointers. Pointers. Pointer Declaration. Two Pointer Operators. What Are Pointers? Memory address POINTERVariable Contents ...

High Performance Computing and Programming, Lecture 3

CS61C : Machine Structures

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

C++ for Java Programmers

Type Checking and Type Equality

EL6483: Brief Overview of C Programming Language

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

Lecture 13: more class, C++ memory management

CS61 Lecture II: Data Representation! with Ruth Fong, Stephen Turban and Evan Gastman! Abstract Machines vs. Real Machines!

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

Lecture Notes on Memory Layout

Class Information ANNOUCEMENTS

Software Design and Analysis for Engineers

Object-Oriented Programming, Iouliia Skliarova

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

G52CPP C++ Programming Lecture 20

BLM2031 Structured Programming. Zeyneb KURT

G Programming Languages - Fall 2012

Pointer Arithmetic and Lexical Scoping. CS449 Spring 2016

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

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

CSC369 Lecture 2. Larry Zhang

a data type is Types

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #1 Examination 12:30 noon, Tuesday, February 14, 2012

C++ for Java Programmers

V850 Calling Convention

G52CPP C++ Programming Lecture 7. Dr Jason Atkin

Overview COMP Microprocessors and Embedded Systems. Lectures 18 : Pointers & Arrays in C/ Assembly

G Programming Languages - Fall 2012

CSCI-1200 Data Structures Spring 2017 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Short Notes of CS201

CSC369 Lecture 2. Larry Zhang, September 21, 2015

Computer Systems Lecture 9

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Parallel Programming: Background Information

Parallel Programming: Background Information

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

CS201 - Introduction to Programming Glossary By

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Object-Oriented Programming, Iouliia Skliarova

Vector and Free Store (Vectors and Arrays)

x86 assembly CS449 Fall 2017

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

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

CSCI-1200 Data Structures Fall 2017 Lecture 5 Pointers, Arrays, & Pointer Arithmetic

CSCI-1200 Data Structures Fall 2012 Lecture 5 Pointers, Arrays, Pointer Arithmetic

Exercise 3 / Ch.7. Given the following array, write code to initialize all the elements to 0: int ed[100]; Hint: It can be done two different ways!

Declaration Syntax. Declarations. Declarators. Declaration Specifiers. Declaration Examples. Declaration Examples. Declarators include:

Pointers, Dynamic Data, and Reference Types

Homework. Reading. Machine Projects. Labs. Intel 8254 Programmable Interval Timer (PIT) Data Sheet. Continue on MP3

Memory Management. CS31 Pascal Van Hentenryck CS031. Lecture 19 1

04-19 Discussion Notes

Important From Last Time

Lecture 2, September 4

CS201- Introduction to Programming Current Quizzes

Page 1. Today. Important From Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right?

SYSTEM CALL IMPLEMENTATION. CS124 Operating Systems Fall , Lecture 14

More C Pointer Dangers

QUIZ. Write the following for the class Bar: Default constructor Constructor Copy-constructor Overloaded assignment oper. Is a destructor needed?

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

CS201 Some Important Definitions

Pointers and Memory 1

Review! * follows a pointer to its value! & gets the address of a variable! Pearce, Summer 2010 UCB! ! int x = 1000; Pearce, Summer 2010 UCB!

CSCI-1200 Data Structures Fall 2018 Lecture 7 Templated Classes & Vector Implementation

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

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

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

Fixed-Point Math and Other Optimizations

Goals of this Lecture

3/7/2018. Sometimes, Knowing Which Thing is Enough. ECE 220: Computer Systems & Programming. Often Want to Group Data Together Conceptually

Fast Introduction to Object Oriented Programming and C++

Functions, Pointers, and the Basics of C++ Classes

CS61C : Machine Structures

Java and C I. CSE 351 Spring Instructor: Ruth Anderson

QUIZ. Can you find 5 errors in this code?

Transcription:

QUIZ How do we implement run-time constants and compile-time constants inside classes?

Compile-time constants in classes The static keyword inside a class means there s only one instance, regardless of how many objects of the class are created User-defined types will be addressed soon! A static const of a built-in type can be treated as a compile-time constant. static consts must be initialized when they are defined (all other data members must be initialized in the constructor or in other member functions)

QUIZ What does the volatile keyword mean in C++?

volatile means This data may change outside the knowledge of the compiler. Somehow, the environment is changing the data (possibly through multitasking, multithreading or interrupts), and volatile tells the compiler not to make any assumptions about that data, especially during optimization.

QUIZ What is the c-v qualifier in C++?

QUIZ What is the c-v qualifier in C++? The syntax of volatile is identical to const, so discussions of the two are often treated together. The two are referred to in combination as the c-v qualifier.

QUIZ Is the destructor called in the right place in this code? Explain!

Yes! Although the destructor is called when the object (f) ends its lifetime, for a static object the lifetime is the entire program!

Ch. 11: References & the Copy-Constructor

Pointers in C and C++ The void pointer assignment trick in C C++ is more strongly typed examples

C C++? Actually, in most C compilers, the direct assignment fptr = iptr; works!

C

QUIZ C++ Make it work with a C++ explicit cast!

? It s really a denormalized number in floating-point representation (IEEE 754).

References A reference (&) is like a constant pointer that is automatically dereferenced. One advantage of this pointer is that you never have to wonder whether it s been initialized (the compiler enforces it) and how to dereference it (the compiler does it). examples

References in function arguments and return values When a reference is used as a function argument, any modification to the reference inside the function will cause changes to the argument outside the function. Of course, you could do the same thing by passing a pointer, but a reference has much cleaner syntax. (You can think of a reference as nothing more than a syntax convenience, if you want.) examples

Frequent mistake: Returning a reference (or pointer!) to an object whose scope is inside the function:

const references const Will this function compile?

If you know the function will respect the constness of an object, making the argument a const reference will allow the function to be used in all situations. This means that: for built-in types, the function will not modify the argument for user-defined types, the function will call only const member functions, and won t modify any public data members. EOL 25

QUIZ Is this program correct? Explain!

QUIZ Is this program correct? Explain!

QUIZ Is this program correct? Explain!

Here s how it can go wrong:

This can happen with references as well, as explained last time: Frequent mistake: Returning a reference (or pointer!) to an object whose scope is inside the function: This is also known as leaked pointers/references.

QUIZ One of the function calls is incorrect. Which one and why?

QUIZ Why would we want to pass a pointer-to-pointer in C?

In C++, we can pass a pointer reference (reference to a pointer) instead! Make sure you grok this pointer arithmetic!

The copy-constructor Passing & returning by value a.k.a. A little bit of assembly language and computer architecture

The definition of function f is elsewhere We re showing the (simplified) assembly code that implements this line Arguments pushed on stack from right to left Beginning address of f() - code is loaded in Program Counter. When f() - code returns, execution continues here.

Not sure why text has 4 here depends on the sizes for int and char on this machine! In cdecl convention, the caller is responsible for cleaning up, i.e. restoring the stack. Do not confuse with the variable a. Functions return values in a specific register, known to the compiler, e.g. in an Intel CPU this is EAX.

Stack before f() is called b a

The complete picture: Function-call stack frame Stack before f() is called b a Return address Local variables

How does this change when passing a (large) userdefined object by value? Stack before f() is called b a Return address Local variables

Array of 100 chars! The question, however is: How to return the result? (There aren t enough registers!)

One possible solution is to also push the return values on the stack: Array of 100 chars! Array of 100 chars! Return values

Historical note: In K&R C, it was not possible to return structs, only pointers to structs. Since C89, C functions can return structs, although many compilers don t actually place the struct on the stack they do return values optimization instead (where only a pointer is returned, e.g. in register eax). Not in text This means that the caller is responsible for setting up space for return values, contrary to C custom! Array of 100 chars! Array of 100 chars! Return values

However, the solution chosen in C++ is this: the address of B2 is pushed before making the call (even though it s not an argument). B2 s address Array of 100 chars!

To comprehend why pushing return values on the stack is not easy, we need to understand the constraints on the compiler when it s making a function call.

Re-entrancy Interrupts and ISRs = Interrupt Service Routines (functions, really!) Recursive functions

Re-entrancy Consider the following scenario: Function pushes return values on stack and returns normally Before the calling function has a chance to process the return values, an interrupt occurs, and the ISR overwrites them! Return values ISRs are smart enough to save all registers on the stack, and restore them upon exit, but they have their limits.

Re-entrancy Would placing the return values on the heap work? Why not? Return values

Conclusion: Push the address of the return value s destination on the stack as a hidden argument Let the function copy the return value into the destination before returning. B2 s address

We stopped before the subsection Bitcopy vs. initialization This is all the material required for the 2nd exam. The exam is this Friday (Oct.30) during lab. Material: chs. 5, 6, 7, 8, and 11 (partial). Friday lecture is review.

Ch.11 exercises for review: End-of-chapter 1 through 9. What else you can do for review: Finish the homework for Ch.8 (due Friday!) Re-solve all quizzes from chs. 5, 6, 7, 8, 11. The instructor s notes are on our webpage. EOL 26