Computer Science Introduction to C++ Haysn Hornbeck

Size: px
Start display at page:

Download "Computer Science Introduction to C++ Haysn Hornbeck"

Transcription

1 Computer Science Introduction to C++ Haysn Hornbeck

2 Housekeeping Online Survey What do you use? How skilled are you? What are you interested in? 2

3 Housekeeping Introductory Courses Quick introductions Refresh knowledge Tease full courses? 3

4 Housekeeping Intro to Linux Advanced Linux Intro to C++ Code Repositories Pointers and Indirection Designing OO Programs Debugging in Eclipse Memory management 4

5 Housekeeping Materials (CompSci) UofC IT account (see HelpDesk) Remote Login 5

6 Housekeeping Materials (not CompSci) Mac OS X Libraries, Various odd places Bootable USB keys Virtual Machines 6

7 Housekeeping Online Survey 7

8 History C 8

9 History 1966 BCPL 9

10 BCPL GET "LIBHDR" // calculate factorials LET START() = VALOF $( FOR I = 1 TO 5 DO WRITEF("%N! = %I4*N", I, FACT(I)) RESULTIS 0 $) AND FACT(N) = N = 0 -> 1, N * FACT(N - 1) 1 0

11 BCPL 1 1 First to use code blocks Two-step compilation Procedures with parameters and return Only one data type, 16-bit word Pointers

12 History B

13 B main() { extrn fact, printn, putchar; auto i, r; } i = 1; while(i<6) { printn( fact(i), 10 ); putchar( *n ); /* *n = newline */ i =+ 1; } printn(n,b) { extrn putchar; auto a; } if(a=n/b) /* assign and check */ printn(a, b); putchar(n%b + '0'); /* exploit ASCII encoding */ fact(n) { } if(n<2) return 1; else return n * fact(n-1); 1 3

14 History Unix on the PDP-11

15 History C

16 C 1 6 #include <stdio.h> int fact( n ) { } if (n<2) return 1; else return n * fact(n-1); int main( int argc, char* argv[] ) { int i = 1; while (i<6) printf( %f\n, fact(i++) ); } return 0;

17 C 1 7 Multiple fundamental data types Structures Static types, weak enforcement Weak array support Multi-step compilation

18 Multiple Data Types 1 8 bool char short int long long long unsigned char unsigned short unsigned int unsigned long unsigned long long float double

19 Structures // by tagname struct point { }; float x; float y; struct point p; // tagname alias typedef struct point d2; // by alias typedef struct { float x; float y; } point; point p; d2 p; 1 9

20 Static Types, Weak Enforcement 2 0 // all these are valid long first = ; int second = ; char third = first + second; int fourth = ; float pi = ; int fifth = *(int*)π

21 Static Types, Weak Enforcement 2 1 // all these are valid long first = ; int second = ; char third = first + second; // 0 int fourth = ; // 4 float pi = ; int fifth = *(int*)π // ?!

22 Weak Array Support 2 2 int array[5]; for (int it = 0; it < 11; it++) array[it] = it; // this works?!

23 Weak Array Support 2 3 int array[5]; for (int it = 0; it < 11; it++) array[it] = it; // this works?! char** argv int argc 16 Arra y[0] Array[1] Array[2] Arra y[3] 32 Arra y[4 ] float pi 4 8 int fifth int fourth char third int second 64 long first

24 Multi-step Compilation Preprocessor (insert files, expand macros) Compile (convert to assembly code) Assemble (convert to machine code) Link (combine parts and libraries) 2 4

25 Preprocessor 2 5 shared.h stdio.h stdio.h shared.h main.cpp main.cpp

26 Compile 2 6 stdio.h shared.h main.asm main.cpp

27 Assembly Language main:.lfb0:.l3:.file "test.c".text.globl main.type pushq %rbp.cfi_def_cfa_offset 16.cfi_offset 6, -16 movq %rsp, %rbp.cfi_def_cfa_register 6 movl %edi, -68(%rbp) movq %rsi, -80(%rbp) movabsq $ , %rax movq %rax, -16(%rbp) movl $ , -20(%rbp) movl -20(%rbp), %eax movl %eax, %edx movq -16(%rbp), %rax addl %edx, %eax movb %al, -21(%rbp) movl $4, -28(%rbp) movss.lc0(%rip), %xmm0 movss %xmm0, -36(%rbp) leaq -36(%rbp), %rax movl (%rax), %eax movl %eax, -32(%rbp) movl $0, -4(%rbp) jmp.l2 movl cltq movl movl addl -4(%rbp), %eax -4(%rbp), %edx %edx, -64(%rbp,%rax,4) $1, -4(%rbp).L2:.LFE0:.LC0: cmpl $10, -4(%rbp) jle.l3 movl $0, %eax popq %rbp.cfi_def_cfa 7, 8 ret.cfi_endproc.size main,.-main.section.rodata.align 4.long ident "GCC: (Debian ) ".section.note.GNU-stack,"",@progbits 2 7

