Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates
|
|
- Dana Cobb
- 5 years ago
- Views:
Transcription
1 Semantics of C++ Hauptseminar im Wintersemester 2009/10 Templates Sebastian Wild Technische Universität München Abstract In this work we will discuss about templates in C++, especially their usage and the handling by the compiler. 1 Introduction Templates are a powerful tool, that gives us the possibility to create algorithms and data structures which are independent of a special data type. The Standard Template Library (STL) and also the boost library are built up using templates. Furthermore, templates gives us a Turing complete language feature which we can use to program whole algorithms. Those will be executed during the compilation process and therefore do not require any runtime, because the solution is already present. In this work, we will give a general overview about how to use templates in the first section. In the second part we will see how the compiler handles templates and in the last part we will give an introduction what will change in the new C++ standard. 2 Templates 2.1 Function Templates With Function templates we can create functions which can perform an operation with more than one type. That gives us the advantage that we do not have to write code multiple times. Each time we use the function, we just tell the function which data type we use. A function template is defined by placing the keyword template followed by <typename wildcard > in front of the function definition. The wildcard has to be placed at every position where the type should be variable, that means if the 1
2 return type is variable it has to be placed as the return type and for the types of the parameters as well as the types of the variables inside of the function. 1 template <typename T> 2 T f u n c t i o n (T var1, T var2 ) {... } It is also possible to use several variable data types in one template function. 1 template <typename T, typename U, typename V> 2 T f u n c t i o n (T var1, U var2, V var ) {... } Instead of using the keyword typename, one can also use class. Some programmers use typename if they want to clarify that this function just works with basic fundamental data types and class if it works with classes also. 1 template <class T> 2 T f u n c t i o n (T var1, T var2 ) {... } Using those function templates is pretty easy, since the compiler chooses the type to use by looking at the parameter which we assign to the function. Let us see how this works at an example function which will just add up two numbers and returns the solution. You should be aware of the fact, that the type of the parameter you will assign to the function must have a summation operator. 1 #include <iostream > 2 using namespace std ; 4 template <class T> 5 T add (T a, T b ) { 6 return a + b ; 7 } 8 9 int main ( int argc, char argv ) { 10 cout << add ( 5, 6) << endl ; // r e t u r n s cout << add (. 5, 2. 6 ) << endl ; // r e t u r n s return 0 ; 14 } It is also possible to write a function which works for several types without templates, but as we will see in this example it is not that elegant. This is an implementation of the shell sort algorithm which is introduced in The art of programming. Volume : Searching and Sorting by Knuth. [] 1 typedef int ( CFT) ( const void, const void ) ; 2 void s s o r t ( void base, s i z e t n, s i z e t sz, CFT cmp) 2
3 4 { 5 for ( int gap = n / 2 ; 0 < gap ; gap /= 2 ) 6 for ( int i = gap ; i < n ; i++ ) 7 for ( int j = i gap ; 0 <= j ; j = gap ) { 8 char b = static cast <char >( base ) ; 9 char pj = b + j s z ; 10 char pjg = b + ( j + gap ) s z ; 11 i f ( cmp( pjg, pj ) < 0 ) { 12 for ( int k = 0 ; k < s z ; k++ ) { 1 char temp = pj [ k ] ; 14 pj [ k ] = pjg [ k ] ; 15 pjg [ k ] = temp ; 16 } 17 } 18 } 19 } 1 template<class T> void s o r t ( vector <T>& v ) 2 { const s i z e t n = v. s i z e ( ) ; 4 for ( int gap = n / 2 ; 0 < gap ; gap /= 2 ) 5 for ( int i = gap ; i < n ; i++ ) 6 for ( int j = i gap ; 0 <= j ; j = gap ) { 7 i f ( v [ j + gap ] < v [ j ] ) { 8 T temp = v [ j ] ; 9 v [ j ] = v [ j + gap ] ; 10 v [ j + gap ] = temp ; 11 } 12 } 1 } 2.2 Class Templates To create a data structure with attributes and methods, we usually use a class in C++. For example, if we want to have a container such as a linked list we could only store items with a specific data type in it if we would not have templates. The whole Standard Template Library is build up on the concept of templates as the name implies. The definition of a template class is basically the same as with the template functions, we just exchange the function definition with the class definition. 1 template <class T> 2 class ClassTemplate {... 4 } ; As usual we can create an instance of a class on the stack or on the heap we just have to tell the compiler which type we want to use in the class. Let us look at both types of creating an instance of a template class at an example.
4 1 template <typename T> 2 class Add { public : 4 T sum(t a, T b ) ; 5 } ; 6 7 template <typename T> 8 Add<T>::sum(T a, T b ) { 9 return a + b ; 10 } int main ( int argc, char argv ) { 1 Add<int> s1 ; 14 cout << s1. sum ( 5, 6) ; Add<double> s2 ; 17 cout << s2. sum (. 5, 2. 6 ) ; Add<int> s = new Add<int >() ; 20 cout << s1 >sum ( 5, 6) << endl ; Add<double> s4 = new Add<double>; 2 cout << s4 >sum (. 5, 2. 6 ) << endl ; return 0 ; 26 } 2. Specialization Let us look at the following example [2]: 1 template <typename T> 2 class CAdd { public : 4 CAdd(T array [ ], unsigned n ) : m sum ( 0 ) { 5 for ( unsigned i = 0 ; i < n ; i++ ) 6 m sum += array [ i ] ; 7 } 8 9 void p r i n t ( char t e x t ) { cout << t e x t << m sum << endl ; } 10 private : 11 T m sum ; 12 } ; 1 14 int main ( int argc, char argv ) { 15 int ganz [ 5 ] = {10, 20, 0, 40, 5 0 } ; 16 double gebr [ 4 ] = {. 1 4, , 7. 0, 2. 0 } ; 17 char s t r [ 4 ] = { Ich, bin, nun, e i n S t r i n g } ; CAdd<int> isum ( ganz, 5) ; 20 CAdd<double> dsum( gebr, 4) ; 21 CAdd<char > ssum ( s t r, 4) ; // Error 22 } 4
5 We can use this template with different types, but why can we not use this one with char*? The answer is at line 6 of the example above. The char* does not have a + operator. To solve this problem there are two different possibilities. One would be to write a new class called CAddString or we use template specialization. That means, we have to write the class again, but still as a template with the specific type and for that matter, we do not have to change the way we create an instance of the class. This is also possible with function templates. It works the same way as class templates do as you can see in the following example []: 1 template <class T> 2 bool l e s s (T a, T b ) { return a < b ; } 4 template <> 5 bool l e s s <const char > ( const char a, const char b ) { return strcmp ( a, b ) < 0 ; } 1 template <> 2 class CAdd<char > { public : 4 CAdd<char >(char array [ ], unsigned n ) { 5 m sum = new char [ ] ; 6 m sum = 0 ; 7 for ( unsigned i = 0 ; i < n ; i++ ) 8 s t r c a t (m sum, array [ i ] ) ; 9 } void p r i n t ( char t e x t ) { cout << t e x t << m sum << endl ; } 12 private : 1 char m sum ; 14 } ; As we can see in this example for the specialization, there are a few differences and also some things we have to keep in mind. Write template <> in front of the class Every time we use the class name we have to write the specialized type after it, e.g. Class declaration: class CAdd < char* >... Constructor: CAdd < char* > (... )... Desctructor: CAdd < char* > ()... Replace all types of the parameters through the special type A specialized template has to be defined after the general class template has been defined 5
6 There is also another type of specialization, the partial specialization. The difference here is, that we do not specialize the class or function for a concrete type. Instead we specialize the template for a certain class of data types, e.g. we specialize it for every pointer type. 1 class CAdd<T > { 2 public : CAdd(T array [ ], unsigned n ) : m sum ( 0 ) { 4 for ( unsigned i = 0 ; i < n ; i++ ) 5 m sum += array [ i ] ; 6 } 7 8 void p r i n t ( char t e x t ) { cout << t e x t << m sum << endl ; } 9 private : 10 T m sum ; 11 } ; In the end, we have first of all the general template which we can then specialize for a certain group of types (e.g. pointers) or we specialize it for a certain data type (char*, void*,...). That gives as a wide range of possibilities to work with templates. Compiler output In this section we want to look at the templates from another point of view, the compilers. To do this, we want to look at the assembly code of the examples which perform the same operation. We write the class with int and double as types and then we also have the template class, which we will instantiate with int as type. First of all the three examples in C++: 1 #include <iostream > 2 using namespace std ; 4 template <typename T> 5 class Add 6 { 7 public : 8 T sum(t a, T b ) 9 { 10 return a + b ; 11 } 12 } ; 1 14 int main ( int argc, char argv ) 15 { 16 Add<int> o1 ; cout << o1. sum ( 5, 6) << endl ; Add<int> o2 = new Add<int >() ; 6
7 21 22 cout << o2 >sum ( 5, 6) << endl ; 2 24 delete o2 ; return 0 ; 27 } 1 #include <iostream > 2 using namespace std ; 4 class Add 5 { 6 public : 7 double sum( double a, double b ) 8 { 9 return a + b ; 10 } 11 } ; 12 1 int main ( int argc, char argv ) 14 { 15 Add o1 ; cout << o1. sum ( 5. 4, 6. ) << endl ; Add o2 = new Add ( ) ; cout << o2 >sum ( 5. 2, 6. 7 ) << endl ; 22 2 delete o2 ; return 0 ; 26 } Now we want to analyze some parts of the assembly, which is produced by the compiler. As we will see in the first two examples, there is no difference between a class written for a specific type or a template class, which is then instantiated with the same type. For this we look at the assembly code of the sum function: 1?sum@Add@@QAEHHH@Z PROC ; Add : : sum, COMDAT 2 ; t h i s \$ = ecx 4 ; 8 : { 5 6 push ebp 7 mov ebp, esp 8 sub esp, 204 ; cch 9 push ebx 10 push e s i 11 push e d i 12 push ecx 1 l e a edi, DWORD PTR [ ebp 204] 7
8 14 mov ecx, 51 ; H 15 mov eax, ; cccccccch 16 rep s t o s d 17 pop ecx 18 mov DWORD PTR t h i s \$ [ ebp ], ecx ; 9 : return a + b ; mov eax, DWORD PTR a \$ [ ebp ] 2 add eax, DWORD PTR b \$ [ ebp ] ; 10 : } pop e d i 28 pop e s i 29 pop ebx 0 mov esp, ebp 1 pop ebp 2 r e t 8?sum@Add@@QAEHHH@Z ENDP ; Add : : sum 1?sum@?\$Add@H@@QAEHHH@Z PROC ; Add<int >:: sum, COMDAT 2 ; t h i s \$ = ecx 4 ; 9 : { 5 6 push ebp 7 mov ebp, esp 8 sub esp, 204 ; cch 9 push ebx 10 push e s i 11 push e d i 12 push ecx 1 l e a edi, DWORD PTR [ ebp 204] 14 mov ecx, 51 ; H 15 mov eax, ; cccccccch 16 rep s t o s d 17 pop ecx 18 mov DWORD PTR t h i s \$ [ ebp ], ecx ; 10 : return a + b ; mov eax, DWORD PTR a \$ [ ebp ] 2 add eax, DWORD PTR b \$ [ ebp ] ; 11 : } pop e d i 28 pop e s i 29 pop ebx 0 mov esp, ebp 1 pop ebp 2 r e t 8?sum@?\$Add@H@@QAEHHH@Z ENDP ; Add<int >:: sum 8
9 If we compare those, there is no difference in the basic code. That is how the compiler handles templates. For every type we use the template with it exactly writes the code down for the special type, which we can see in these two examples for the int class and the template instantiated with the type int. If we compare it to the compiler output for the special type double, it is different in the way, that we would have for the double class. PROC ; Add<double >:: sum, COMDAT 2 ; t h i s \$ = ecx 4 ; 9 : { 5 6 push ebp 7 mov ebp, esp 8 sub esp, 204 ; cch 9 push ebx 10 push e s i 11 push e d i 12 push ecx 1 l e a edi, DWORD PTR [ ebp 204] 14 mov ecx, 51 ; H 15 mov eax, ; cccccccch 16 rep s t o s d 17 pop ecx 18 mov DWORD PTR t h i s \$ [ ebp ], ecx ; 10 : return a + b ; f l d QWORD PTR a \$ [ ebp ] ; F l o a t i n g Point I n s t r u c t i o n s 2 fadd QWORD PTR b \$ [ ebp ] ; 11 : } pop e d i 28 pop e s i 29 pop ebx 0 mov esp, ebp 1 pop ebp 2 r e t 16 ; H?sum@?\$Add@N@@QAENNN@Z ENDP ; Add<double >:: sum If we compare the rest of the assembly we will see that, besides some differences in the names of procedures, there are no differences at all. That means for the compiler that he produces the same code and, in the case of the template, just replaces the variable data type with the concrete one. 4 Conclusion Templates are a powerful tool that gives us a wide range of possibilities to implement data type independent data structures and algorithms. But there are also some restrictions, e.g. that we can not implement them in precompiled 9
10 libraries such as.dll,.lib,.a,.so. The most changes in the new C++ standard are that parts of the boost library will be included in the standard library and those data structures and algorithms are implemented with templates. There is just one additional feature to mention and those are variadic templates. Those give us the possibility to write templates where we can decide how many template parameters we use at the time we create an instance of a class. That can be between zero an many parameters. We can observe this in the following example [1]: 1 template<class... Types> struct Tuple { } ; 2 Tuple<> t0 ; // Types c o n t a i n s no arguments 4 Tuple<int> t1 ; // Types c o n t a i n s one argument : i n t 5 Tuple<int, float > t2 ; // Types c o n t a i n s two arguments : i n t and f l o a t This feature gives us new possibilities like writing a printf like function with templates instead of a multiple parameter list of a function. In the end we should always think about using templates when we design new data structures or algorithms since it can keep us from writing the same code over and over again and with the use of templates we can also calculate some procedures during the compilation process. References [1] Peter Becker. Working draft, standard for programming language c++. Technical report, ISO/IEC, [2] Helmut Herold, Michael Klar, and Susanne Klar. C++, UML und Design Patterns. Addison-Wesley, [] Bjarne Stroustrup. Die C++ Programmiersprache. Addison-Wesley,
EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution
1. (40 points) Write the following subroutine in x86 assembly: Recall that: int f(int v1, int v2, int v3) { int x = v1 + v2; urn (x + v3) * (x v3); Subroutine arguments are passed on the stack, and can
More informationEECE.3170: Microprocessor Systems Design I Summer 2017
EECE.3170: Microprocessor Systems Design I Summer 2017 Lecture 8: Key Questions June 5, 2017 1. (Review) Describe the structure of a typical x86 stack frame. EECE.3170: Microprocessor Systems Design I
More informationAgenda. 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 informationStack -- Memory which holds register contents. Will keep the EIP of the next address after the call
Call without Parameter Value Transfer What are involved? ESP Stack Pointer Register Grows by 4 for EIP (return address) storage Stack -- Memory which holds register contents Will keep the EIP of the next
More informationCS 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 informationOverview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading
How C++ Works 1 Overview Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading Motivation There are lot of myths about C++
More informationCE221 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 informationOverview. Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading
HOW C++ WORKS Overview Constructors and destructors Virtual functions Single inheritance Multiple inheritance RTTI Templates Exceptions Operator Overloading Motivation There are lot of myths about C++
More informationCS
CS 1666 www.cs.pitt.edu/~nlf4/cs1666/ Programming in C++ First, some praise for C++ "It certainly has its good points. But by and large I think it s a bad language. It does a lot of things half well and
More informationLab 8 The Stack (LIFO) Structure
Lab 8 The Stack (LIFO) Structure Objective: The stack segment in memory is where the 80x86 maintains the stack. The stack stores important information about program including local variables, subroutine
More informationLinked 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 informationAS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions
CS220 Logic Design Outline Calling Conventions Multi-module Programs 1 Calling and Returning We have already seen how the call instruction is used to execute a subprogram. call pushes the address of the
More informationIslamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack
Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB Lab # 7 Procedures and the Stack April, 2014 1 Assembly Language LAB Runtime Stack and Stack
More informationProgram Exploitation Intro
Program Exploitation Intro x86 Assembly 04//2018 Security 1 Univeristà Ca Foscari, Venezia What is Program Exploitation "Making a program do something unexpected and not planned" The right bugs can be
More informationcout << "How many numbers would you like to type? "; cin >> memsize; p = new int[memsize];
1 C++ Dynamic Allocation Memory needs were determined before program execution by defining the variables needed. Sometime memory needs of a program can only be determined during runtime, or the memory
More information377 Student Guide to C++
377 Student Guide to C++ c Mark Corner January 21, 2004 1 Introduction In this course you will be using the C++ language to complete several programming assignments. Up to this point we have only provided
More informationChapter 16. Templates. Copyright 2010 Pearson Addison-Wesley. All rights reserved
Chapter 16 Templates Copyright 2010 Pearson Addison-Wesley. All rights reserved Learning Objectives Function Templates Syntax, defining Compiler complications Class Templates Syntax Example: array template
More informationCS 251 INTERMEDIATE SOFTWARE DESIGN SPRING C ++ Basics Review part 2 Auto pointer, templates, STL algorithms
CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms AUTO POINTER (AUTO_PTR) //Example showing a bad situation with naked pointers void MyFunction()
More informationCPSC 427: Object-Oriented Programming
CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 22 November 28, 2016 CPSC 427, Lecture 22 1/43 Exceptions (continued) Code Reuse Linear Containers Ordered Containers Multiple Inheritance
More informationHomework 5. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine
Homework 5 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 13, 2013 Question 1 Consider the following Java definition of a mutable string class. class MutableString private char[] chars
More information1/29/2011 AUTO POINTER (AUTO_PTR) INTERMEDIATE SOFTWARE DESIGN SPRING delete ptr might not happen memory leak!
//Example showing a bad situation with naked pointers CS 251 INTERMEDIATE SOFTWARE DESIGN SPRING 2011 C ++ Basics Review part 2 Auto pointer, templates, STL algorithms void MyFunction() MyClass* ptr( new
More informationComputer Systems Lecture 9
Computer Systems Lecture 9 CPU Registers in x86 CPU status flags EFLAG: The Flag register holds the CPU status flags The status flags are separate bits in EFLAG where information on important conditions
More informationPointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation The new operator The delete operator Dynamic Memory Allocation for Arrays 1 C++ Data Types simple
More informationIslamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures
Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB Lab # 10 Advanced Procedures May, 2014 1 Assembly Language LAB Stack Parameters There are
More informationFinal CSE 131B Spring 2004
Login name Signature Name Student ID Final CSE 131B Spring 2004 Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 (25 points) (24 points) (32 points) (24 points) (28 points) (26 points) (22 points)
More informationCS2141 Software Development using C/C++ C++ Basics
CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short
More informationProcesses (Intro) Yannis Smaragdakis, U. Athens
Processes (Intro) Yannis Smaragdakis, U. Athens Process: CPU Virtualization Process = Program, instantiated has memory, code, current state What kind of memory do we have? registers + address space Let's
More informationOverview of Compiler. A. Introduction
CMPSC 470 Lecture 01 Topics: Overview of compiler Compiling process Structure of compiler Programming language basics Overview of Compiler A. Introduction What is compiler? What is interpreter? A very
More informationWhat will happen if we try to compile, link and run this program? Do you have any comments to the code?
1 # include < iostream > 2 3 int main () 4 { 5 std :: cout
More informationMidterm Review. PIC 10B Spring 2018
Midterm Review PIC 10B Spring 2018 Q1 What is size t and when should it be used? A1 size t is an unsigned integer type used for indexing containers and holding the size of a container. It is guarenteed
More informationCpt S 122 Data Structures. Templates
Cpt S 122 Data Structures Templates Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Topics Introduction Function Template Function-template and function-template
More informationAssembler Programming. Lecture 10
Assembler Programming Lecture 10 Lecture 10 Mixed language programming. C and Basic to MASM Interface. Mixed language programming Combine Basic, C, Pascal with assembler. Call MASM routines from HLL program.
More informationObject-Oriented Programming, Iouliia Skliarova
Object-Oriented Programming, Iouliia Skliarova A reference (&) is like a constant pointer that is automatically dereferenced. There are certain rules when using references: 1. A reference must be initialized
More informationC++ Programming Lecture 7 Software Engineering Group
C++ Programming Lecture 7 Software Engineering Group Philipp D. Schubert Contents 1. Template metaprogramming 2. Variadic template arguments 3. Smart pointer Template metaprogramming Template metaprogramming
More informationPartha Sarathi Mandal
MA 253: Data Structures Lab with OOP Tutorial 1 http://www.iitg.ernet.in/psm/indexing_ma253/y13/index.html Partha Sarathi Mandal psm@iitg.ernet.in Dept. of Mathematics, IIT Guwahati Reference Books Cormen,
More informationFunction Call Convention
Function Call Convention Compass Security Schweiz AG Werkstrasse 20 Postfach 2038 CH-8645 Jona Tel +41 55 214 41 60 Fax +41 55 214 41 61 team@csnc.ch www.csnc.ch Content Intel Architecture Memory Layout
More informationAnnouncements. 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 informationG52CPP C++ Programming Lecture 18
G52CPP C++ Programming Lecture 18 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Welcome Back 2 Last lecture Operator Overloading Strings and streams 3 Operator overloading - what to know
More informationFinancial computing with C++
Financial Computing with C++, Lecture 6 - p1/24 Financial computing with C++ LG Gyurkó University of Oxford Michaelmas Term 2015 Financial Computing with C++, Lecture 6 - p2/24 Outline Linked lists Linked
More informationC The new standard
C++11 - The new standard Lars Kühne Institut für Informatik Lehrstuhl für theoretische Informatik II Friedrich-Schiller-Universität Jena January 16, 2013 Overview A little bit of history: C++ was initially
More informationA 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 informationVariables. Data Types.
Variables. Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable. We had to write several lines of code, compile them, and then execute the resulting
More informationG52CPP C++ Programming Lecture 18. Dr Jason Atkin
G52CPP C++ Programming Lecture 18 Dr Jason Atkin 1 Last lecture Operator Overloading Strings and streams 2 Operator overloading - what to know Know that you can change the meaning of operators Know that
More informationProcedure Calls. Young W. Lim Mon. Young W. Lim Procedure Calls Mon 1 / 29
Procedure Calls Young W. Lim 2017-08-21 Mon Young W. Lim Procedure Calls 2017-08-21 Mon 1 / 29 Outline 1 Introduction Based on Stack Background Transferring Control Register Usage Conventions Procedure
More information6.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 information16.317: Microprocessor Systems Design I Fall 2014
16.317: Microprocessor Systems Design I Fall 2014 Exam 2 Solution 1. (16 points, 4 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling
More informationIntermediate Programming, Spring 2017*
600.120 Intermediate Programming, Spring 2017* Misha Kazhdan *Much of the code in these examples is not commented because it would otherwise not fit on the slides. This is bad coding practice in general
More informationAssembly Language: Function Calls
Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and returning Passing parameters Storing local variables Handling registers without interference
More information1. The term STL stands for?
1. The term STL stands for? a) Simple Template Library b) Static Template Library c) Single Type Based Library d) Standard Template Library Answer : d 2. Which of the following statements regarding the
More informationAn introduction to C++ template programming
An introduction to C++ template programming Hayo Thielecke University of Birmingham http://www.cs.bham.ac.uk/~hxt March 2015 Templates and parametric polymorphism Template parameters Member functions of
More informationCMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC
CMSC 341 Lecture 6 Templates, Stacks & Queues Based on slides by Shawn Lupoli & Katherine Gibson at UMBC Today s Topics Data types in C++ Overloading functions Templates How to implement them Possible
More informationCSI33 Data Structures
Outline Department of Mathematics and Computer Science Bronx Community College November 22, 2017 Outline Outline 1 Chapter 12: C++ Templates Outline Chapter 12: C++ Templates 1 Chapter 12: C++ Templates
More informationC++ For Science and Engineering Lecture 15
C++ For Science and Engineering Lecture 15 John Chrispell Tulane University Wednesday September 29, 2010 Function Review Recall the basics you already know about functions. Provide a function definition.
More informationIntroduction 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 informationShort 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 informationAssembly Language: Function Calls" Goals of this Lecture"
Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and returning" Passing parameters" Storing local variables" Handling registers without interference"
More informationCPS104 Recitation: Assembly Programming
CPS104 Recitation: Assembly Programming Alexandru Duțu 1 Facts OS kernel and embedded software engineers use assembly for some parts of their code some OSes had their entire GUIs written in assembly in
More informationCPSC 427: Object-Oriented Programming
CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 10 October 1, 2018 CPSC 427, Lecture 10, October 1, 2018 1/20 Brackets Example (continued from lecture 8) Stack class Brackets class Main
More informationCS201 - 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 informationX86 Addressing Modes Chapter 3" Review: Instructions to Recognize"
X86 Addressing Modes Chapter 3" Review: Instructions to Recognize" 1 Arithmetic Instructions (1)! Two Operand Instructions" ADD Dest, Src Dest = Dest + Src SUB Dest, Src Dest = Dest - Src MUL Dest, Src
More informationTEMPLATE IN C++ Function Templates
TEMPLATE IN C++ Templates are powerful features of C++ which allows you to write generic programs. In simple terms, you can create a single function or a class to work with different data types using templates.
More informationAssembly Language: Function Calls" Goals of this Lecture"
Assembly Language: Function Calls" 1 Goals of this Lecture" Help you learn:" Function call problems:" Calling and urning" Passing parameters" Storing local variables" Handling registers without interference"
More informationAssembly Language: Function Calls. Goals of this Lecture. Function Call Problems
Assembly Language: Function Calls 1 Goals of this Lecture Help you learn: Function call problems: Calling and urning Passing parameters Storing local variables Handling registers without interference Returning
More informationExercise 1.1 Hello world
Exercise 1.1 Hello world The goal of this exercise is to verify that computer and compiler setup are functioning correctly. To verify that your setup runs fine, compile and run the hello world example
More informationIntroduction 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 informationCPEG421/621 Tutorial
CPEG421/621 Tutorial Compiler data representation system call interface calling convention Assembler object file format object code model Linker program initialization exception handling relocation model
More informationProgramming C++ Lecture 5. Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor
Programming C++ Lecture 5 Howest, Fall 2013 Instructor: Dr. Jennifer B. Sartor Jennifer.sartor@elis.ugent.be S Templates S Function and class templates you specify with a single code segment an entire
More informationPractical Malware Analysis
Practical Malware Analysis Ch 4: A Crash Course in x86 Disassembly Revised 1-16-7 Basic Techniques Basic static analysis Looks at malware from the outside Basic dynamic analysis Only shows you how the
More informationInspecting and Manipulating binaries
Inspecting and Manipulating binaries Introduction. x86 architecture. Assembler. Binary inspection. General sample (crackme) Binary manipulation. Python to the rescue! Malware analysis What we (you) are
More information16.317: Microprocessor Systems Design I Fall 2015
16.317: Microprocessor Systems Design I Fall 2015 Exam 2 Solution 1. (16 points, 4 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by circling
More informationCSE 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 informationLogistics. Templates. Plan for today. Logistics. A quick intro to Templates. A quick intro to Templates. Project. Questions? Introduction to Templates
Logistics Templates Project Part 1 (clock and design) due Sunday, Sept 25 th Start thinking about partners for Parts 2-3 Questions? Logistics Important date: THURSDAY is Exam 1 Will cover: C++ environment
More informationObject Reference and Memory Allocation. Questions:
Object Reference and Memory Allocation Questions: 1 1. What is the difference between the following declarations? const T* p; T* const p = new T(..constructor args..); 2 2. Is the following C++ syntax
More informationG52CPP C++ Programming Lecture 16
G52CPP C++ Programming Lecture 16 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Last Lecture Casting static cast dynamic cast const cast reinterpret cast Implicit type conversion 2 How
More informationToday. andyoucanalsoconsultchapters6amd7inthetextbook. cis15-fall2007-parsons-lectvii.1 2
TEMPLATES Today This lecture looks at techniques for generic programming: Generic pointers Templates The standard template library Thebestreferenceis: http://www.cppreference.com/index.html andyoucanalsoconsultchapters6amd7inthetextbook.
More information/ 28 HLL assembly Q4: Conditional instructions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10
16.317: Microprocessor Systems Design I Fall 2014 Exam 2 November 5, 2014 Name: ID #: For this exam, you may use a calculator and one 8.5 x 11 double-sided page of notes. All other electronic devices (e.g.,
More informationCSC 2400: Computer Systems. Using the Stack for Function Calls
CSC 24: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing
More informationCSC 2400: Computer Systems. Using the Stack for Function Calls
CSC 24: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing
More informationTemplates (again) Professor Hugh C. Lauer CS-2303, System Programming Concepts
Templates (again) 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 informationIntroduction to Programming
Introduction to Programming session 6 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Spring 2011 These slides are created using Deitel s slides Sharif University of Technology Outlines
More informationCA341 - Comparative Programming Languages
CA341 - Comparative Programming Languages David Sinclair Dynamic Data Structures Generally we do not know how much data a program will have to process. There are 2 ways to handle this: Create a fixed data
More informationCOEN244: Class & function templates
COEN244: Class & function templates Aishy Amer Electrical & Computer Engineering Templates Function Templates Class Templates Outline Templates and inheritance Introduction to C++ Standard Template Library
More informationProgram template-smart-pointers-again.cc
1 // Illustrate the smart pointer approach using Templates 2 // George F. Riley, Georgia Tech, Spring 2012 3 // This is nearly identical to the earlier handout on smart pointers 4 // but uses a different
More informationAssembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction
Assembly Programmer s View Lecture 4A Machine-Level Programming I: Introduction E I P CPU isters Condition Codes Addresses Data Instructions Memory Object Code Program Data OS Data Topics Assembly Programmer
More informationA506 / C201 Computer Programming II Placement Exam Sample Questions. For each of the following, choose the most appropriate answer (2pts each).
A506 / C201 Computer Programming II Placement Exam Sample Questions For each of the following, choose the most appropriate answer (2pts each). 1. Which of the following functions is causing a temporary
More informationCSC 8400: Computer Systems. Using the Stack for Function Calls
CSC 84: Computer Systems Using the Stack for Function Calls Lecture Goals Challenges of supporting functions! Providing information for the called function Function arguments and local variables! Allowing
More informationAssembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall
Assembly Language Programming: Procedures EECE416 uc Charles Kim Howard University Fall 2013 www.mwftr.com Before we start Schedule of the next few weeks T Nov 19: Procedure and Calls (continued) R Nov
More informationCAAM 420 Fall 2012 Lecture 29. Duncan Eddy
CAAM 420 Fall 2012 Lecture 29 Duncan Eddy November 7, 2012 Table of Contents 1 Templating in C++ 3 1.1 Motivation.............................................. 3 1.2 Templating Functions........................................
More information16.317: Microprocessor Systems Design I Spring 2015
16.317: Microprocessor Systems Design I Spring 2015 Exam 2 Solution 1. (16 points, 4 points per part) Multiple choice For each of the multiple choice questions below, clearly indicate your response by
More informationObject Oriented Design
Object Oriented Design Chapter 6 Example Activity Diagram 1 Outline Chapter 6 Topics 6.6 C++ Standard Library Header Files 6.14 Inline Functions 6.16 Default Arguments 6.17 Unary Scope Resolution Operator
More informationUniversity of Illinois at Urbana-Champaign Department of Computer Science. First Examination
University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section
More informationC++ Structures Programming Workshop 2 (CSCI 1061U)
C++ Structures Programming Workshop 2 (CSCI 1061U) Faisal Qureshi http://faculty.uoit.ca/qureshi University of Ontario Institute of Technology C++ struct struct keyword can be used to define new data types
More informationCMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 16, SPRING 2013 TOPICS TODAY Project 6 Perils & Pitfalls of Memory Allocation C Function Call Conventions in Assembly Language PERILS
More informationOBJECT ORIENTED PROGRAMMING USING C++
OBJECT ORIENTED PROGRAMMING USING C++ Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both operators and functions can be overloaded
More informationLAB 5 Arithmetic Operations Simple Calculator
LAB 5 Arithmetic Operations Simple Calculator Objective: Practice arithmetic operation for the 80x86, such as add, subtract, multiple, divide, and mod. When dealing with the multiply, divide, and mod instructions
More informationTemplates. Zoltán Porkoláb: C++11/14 1
Templates From macros to templates Parameter deduction, instantiation,specialization Class templates, partial specialization Explicit instantiation Dependent types Scope resolution, lookup Mixins CRTP
More informationG52CPP C++ Programming Lecture 20
G52CPP C++ Programming Lecture 20 Dr Jason Atkin http://www.cs.nott.ac.uk/~jaa/cpp/ g52cpp.html 1 Wrapping up Slicing Problem Smart pointers More C++ things Exams 2 The slicing problem 3 Objects are not
More informationObject-Oriented Programming, Iouliia Skliarova
Object-Oriented Programming, Iouliia Skliarova You reuse code by creating new classes, but instead of creating them from scratch, you use existing classes that someone else has built and debugged. In composition
More informationCMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013
CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013 CONST POINTERS CONST POINTERS 4 ways to declare pointers in combination with const:!! int *ptr! const int *ptr!
More informationx86 assembly CS449 Fall 2017
x86 assembly CS449 Fall 2017 x86 is a CISC CISC (Complex Instruction Set Computer) e.g. x86 Hundreds of (complex) instructions Only a handful of registers RISC (Reduced Instruction Set Computer) e.g. MIPS
More information