Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Similar documents
Short Notes of CS201

CS201 - Introduction to Programming Glossary By

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

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

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CS201 Latest Solved MCQs

CMSC 330: Organization of Programming Languages

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Lecture 2, September 4

A brief introduction to C programming for Java programmers

CA341 - Comparative Programming Languages

Heap, Variables, References, and Garbage. CS152. Chris Pollett. Oct. 13, 2008.

Stream Computing using Brook+

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Contents. 8-1 Copyright (c) N. Afshartous

Multiple Choice Questions. Chapter 5

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

COMP 2355 Introduction to Systems Programming

Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language

SPIM Procedure Calls

CMSC 330: Organization of Programming Languages. Memory Management and Garbage Collection

Lectures 5-6: Introduction to C

Introduction to C++ Introduction to C++ 1

Reliable C++ development - session 1: From C to C++ (and some C++ features)

CMSC 330: Organization of Programming Languages

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

CS3157: Advanced Programming. Outline

VALLIAMMAI ENGINEERING COLLEGE

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

Introduction to Programming Using Java (98-388)

CS250 Final Review Questions

ENERGY 211 / CME 211. Functions

StackVsHeap SPL/2010 SPL/20

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Pointers and References

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Java Internals. Frank Yellin Tim Lindholm JavaSoft

Tutorial 1: Introduction to C Computer Architecture and Systems Programming ( )

377 Student Guide to C++

Computer Systems Lecture 9

D Programming Language

CS24 Week 3 Lecture 1

C11: Garbage Collection and Constructors

CSE 307: Principles of Programming Languages

Starting Savitch Chapter 10. A class is a data type whose variables are objects. Some pre-defined classes in C++ include int,

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Page 1. Stuff. Last Time. Today. Safety-Critical Systems MISRA-C. Terminology. Interrupts Inline assembly Intrinsics

CS 376b Computer Vision

Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

CS558 Programming Languages

Interview Questions of C++

Object-Oriented Programming for Scientific Computing

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

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

CA31-1K DIS. Pointers. TA: You Lu

CS558 Programming Languages

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Advanced Programming & C++ Language

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

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Lecture Notes on Memory Management

CS 11 C track: lecture 5

September 10,

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

COMP6771 Advanced C++ Programming

Assumptions. History

C10: Garbage Collection and Constructors

Names, Scope, and Bindings

G Programming Languages - Fall 2012

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CPSC 213. Introduction to Computer Systems. Instance Variables and Dynamic Allocation. Unit 1c

Lecture 15: Even more pointer stuff Virtual function table

Introducing C++ to Java Programmers

CS558 Programming Languages

Pointers II. Class 31

OBJECT ORIENTED PROGRAMMING USING C++

Lecture Notes on Memory Management

CSE 303: Concepts and Tools for Software Development

Question No: 1 ( Marks: 1 ) - Please choose one One difference LISP and PROLOG is. AI Puzzle Game All f the given

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

CS143 Final Fall 2009

CS558 Programming Languages

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Processes. Johan Montelius KTH

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

III. Classes (Chap. 3)

GEA 2017, Week 4. February 21, 2017

Goal of lecture. Object-oriented Programming. Context of discussion. Message of lecture

From Java to C++ From Java to C++ CSE250 Lecture Notes Weeks 1 2, part of 3. Kenneth W. Regan University at Buffalo (SUNY) September 10, 2009

Inheritance, Polymorphism and the Object Memory Model

A process. the stack

CS105 C++ Lecture 7. More on Classes, Inheritance

C++ Programming Lecture 4 Software Engineering Group

Operator overloading

Separate compilation. Topic 6: Runtime Environments p.1/21. CS 526 Topic 6: Runtime Environments The linkage convention

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

Procedure and Object- Oriented Abstraction

Transcription:

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy 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 Department of Computer Science 22

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 Department of Computer Science 33 Outline, part I Basics compiling & running Intrinsic types, conditionals, etc. Pointers + Reference variables Assignment Objects &, *, -> Stack vs. heap Department of Computer Science 44

Outline, part II Functions Parameter passing Structs & classes Overloading & inheritance Stack vs. heap I/O, command-line STL Department of Computer Science 55 Basics Main & compilation Department of Computer Science 66

Intrinsic Types Essentially identical Department of Computer Science 77 Conditionals Mostly the same C/C++: nonzero int same as true Department of Computer Science 88

File I/O Simple stream-based I/O cout << foo print foo cin >> x read x from the console Department of Computer Science 99 Command-line Arguments Again, similar to Java Department of Computer Science 10 10

Key Differences Differences between C/C++ and Java Assignment Pointers Parameter passing Heap & Stack Arrays Department of Computer Science 11 11 Assignment Java assignment: makes reference C++ assignment: makes copy Department of Computer Science 12 12

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 we may never recover. C.A.R. Hoare Department of Computer Science 13 13 Pointers & Friends Concept not in Java: address manipulation Department of Computer Science 14 14

Functions & Parameter Passing C/C++ all parameters copied by default Department of Computer Science 15 15 Parameter Passing To change input, pass pointer or call by reference Department of Computer Science 16 16

Pass by Reference Syntactic sugar: foo (int &i) = pass by reference Secretly does pointer stuff for you Department of Computer Science 17 17 Stack & Heap In C/C++ as in Java, objects can live on: 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! Department of Computer Science 18 18

The Stack Stack data: new every time Department of Computer Science 19 19 Big Stack Mistake Never return pointers to the stack! Department of Computer Science 20 20

The Heap Allocate persistent data on heap with new Department of Computer Science 21 21 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 Department of Computer Science 22 22

Classes & Objects No top object (as in Java Object) Also: C++ has no interfaces but has multiple inheritance stay far away Department of Computer Science 23 23 Struct Member Access struct = class with everything public Use these sparingly Department of Computer Science 24 24

Class Declaration Pretty similar Department of Computer Science 25 25 Arrays Numerous differences Arrays do not have to be allocated with new Array bounds not checked Item = pointer to start of array Arrays just syntactic sugar for 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 arrays Department of Computer Science 26 26

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); Department of Computer Science 27 27 Standard Template Library(STL) Implements useful data structures Department of Computer Science 28

End of Lecture Department of Computer Science 29 29 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 Department of Computer Science 30 30