28 Assemble 2 8 main.asm main.bin

29 Link 2 9 stdio.bin stdio.bin main.bin main.bin processing.bin processing.bin

30 Why C Stinks Pointers 3 0 Poor organization Side effects galore Trust the coder

31 History Simula 67

32 Simula Class Rectangle (Width, Height); Real Width, Height;! Class with two parameters; Begin Real Area, Perimeter;! Attributes; Procedure Update;! Methods (Can be Virtual); Begin Area := Width * Height; Perimeter := 2*(Width + Height) End of Update; Boolean Procedure IsSquare; IsSquare := Width=Height; Update;! Life of rectangle started at creation; OutText("Rectangle created: "); OutFix(Width,2,6); OutFix(Height,2,6); OutImage End of Rectangle;

33 History C with Classes

34 History C++

35 History C++ 2.0

36 History ISO/IEC 14882:1998 (C++98)

37 History

38 Demo Code 3 8

39 Demo Code 3 9 (x x o ) 2 +( y y o ) 2 = r π = circumference diameter

40 Demo Code 4 0 x x o + y y o = r π = circumference diameter

41 Demo Code 4 1 x x o + y y o = r π = 4

42 Demo Code (x x o ) 3 +( y y o ) 3 = r π

43 Demo Code p (x x o ) p +( y y o ) p = r π =?

