C++ Lecture Notes: Fran Cois Fleuret November 21, 2005
C++ Lecture Notes: Fran Cois Fleuret November 21, 2005
Fran¸cois Fleuret
<[email protected]>
November 21, 2005
Ii
Note
This document is based on a C++ course given at the University of Chicago in
spring of 2001 and was modified for a course at EPFL in fall of 2004. It is still
a work in progress and needs to be polished to be a reference text.
The tools for this course are free-softwares. Mainly the GNU/Linux operating
system, the GNU C++ compiler, the emacs text editor, and a few standard
UNIX commands.
This document is c Franc¸ois Fleuret. It can be redistributed for free as is,
without any modification.
$Id: cpp-course.tex,v 1.33 2004/12/19 21:04:27 fleuret Exp $
Iv
Contents
1 Memory, CPU, files 1
1.1 Memory, files, CPU and compilation . . . . . . . . . . . . . . . . 1
1.1.1 Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1.2 Data types . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.3 Signal quantification . . . . . . . . . . . . . . . . . . . . . 2
1.1.4 File system . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.5 Size orders of magnitude . . . . . . . . . . . . . . . . . . . 3
1.2 CPU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.1 What is a CPU . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.2 Speed orders of magnitude . . . . . . . . . . . . . . . . . 4
1.3 Compilation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.1 Role of compilation . . . . . . . . . . . . . . . . . . . . . . 5
1.3.2 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4 Object-Oriented Programming . . . . . . . . . . . . . . . . . . . 7
2 Shell and basic C++ 9
2.1 GNU/Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1.1 What is Linux . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1.2 What is Open-Source . . . . . . . . . . . . . . . . . . . . 9
2.1.3 Tools for this course . . . . . . . . . . . . . . . . . . . . . 11
2.2 Shell and simple file management . . . . . . . . . . . . . . . . . . 11
2.2.1 File names and paths . . . . . . . . . . . . . . . . . . . . 11
2.2.2 Shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
2.2.3 Basic commands . . . . . . . . . . . . . . . . . . . . . . . 12
2.2.4 References for documentation . . . . . . . . . . . . . . . . 14
2.3 First steps in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3.1 Data types . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3.2 A simple example of variable manipulation . . . . . . . . 15
2.3.3 Variable naming conventions . . . . . . . . . . . . . . . . 16
2.3.4 Streams, include files . . . . . . . . . . . . . . . . . . . . . 16
2.3.5 The sizeof operator . . . . . . . . . . . . . . . . . . . . . . 17
2.3.6 The if statement . . . . . . . . . . . . . . . . . . . . . . . 17
2.3.7 The for statement . . . . . . . . . . . . . . . . . . . . . . 18
2.3.8 The while statement . . . . . . . . . . . . . . . . . . . . . 19
4.2.2 Dynamic arrays . . . . . . . . . . . . . . . . . . . . . . . . 45
4.2.3 Test of a null pointer . . . . . . . . . . . . . . . . . . . . . 46
4.2.4 A non-trivial example using dynamic memory allocation . 46
4.2.5 Dynamically allocated bi-dimensional arrays . . . . . . . . 47
4.2.6 What is going on inside: the stack and the heap . . . . . 48
4.3 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4.3.1 Declaration vs. definition . . . . . . . . . . . . . . . . . . 49
4.3.2 The const statements . . . . . . . . . . . . . . . . . . . . 50
4.3.3 The enum type . . . . . . . . . . . . . . . . . . . . . . . . 51
4.3.4 The break statement . . . . . . . . . . . . . . . . . . . . . 51
4.3.5 Bitwise operators . . . . . . . . . . . . . . . . . . . . . . . 52
4.3.6 The comma operator . . . . . . . . . . . . . . . . . . . . . 52
5 War with the bugs 55
5.1 Preamble . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.2 The Bug Zoo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
5.2.1 The program crashes: Segmentation fault . . . . . . . . 55
Unauthorized memory access . . . . . . . . . . . . . . . . 56
Incorrect system call . . . . . . . . . . . . . . . . . . . . . 57
5.2.2 The program crashes: Floating point exception . . . . 57
5.2.3 The program never stops . . . . . . . . . . . . . . . . . . 58
5.2.4 The program uses more and more memory . . . . . . . . . 58
5.2.5 The program does not do what it is supposed to do . . . 58
5.3 How to avoid bugs . . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.3.1 Write in parts . . . . . . . . . . . . . . . . . . . . . . . . . 60
5.3.2 Good identifiers . . . . . . . . . . . . . . . . . . . . . . . . 60
5.3.3 Use constants instead of numerical values . . . . . . . . . 60
5.3.4 Comment your code . . . . . . . . . . . . . . . . . . . . . 61
5.3.5 Symmetry and indentation . . . . . . . . . . . . . . . . . 61
5.3.6 Use a DEBUG flag . . . . . . . . . . . . . . . . . . . . . . 62
5.4 How to find bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
5.4.1 Print information during execution . . . . . . . . . . . . . 63
5.4.2 Write the same routine twice . . . . . . . . . . . . . . . . 63
5.4.3 Heavy invariants . . . . . . . . . . . . . . . . . . . . . . . 64
5.5 Anti-bug tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.5.1 gdb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.5.2 Valgrind . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
6 Homework 69
7 Cost of algorithm, sorting 73
7.1 Big-O notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
7.1.1 Why ? How ? . . . . . . . . . . . . . . . . . . . . . . . . . 73
7.1.2 Definition of O(.) . . . . . . . . . . . . . . . . . . . . . . . 74
7.1.3 Some O(.) . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
7.1.4 Summing O(.)s . . . . . . . . . . . . . . . . . . . . . . . . 74
Chapter 1
Memory, CPU, files
Chapter 2
Shell and basic C++
2.1 GNU/Linux
2.1.1 What is Linux
1. A big piece of software called the kernel which run as soon as you turn
on a Linux computer, and is able to deal with all devices (hard disk,
keyboard, mouse, network cards, etc.)
2. X-Window, which controls the graphical display ;
3. a large list of programs that allow you to edit texts, manage your files,
compile source codes, do network administration, etc.
The main goal of this course is to learn C++, not to go into the specificities
of neither Linux or X-Window. We will focus on standard C++ which can be
used on other operating systems (Windows, MacOS, BeOS, etc.)
2.1.2 What is Open-Source
Note that all softwares we will use in this course are free open-source soft-
wares. Which means :
1. you can get them for free ;
2. you can get their source codes ;
3. you can use them, distribute them and modify them as long as you give
the same freedom to the users of what you distribute.
The main license for such software is the GPL or the BSD one. You can get
Linux either on Internet (in that case you need a very good connection), or on
a CD.
The Linux kernel was originally written by a student called Linus Torvald,
and has been since then heavily improved by thousands of programmers. Its
existence proves by itself the incredible power of the open source model of de-
velopment.
2.1.3 Tools for this course
The main setup for this course will be a GNU/Linux computer, the gcc compiler,
the emacs text editor and the standard shell commands. Since all this is very
standard, any Linux distribution should be fine.
MS-Windows users who are reluctant to install this free open-source operating
system have two options:
1. Use a one-CD live distribution, for instance Knoppix1 which allows to run
a complete GNU/Linux system without installing any data on the hard
disk. For such a configuration, the data can be saved on an usb key or
floppy disk.
2. Use Cygwin2 which is free but only partially open-source and provides all
the classical UNIX tools under Windows, thus allowing to read and write
on the Windows partitions.
2.2 Shell and simple file management
2.2.1 File names and paths
We will call path a list of directory names indicating a location where can be
found either files or other directories. The convention is to separate directory
names with a ’/’. If a directory a contains a directory b, we will say a is the
parent directory of b.
Notes on c++