Operating Systems - Intro to C++
-
Upload
emery-berger -
Category
Technology
-
view
1.814 -
download
3
description
Transcript of Operating Systems - Intro to C++
![Page 1: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/1.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Emery Berger
University of Massachusetts Amherst
Operating SystemsCMPSCI 377
C/C++
![Page 2: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/2.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Why C?
2
![Page 3: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/3.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Why C?
Low-level
Direct access to memory
WYSIWYG (more or less)
Effectively no runtime system
No garbage collector
No other threads
No “read” or “write barriers”
Efficient
Space & time
C: effectively portable assembly code
3
![Page 4: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/4.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
OK, Why C++?
4
![Page 5: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/5.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
OK, Why C++?
C++: extends C
Upwardly-compatible
Adds significant software engineering benefits
Classes
Encapsulation (private)
Templates (“generics”)
Other modularity advantages
Inlining instead of macros
5
![Page 6: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/6.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science 6
Outline, part I
Basics – compiling & running
Intrinsic types, conditionals, etc.
Pointers + Reference variables
Assignment
Objects
&, *, ->
Stack vs. heap
![Page 7: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/7.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Outline, part II
Functions
Parameter passing
Structs & classes
Overloading & inheritance
Stack vs. heap
I/O, command-line
STL
7
![Page 8: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/8.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Basics
Main & compilation
8
![Page 9: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/9.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Intrinsic Types
Essentially identical
9
![Page 10: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/10.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Conditionals
Mostly the same
C/C++: nonzero int same as true
10
![Page 11: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/11.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
File I/O
Simple stream-based I/O
cout << “foo” print foo
cin >> x read x from the console
11
![Page 12: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/12.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Command-line Arguments
Again, similar to Java
12
![Page 13: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/13.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Key Differences
Differences between C/C++ and Java
Assignment
Pointers
Parameter passing
Heap & Stack
Arrays
13
![Page 14: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/14.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Assignment
Java assignment: makes reference
C++ assignment: makes copy
14
![Page 15: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/15.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Pointers & Friends
Pointers are like jumps, leading wildly from one part of the data structure to another. Their introduction into high-level languages has been a step backwards from which wemay never recover.
C.A.R. Hoare
15
![Page 16: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/16.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Pointers & Friends
Concept not in Java: address manipulation
16
![Page 17: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/17.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Functions & Parameter Passing
C/C++ – all parameters copied by default
17
![Page 18: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/18.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Parameter Passing
To change input, pass pointeror call by reference
18
![Page 19: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/19.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Pass by Reference
Syntactic sugar:foo (int &i) = pass by reference
Secretly does pointer stuff for you
19
![Page 20: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/20.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Stack & Heap
20
![Page 21: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/21.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Stack & Heap
In C/C++ as in Java, objects can live on stack or on heap
Stack = region of memory for temporaries
Stack pointer pushed on function entry
Popped on function exit
Heap = distinct region of memory for persistent objects
C/C++ – explicitly managed
Pointers introduce problems!
21
![Page 22: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/22.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
The Stack
Stack data: new every time
22
![Page 23: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/23.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Big Stack Mistake
Never return pointers to the stack!
23
![Page 24: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/24.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
The Heap
Allocate persistent data on heap with new
24
![Page 25: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/25.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Explicit Memory Management
Java heap – garbage collected
C/C++ – explicit memory management
You must delete items (or memory leak)
Delete them too soon (still in use) – crash
“Dangling pointer” error
Delete something twice – crash
“Double-free” error
25
![Page 26: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/26.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Classes & Objects
No “top” object (as in Java Object)
Also: C++ has no interfaces but has multiple inheritance – stay far away
Key difference for you – not all methods dynamically-dispatched
Methods associated with declared type rather than dynamic type unless labeled virtual
26
![Page 27: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/27.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Struct Member Access
struct = class with everything public
Use these sparingly
27
![Page 28: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/28.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Class Declaration
Pretty similar
28
![Page 29: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/29.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Arrays
Numerous differences
Arrays do not have to be allocated with new
Array bounds not checked
Item[0] = pointer to start of array
Arrays just syntactic sugarfor pointer arithmetic! (scary! avoid!) v = 12; *(Item + v) = 1;
Same as Item[12] = 1;
Note: sizeof(x) = number of bytes to hold x
Multi-dimensional arrays (matrices)
just arrays of pointers to arrays29
![Page 30: Operating Systems - Intro to C++](https://reader034.fdocuments.us/reader034/viewer/2022051322/545ae3a2b1af9fc0638b5e1e/html5/thumbnails/30.jpg)
UNIVERSITY OF MASSACHUSETTS AMHERST • Department of Computer Science
Other Features
Operator overloading
New meanings to existing operators int operator+(MyType& a, MyType& b);
Controversial, but useful for things like complex math, matrix operations int& operator()(int x, int y);
Templates
A.k.a. generics in Java template <class X> void foo (X arg);
30