44 Classes, Attributes, Methods, class PiEstimator { private: CircleGenerator cg; set<point> points; public: PiEstimator( fp p ); fp getp() { return cg.getp(); } void setp( fp p ); // used to create points // the points that make up our arc // estimate Pi via a lot of "random" points fp estimateviaflood( ulong count ); // estimate Pi via distance segmentation fp estimateviasegment( fp maxdist ); }; // PiEstimator

45 Typedef 4 5 typedef unsigned long ulong; typedef double fp;

46 Inheritence, Virtual Functions 4 6 class DistanceMetric { public: static virtual fp distance( const& Point, const& Point ); }; // DistanceMetric class EuclideanDistance : public DistanceMetric { public: static fp distance( const& Point a, const& Point b ) { }; } return sqrt( (a.getx() - b.getx())*(a.getx() - b.getx()) + (a.gety() - b.gety())*(a.gety() - b.gety()) );

47 Operator Overloading 4 7 class Point { private: fp x; fp y; public: /*... */ // the coordinates of this point in 2D space // so we're organized in set bool operator<( const Point& a ) const; // to make some operations easier Point operator+( const Point& a ) const; Point operator-( const Point& a ) const; Point operator*( const fp& a ); Point power( const fp& a ); Point absolute(); }; // Point

48 Operator Overloading class Point { private: fp x; fp y; // the coordinates of this point in 2D space public: /*... */ // so we're organized in set bool operator<( const Point& a ) const; // to make some operations easier Point operator+( const Point& a ) const; Point operator-( const Point& a ) const; Point operator*( const fp& a ); Point power( const fp& a ); Point absolute(); }; // Point Point a; Point b; Point c = a b; // Point c( a.getx() - b.getx(), a.gety() - b.gety() ); 4 8

49 Inheritence, Virtual Functions 4 9 class DistanceMetric { public: static virtual fp distance( const& Point, const& Point ); }; // DistanceMetric class EuclideanDistance : public DistanceMetric { public: static fp distance( const& Point a, const& Point b ) { // return sqrt( (a.getx() - b.getx())*(a.getx() - b.getx()) + // (a.gety() - b.gety())*(a.gety() - b.gety()) ); }; } return ((a b).dot(a b)).sqrt();

50 Input / Output 5 0 #include <stdio.h> printf( For precision %f and exponent %f, Pi ~= %.15f\n, precision, exponent, pe.estimateviasegment( precision ) ); using std::cout; using std::endl; using std::setprecision; #include <iostream> std::cout << "For precision " << precision << " and exponent " << exponent << ", Pi ~= " << std::setprecision(15) << pe.estimateviasegment( precision ) << std::endl;

51 Namespaces 5 1 #include <stdio.h> printf( For precision %f and exponent %f, Pi ~= %.15f\n, precision, exponent, pe.estimateviasegment( precision ) ); #include <iostream> using std::cout; using std::endl; using std::setprecision; cout << "For precision " << precision << " and exponent " << exponent << ", Pi ~= " << setprecision(15) << pe.estimateviasegment( precision ) << endl;

52 Namespaces 5 2 #include <stdio.h> printf( For precision %f and exponent %f, Pi ~= %.15f\n, precision, exponent, pe.estimateviasegment( precision ) ); #include <iostream> using std::cout; using std::endl; using std::setprecision; cout << "For precision " << precision << " and exponent " << exponent << ", Pi ~= " << setprecision(15) << pe.estimateviasegment( precision ) << endl; // Point Point::operator+( const Point& a ) const;

53 Namespaces 5 3 // creating a namespace namespace mypackage { class myclass { public: void dosomething(); }; } // importing from a namespace using mypackage::myclass; myclass.dosomething();

54 Namespaces 5 4 Point Point::pow( const fp& s ) { // doesn t compile } return Point( pow(x,s), pow(y,s) ); Point Point::power( const fp& s ) { } return Point( pow(x,s), pow(y,s) );

55 Templates 5 5 class PairPoints { private: Point a; Point b; public: Point first() { return a; } Point second() { return b; } };

56 Templates 5 6 template <class T>; class Pair { private: T a; T b; public: T first() { return a; } T second() { return b; } }; Pair<Point> ray;

57 Template Specialization 5 7 template <class T>; class Pair { // assume it s a pass-by-value bool lessthan( const& T rhs ) { return *this < rhs; } //... template <>; class Pair<T*> { // know we have a reference bool lessthan( const& T rhs ) { return **this < *rhs; } //...

58 Standard Template Library 5 8 Vector - Dynamic array List Set Map Queue Array - Linked list - Ordered list - Key -> value - First in, First out - Fixed-size array

59 Operator Overloading 5 9 class Point { private: fp x; fp y; public: /*... */ // the coordinates of this point in 2D space // so we're organized in set bool operator<( const Point& a ) const; // to make some operations easier Point operator+( const Point& a ) const; Point operator-( const Point& a ) const; Point operator*( const fp& a ); Point power( const fp& a ); Point absolute(); }; // Point

60 Memory Management 6 0 // C Point* point = (Point*) malloc( sizeof(point) ); Point* array = (Point*) malloc( sizeof(point) * count ); free(point); free(array); // C++ Point* point = new Point(); Point* array = new Point[3]; std::vector<point> better; delete point; delete[] array; // quite important!

61 But C++ = C with some added syntactic sugar

62 C++ = C 6 2 All C operators work main function still around Ditto pointers Preprocessor and garbage dependency handling Compile-time enforcement Permissive casting

63 C++ = C years of development More libraries than any other language Interfaces to other languages Ported everywhere The Boost libraries

64 6 4 Lab Challenges

65 G++ (GNU C compiler) -o [file] -g -ggdb -O[number] -I [directory] -l [file] The file to output to. Add debugging symbols. Add debugging symbols for GDB. Optimize the code, to varying levels. [0-3] Search for header files in this directory, too. Link in this library, too

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory

Draft. Chapter 1 Program Structure. 1.1 Introduction. 1.2 The 0s and the 1s. 1.3 Bits and Bytes. 1.4 Representation of Numbers in Memory Chapter 1 Program Structure In the beginning there were 0s and 1s. GRR 1.1 Introduction In this chapter we will talk about memory: bits, bytes and how data is represented in the computer. We will also

More information

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11

Binghamton University. CS-220 Spring X86 Debug. Computer Systems Section 3.11 X86 Debug Computer Systems Section 3.11 GDB is a Source Level debugger We have learned how to debug at the C level But the machine is executing X86 object code! How does GDB play the shell game? Makes

More information

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here

CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here CSE 351 Section 4 GDB and x86-64 Assembly Hi there! Welcome back to section, we re happy that you re here x86-64 Assembly Language Assembly language is a human-readable representation of machine code instructions

More information

The Compilation Process

The Compilation Process Crash Course in C Lecture 2 Moving from Python to C: The compilation process Differences between Python and C Variable declaration and strong typing The memory model: data vs. address The Compilation Process

More information

Chapter 15 - C++ As A "Better C"

Chapter 15 - C++ As A Better C Chapter 15 - C++ As A "Better C" Outline 15.1 Introduction 15.2 C++ 15.3 A Simple Program: Adding Two Integers 15.4 C++ Standard Library 15.5 Header Files 15.6 Inline Functions 15.7 References and Reference

More information

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C

SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C Fall Term 2016 SYSTEMS PROGRAMMING AND COMPUTER ARCHITECTURE Assignment 5: Assembly and C Assigned on: 20th Oct 2016 Due by: 27th Oct 2016 Pen & Paper exercise Assembly Code Fragments Consider the following

More information

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced:

1. A student is testing an implementation of a C function; when compiled with gcc, the following x86-64 assembly code is produced: This assignment refers to concepts discussed in sections 2.1.1 2.1.3, 2.1.8, 2.2.1 2.2.6, 3.2, 3.4, and 3.7.1of csapp; see that material for discussions of x86 assembly language and its relationship to

More information

CS356: Discussion #7 Buffer Overflows. Marco Paolieri

CS356: Discussion #7 Buffer Overflows. Marco Paolieri CS356: Discussion #7 Buffer Overflows Marco Paolieri (paolieri@usc.edu) Array Bounds class Bounds { public static void main(string[] args) { int[] x = new int[10]; for (int i = 0; i

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 3 nd Edition by Bryant and O'Hallaron

More information

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2

Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance. John Edgar 2 CMPT 125 Running a C program Compilation Python and C Variables and types Data and addresses Functions Performance John Edgar 2 Edit or write your program Using a text editor like gedit Save program with

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 3 nd Edition by Bryant and O'Hallaron

More information

Areas for growth: I love feedback

Areas for growth: I love feedback Assembly part 2 1 Areas for growth: I love feedback Speed, I will go slower. Clarity. I will take time to explain everything on the slides. Feedback. I give more Kahoot questions and explain each answer.

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2 nd edition, by Kernighan and Ritchie, Absolute C++, by Walter

More information

Introduction to C++ Systems Programming

Introduction to C++ Systems Programming Introduction to C++ Systems Programming Introduction to C++ Syntax differences between C and C++ A Simple C++ Example C++ Input/Output C++ Libraries C++ Header Files Another Simple C++ Example Inline Functions

More information

C and Programming Basics

C and Programming Basics Announcements Assignment 1 Will be posted on Wednesday, Jan. 9 Due Wednesday, Jan. 16 Piazza Please sign up if you haven t already https://piazza.com/sfu.ca/spring2019/cmpt125 Lecture notes Posted just

More information

Page 1. Agenda. Programming Languages. C Compilation Process

Page 1. Agenda. Programming Languages. C Compilation Process EE 472 Embedded Systems Dr. Shwetak Patel Assistant Professor Computer Science & Engineering Electrical Engineering Agenda Announcements C programming intro + pointers Shwetak N. Patel - EE 472 2 Programming

More information

Function Calls and Stack

Function Calls and Stack Function Calls and Stack Philipp Koehn 16 April 2018 1 functions Another Example 2 C code with an undefined function int main(void) { int a = 2; int b = do_something(a); urn b; } This can be successfully

More information

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference Pass by Value More Functions Different location in memory Changes to the parameters inside the function body have no effect outside of the function. 2 Passing Parameters by Reference Example: Exchange

More information

A brief introduction to C++

A brief introduction to C++ A brief introduction to C++ Rupert Nash r.nash@epcc.ed.ac.uk 13 June 2018 1 References Bjarne Stroustrup, Programming: Principles and Practice Using C++ (2nd Ed.). Assumes very little but it s long Bjarne

More information

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy CSCI 334: Principles of Programming Languages Lecture 18: C/C++ Homework help session will be tomorrow from 7-9pm in Schow 030A instead of on Thursday. Instructor: Dan Barowy HW6 and HW7 solutions We only

More information

typedef Labeling<unsigned char,short> LabelingBS; typedef Labeling<unsigned char,short>::regioninfo RegionInfoBS;

typedef Labeling<unsigned char,short> LabelingBS; typedef Labeling<unsigned char,short>::regioninfo RegionInfoBS; 2005 7 19 1 ( ) Labeling 2 C++ STL(Standard Template Library) g++ (GCC) 3.3.2 3 3.1 Labeling SrcT DstT SrcT: unsigned char, shoft DstT: short typedef 1. unsigned char, short typedef Labeling

More information

Lab 10: Introduction to x86 Assembly

Lab 10: Introduction to x86 Assembly CS342 Computer Security Handout # 8 Prof. Lyn Turbak Wednesday, Nov. 07, 2012 Wellesley College Revised Nov. 09, 2012 Lab 10: Introduction to x86 Assembly Revisions: Nov. 9 The sos O3.s file on p. 10 was

More information

Compiler Design IIIT Kalyani, West Bengal 1. Introduction. Goutam Biswas. Lect 1

Compiler Design IIIT Kalyani, West Bengal 1. Introduction. Goutam Biswas. Lect 1 Compiler Design IIIT Kalyani, West Bengal 1 Introduction Compiler Design IIIT Kalyani, West Bengal 2 Programming a Computer High level language program Assembly language program Machine language program

More information

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time.

What is concurrency? Concurrency. What is parallelism? concurrency vs parallelism. Concurrency: (the illusion of) happening at the same time. What is concurrency? Concurrency Johan Montelius KTH 2017 Concurrency: (the illusion of) happening at the same time. A property of the programing model. Why would we want to do things concurrently? What

More information

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

Fast Introduction to Object Oriented Programming and C++

Fast Introduction to Object Oriented Programming and C++ Fast Introduction to Object Oriented Programming and C++ Daniel G. Aliaga Note: a compilation of slides from Jacques de Wet, Ohio State University, Chad Willwerth, and Daniel Aliaga. Outline Programming

More information

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco CS 326 Operating Systems C Programming Greg Benson Department of Computer Science University of San Francisco Why C? Fast (good optimizing compilers) Not too high-level (Java, Python, Lisp) Not too low-level

More information

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006

C Compilation Model. Comp-206 : Introduction to Software Systems Lecture 9. Alexandre Denault Computer Science McGill University Fall 2006 C Compilation Model Comp-206 : Introduction to Software Systems Lecture 9 Alexandre Denault Computer Science McGill University Fall 2006 Midterm Date: Thursday, October 19th, 2006 Time: from 16h00 to 17h30

More information

C Programming Review CSC 4320/6320

C Programming Review CSC 4320/6320 C Programming Review CSC 4320/6320 Overview Introduction C program Structure Keywords & C Types Input & Output Arrays Functions Pointers Structures LinkedList Dynamic Memory Allocation Macro Compile &

More information

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Roadmap. Java: Assembly language: OS: Machine code: Computer system: Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp

More information

GCC and Assembly language. GCC and Assembly language. Consider an example (dangeous) foo.s

GCC and Assembly language. GCC and Assembly language. Consider an example (dangeous) foo.s GCC and Assembly language slide 1 GCC and Assembly language slide 2 during the construction of an operating system kernel, microkernel, or embedded system it is vital to be able to access some of the microprocessor

More information

CS 376b Computer Vision

CS 376b Computer Vision CS 376b Computer Vision 09 / 25 / 2014 Instructor: Michael Eckmann Today s Topics Questions? / Comments? Enhancing images / masks Cross correlation Convolution C++ Cross-correlation Cross-correlation involves

More information

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates

Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates Sebastian Wild Technische Universität München 11.01.2010 Abstract In this work we will discuss about templates in C++, especially their

More information

Introduction Selected details Live demos. HrwCC. A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner

Introduction Selected details Live demos. HrwCC. A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner HrwCC A self-compiling C-compiler. Stefan Huber Christian Rathgeb Stefan Walkner Universität Salzburg VP Compiler Construction June 26, 2007 Overview 1 Introduction Basic properties Features 2 Selected

More information

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010 CSE 374 Programming Concepts & Tools Hal Perkins Spring 2010 Lecture 19 Introduction ti to C++ C++ C++ is an enormous language: g All of C Classes and objects (kind of like Java, some crucial differences)

More information

C++ Named Return Value Optimization

C++ Named Return Value Optimization C++ Named Return Value Optimization post. Robert.Schneider@hotmail.de meetup.com/c-user-group-karlsruhe auto create() -> T { T castor /* initialize */; //... return castor; } void use() { T pollux = create();

More information

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program? Intro to Programming & C++ Unit 1 Sections 1.1-4 and 2.1-10, 2.12-13, 2.15-17 CS 1428 Spring 2019 Jill Seaman 1.1 Why Program? Computer programmable machine designed to follow instructions Program a set

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

CSE 333 Lecture 9 - intro to C++

CSE 333 Lecture 9 - intro to C++ CSE 333 Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia & Agenda Main topic: Intro to C++ But first: Some hints on HW2 Labs: The

More information

C++ Tutorial AM 225. Dan Fortunato

C++ Tutorial AM 225. Dan Fortunato C++ Tutorial AM 225 Dan Fortunato Anatomy of a C++ program A program begins execution in the main() function, which is called automatically when the program is run. Code from external libraries can be

More information

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1.

Program Translation. text. text. binary. binary. C program (p1.c) Compiler (gcc -S) Asm code (p1.s) Assembler (gcc or as) Object code (p1. Program Translation Compilation & Linking 1 text C program (p1.c) Compiler (gcc -S) text Asm code (p1.s) binary binary Assembler (gcc or as) Object code (p1.o) Linker (gcc or ld) Executable program (p1)

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 3 nd Edition by Bryant and O'Hallaron

More information

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit. C++ Basics January 19, 2012 Brian A. Malloy Slide 1 of 24 1. Many find Deitel quintessentially readable; most find Stroustrup inscrutable and overbearing: Slide 2 of 24 1.1. Meyers Texts Two excellent

More information

Introduction to C++ (Extensions to C)

Introduction to C++ (Extensions to C) Introduction to C++ (Extensions to C) C is purely procedural, with no objects, classes or inheritance. C++ is a hybrid of C with OOP! The most significant extensions to C are: much stronger type checking.

More information

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

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington CSE 333 Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington Administrivia New exercise posted yesterday afternoon, due Monday morning - Read a directory

More information

Machine/Assembler Language Putting It All Together

Machine/Assembler Language Putting It All Together COMP 40: Machine Structure and Assembly Language Programming Fall 2015 Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Email: noah@cs.tufts.edu Web: http://www.cs.tufts.edu/~noah

More information

Concurrency. Johan Montelius KTH

Concurrency. Johan Montelius KTH Concurrency Johan Montelius KTH 2017 1 / 32 What is concurrency? 2 / 32 What is concurrency? Concurrency: (the illusion of) happening at the same time. 2 / 32 What is concurrency? Concurrency: (the illusion

More information

How Software Executes

How Software Executes How Software Executes CS-576 Systems Security Instructor: Georgios Portokalidis Overview Introduction Anatomy of a program Basic assembly Anatomy of function calls (and returns) Memory Safety Programming

More information

Linked List using a Sentinel

Linked List using a Sentinel Linked List using a Sentinel Linked List.h / Linked List.h Using a sentinel for search Created by Enoch Hwang on 2/1/10. Copyright 2010 La Sierra University. All rights reserved. / #include

More information

Lab 1: First Steps in C++ - Eclipse

Lab 1: First Steps in C++ - Eclipse Lab 1: First Steps in C++ - Eclipse Step Zero: Select workspace 1. Upon launching eclipse, we are ask to chose a workspace: 2. We select a new workspace directory (e.g., C:\Courses ): 3. We accept the

More information

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures The main body and cout Agenda 1 Fundamental data types Declarations and definitions Control structures References, pass-by-value vs pass-by-references The main body and cout 2 C++ IS AN OO EXTENSION OF

More information

1 Number Representation(10 points)

1 Number Representation(10 points) Name: Sp15 Midterm Q1 1 Number Representation(10 points) 1 NUMBER REPRESENTATION(10 POINTS) Let x=0xe and y=0x7 be integers stored on a machine with a word size of 4bits. Show your work with the following

More information

Programming at Compile Time. Rainer Grimm Training, Coaching, and Technology Consulting

Programming at Compile Time. Rainer Grimm Training, Coaching, and Technology Consulting Programming at Compile Time Rainer Grimm Training, Coaching, and Technology Consulting www.modernescpp.de Overview Constant expressions Type-traits library Template metaprogramming Template Metaprogramming

More information

CSE 303: Concepts and Tools for Software Development

CSE 303: Concepts and Tools for Software Development CSE 303: Concepts and Tools for Software Development Hal Perkins Autumn 2008 Lecture 24 Introduction to C++ CSE303 Autumn 2008, Lecture 24 1 C++ C++ is an enormous language: All of C Classes and objects

More information

Come and join us at WebLyceum

Come and join us at WebLyceum Come and join us at WebLyceum For Past Papers, Quiz, Assignments, GDBs, Video Lectures etc Go to http://www.weblyceum.com and click Register In Case of any Problem Contact Administrators Rana Muhammad

More information

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls

Princeton University Computer Science 217: Introduction to Programming Systems. Assembly Language: Function Calls Princeton University Computer Science 217: Introduction to Programming Systems Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems x86-64 solutions Pertinent

More information

Machine Program: Procedure. Zhaoguo Wang

Machine Program: Procedure. Zhaoguo Wang Machine Program: Procedure Zhaoguo Wang Requirements of procedure calls? P() { y = Q(x); y++; 1. Passing control int Q(int i) { int t, z; return z; Requirements of procedure calls? P() { y = Q(x); y++;

More information

Essentials for Scientific Computing: Source Code, Compilation and Libraries Day 8

Essentials for Scientific Computing: Source Code, Compilation and Libraries Day 8 Essentials for Scientific Computing: Source Code, Compilation and Libraries Day 8 Ershaad Ahamed TUE-CMS, JNCASR May 2012 1 Introduction In the first session we discussed instructions that the CPU processes

More information

Introduction to Programming using C++

Introduction to Programming using C++ Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know

More information

Programming at Compile Time. Rainer Grimm Training, Coaching, and Technology Consulting

Programming at Compile Time. Rainer Grimm Training, Coaching, and Technology Consulting Programming at Compile Time Rainer Grimm Training, Coaching, and Technology Consulting www.modernescpp.de Overview Constant expressions Type-traits library Template metaprogramming Template Metaprogramming

More information

CS-220 Spring 2018 Test 2 Version Practice Apr. 23, Name:

CS-220 Spring 2018 Test 2 Version Practice Apr. 23, Name: CS-220 Spring 2018 Test 2 Version Practice Apr. 23, 2018 Name: 1. (10 points) For the following, Check T if the statement is true, the F if the statement is false. (a) T F : The main difference between

More information

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 7 CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2017 Lecture 7 LAST TIME Dynamic memory allocation and the heap: A run-time facility that satisfies multiple needs: Programs can use widely varying, possibly

More information

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay C++ Basics Data Processing Course, I. Hrivnacova, IPN Orsay The First Program Comments Function main() Input and Output Namespaces Variables Fundamental Types Operators Control constructs 1 C++ Programming

More information

Template Metaprogramming in Finite Element Computations

Template Metaprogramming in Finite Element Computations Template Metaprogramming in Finite Element Computations Zuse Institute Berlin DFG Research Center Workshop on Data Structures for Finite Element and Finite Volume Computations, 2008 02 29, Berlin Contents

More information

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

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++ CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 19 Introduction to C++ C++ C++ is an enormous language: All of C Classes and objects (kind of like Java, some crucial differences) Many

More information

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor

CS 261 Fall Machine and Assembly Code. Data Movement and Arithmetic. Mike Lam, Professor CS 261 Fall 2018 0000000100000f50 55 48 89 e5 48 83 ec 10 48 8d 3d 3b 00 00 00 c7 0000000100000f60 45 fc 00 00 00 00 b0 00 e8 0d 00 00 00 31 c9 89 0000000100000f70 45 f8 89 c8 48 83 c4 10 5d c3 Mike Lam,

More information

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING Dr. Shady Yehia Elmashad Outline 1. Introduction to C++ Programming 2. Comment 3. Variables and Constants 4. Basic C++ Data Types 5. Simple Program: Printing

More information

Layers of Abstraction CS 3330: C. Compilation Steps. What s in those files? Higher-level language: C. Assembly: X86-64.

Layers of Abstraction CS 3330: C. Compilation Steps. What s in those files? Higher-level language: C. Assembly: X86-64. Layers of Abstraction CS 3330: C 25 August 2016 x += y add %rbx, %rax 60 03 Higher-level language: C Assembly: X86-64 Machine code: Y86 (we ll talk later) Logic and Registers 1 2 Compilation Steps compile:

More information

University of Washington

University of Washington Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq %rbp movq %rsp, %rbp... popq %rbp

More information

CE221 Programming in C++ Part 1 Introduction

CE221 Programming in C++ Part 1 Introduction CE221 Programming in C++ Part 1 Introduction 06/10/2017 CE221 Part 1 1 Module Schedule There are two lectures (Monday 13.00-13.50 and Tuesday 11.00-11.50) each week in the autumn term, and a 2-hour lab

More information

The University of Nottingham

The University of Nottingham The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 2 MODULE, AUTUMN SEMESTER 2008 2009 C/C++ for Java Programmers Time allowed TWO hours Candidates may complete the front cover of their answer

More information

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2017 Lecture 12 CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2017 Lecture 12 CS24 MIDTERM Midterm format: 6 hour overall time limit, multiple sittings (If you are focused on midterm, clock should be running.) Open book

More information

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah Lecturer Department of Computer Science & IT University of Balochistan 1 Outline p Introduction p Program development p C language and beginning with

More information

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and #include The Use of printf() and scanf() The Use of printf()

More information

Machine Programming 3: Procedures

Machine Programming 3: Procedures Machine Programming 3: Procedures CS61, Lecture 5 Prof. Stephen Chong September 15, 2011 Announcements Assignment 2 (Binary bomb) due next week If you haven t yet please create a VM to make sure the infrastructure

More information

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5 APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5 APPENDIX E : ASCII CHARACTER SET... 6 APPENDIX F : USING THE GCC COMPILER

More information

CS3157: Advanced Programming. Outline

CS3157: Advanced Programming. Outline CS3157: Advanced Programming Lecture #12 Apr 3 Shlomo Hershkop shlomo@cs.columbia.edu 1 Outline Intro CPP Boring stuff: Language basics: identifiers, data types, operators, type conversions, branching

More information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information Laboratory 2: Programming Basics and Variables Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information 3. Comment: a. name your program with extension.c b. use o option to specify

More information

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver) Introduction to C++ 1. General C++ is an Object oriented extension of C which was derived from B (BCPL) Developed by Bjarne Stroustrup (AT&T Bell Labs) in early 1980 s 2. A Simple C++ Program A C++ program

More information

Changelog. Assembly (part 1) logistics note: lab due times. last time: C hodgepodge

Changelog. Assembly (part 1) logistics note: lab due times. last time: C hodgepodge Changelog Assembly (part 1) Corrections made in this version not seen in first lecture: 31 August 2017: slide 34: split out from previous slide; clarify zero/positive/negative 31 August 2017: slide 26:

More information

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes Distributed Real-Time Control Systems Lecture 17 C++ Programming Intro to C++ Objects and Classes 1 Bibliography Classical References Covers C++ 11 2 What is C++? A computer language with object oriented

More information

Corrections made in this version not seen in first lecture:

Corrections made in this version not seen in first lecture: Assembly (part 1) 1 Changelog 1 Corrections made in this version not seen in first lecture: 31 August 2017: slide 34: split out from previous slide; clarify zero/positive/negative 31 August 2017: slide

More information

A Tour of the C++ Programming Language

A Tour of the C++ Programming Language A Tour of the C++ Programming Language We already know C Everything that can be done with a computer, can be done in C Why should we learn another language? Newer languages provide a bigger toolbox Some

More information

Floating point - summary 1

Floating point - summary 1 Floating point - summary 1 More in section Numbers are represented as [Mantissa]*(2**[Exponent]) IEEE 754 Mantissa is normalized sign/magnitude; normalization means the number always has a leading 1 (e.g.

More information

Buffer Overflow. An Introduction

Buffer Overflow. An Introduction Buffer Overflow An Introduction Workshop Flow-1 Revision (4-10) How a program runs Registers Memory Layout of a Process Layout of a StackFrame Layout of stack frame using GDB and looking at Assembly code

More information

Layers of Abstraction. CS 3330: More C. printf. Last time

Layers of Abstraction. CS 3330: More C. printf. Last time Layers of Abstraction CS 3330: More C 30 August 2016 x += y add %rbx, %rax 60 03 Higher-level language: C Assembly: X86-64 Machine code: Y86 (we ll talk later) Logic and Registers 1 2 Last time printf

More information

Credits and Disclaimers

Credits and Disclaimers Credits and Disclaimers 1 The examples and discussion in the following slides have been adapted from a variety of sources, including: Chapter 3 of Computer Systems 3 nd Edition by Bryant and O'Hallaron

More information

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted one-page fact sheet. Your fact sheet may contain definitions and examples,

More information

CS356: Discussion #8 Buffer-Overflow Attacks. Marco Paolieri

CS356: Discussion #8 Buffer-Overflow Attacks. Marco Paolieri CS356: Discussion #8 Buffer-Overflow Attacks Marco Paolieri (paolieri@usc.edu) Previous Example #include void unreachable() { printf("impossible.\n"); void hello() { char buffer[6]; scanf("%s",

More information

Memory and C/C++ modules

Memory and C/C++ modules Memory and C/C++ modules From Reading #6 Will return to OOP topics (templates and library tools) soon Compilation/linking revisited source file 1 object file 1 source file 2 compilation object file 2 library

More information

GCC : From 2.95 to 3.2

GCC : From 2.95 to 3.2 GCC : From 2.95 to 3.2 Topics Simple changes name of standard include files, std::endl, iostream, throw statements, vector iterators More complicated changes string streams, parameterized macros, hash_map

More information

CS429: Computer Organization and Architecture

CS429: Computer Organization and Architecture CS429: Computer Organization and Architecture Dr. Bill Young Department of Computer Sciences University of Texas at Austin Last updated: October 31, 2017 at 09:37 CS429 Slideset 10: 1 Basic Data Types

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Winter 2011 Lecture 2 - Language Basics Milena Scaccia School of Computer Science McGill University January 11, 2011 Course Web Tools Announcements, Lecture Notes, Assignments

More information

A Tour of the C++ Programming Language

A Tour of the C++ Programming Language A Tour of the C++ Programming Language We already know C Everything that can be done with a computer, can be done in C Why should we learn another language? Newer languages provide a bigger toolbox Some

More information

PRINCIPLES OF OPERATING SYSTEMS

PRINCIPLES OF OPERATING SYSTEMS PRINCIPLES OF OPERATING SYSTEMS Tutorial-1&2: C Review CPSC 457, Spring 2015 May 20-21, 2015 Department of Computer Science, University of Calgary Connecting to your VM Open a terminal (in your linux machine)

More information

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is

More information

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017

PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017 PROGRAMMING IN C++ KAUSIK DATTA 18-Oct-2017 Objectives Recap C Differences between C and C++ IO Variable Declaration Standard Library Introduction of C++ Feature : Class Programming in C++ 2 Recap C Built

More information