0% found this document useful (0 votes)
94 views7 pages

C++ Lecture Notes: Fran Cois Fleuret November 21, 2005

This document provides lecture notes for a C++ course. It discusses key topics like memory, files, CPUs, compilation, and object-oriented programming. The tools used for the course include GNU/Linux, GNU C++ compiler, emacs text editor, and UNIX commands. The document is a work in progress and needs polishing to be a reference text. It is freely redistributable without modification under the stated license.

Uploaded by

balu mughal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views7 pages

C++ Lecture Notes: Fran Cois Fleuret November 21, 2005

This document provides lecture notes for a C++ course. It discusses key topics like memory, files, CPUs, compilation, and object-oriented programming. The tools used for the course include GNU/Linux, GNU C++ compiler, emacs text editor, and UNIX commands. The document is a work in progress and needs polishing to be a reference text. It is freely redistributable without modification under the stated license.

Uploaded by

balu mughal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

C++ lecture notes

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

1.1 Memory, files, CPU and compilation


1.1.1 Memory
• Used to store informations ;
• 0 / 1 representation with electric voltage ;
• the memory or a file is a gigantic set of bytes, each of them composed of
8 bits ;
• each of the bytes has an index called its address.
543210
#1
#2
#3
#4
1
0
1
1
01
0
0
0
11
1
0
1
10
0
0
1
10
1
0
1
00
0
0
0
00
0
0
1
01
1
0
1
0
76
#0
... ...
Figure 1.1: The memory is a succession of binary values called bits. A group
of 8 such bits is called a byte and can be indexed by its address.
24 − bits color encoding.
This is as simple as it sounds: in the computer memory, an image is encoded as
a succession of groups of three bytes, each one of those triplets corresponding
to the three component red, green and blue of a point on the screen.
1.1.4 File system
To store information when the computer is turned off, or to manipulate larger
set of bytes, one uses magnetic storage devices. The most common are the hard
disks. Informations are stored under the forms of files, each of them having a
name and a size.
A file is very similar to the memory : a set of bytes indexed by their positions.
• Files can be very large, up to hundred times the memory ;
• the file access is very slow compared to memory access.
In practice the files are organized on a hard disk with directories (also called
folder under Microsoft or Apple Operating systems). A directory can contains
files and other directories (we can imagine it as a file containing a list of names
of files and directories). This leads to a very powerful hierarchical organization.
1.1.5 Size orders of magnitude
Because a very large number of bytes is required to encode useful objects, the
standard units go up to very large numbers : 1Kbyte = 1024 bytes, 1Mbytes =
1024 Kbytes, 1Gbyte = 1024 Mbytes, 1Tbyte = 1024 Gbytes.
1.2 CPU
1.2.1 What is a CPU
The Central Processing Unit (CPU) is a very fast electronic device, able to
read from and write into the memory, and to do arithmetic operations. The
native language of a CPU is called the assembler. A CPU has a frequency,
which indicates roughly how many operations it can do each second.
Compression of a 3 min song in mp3 ≃ 3 minutes with a P600
Compression of a 90 min movie in divx ≃ 15 hours with a P600
Neural network training Up to days
Table 1.2: Speed of processors and computation time.
1.3 Compilation
1.3.1 Role of compilation
The compilation operation consists of translating a program written in a com-
plex and human-readable language like C++, C, PASCAL, ADA, etc. into assembler
code, which can be directly understood by the CPU.
Practically, a compiler is itself a program which reads source files written by a
human, for instance in C++, and generate object files which contains assembler
code.
Why using a language like C++ and a compiler instead of writing assembler :
1. CPUs have different assembler codes, so using a compiler allows to use the
same language on different CPUs ;
2. compilers are able to spot in your program things that can be rewritten
differently and more efficiently ;
3. languages like C++ or Java allow to manipulate complex structures of data
in an abstract way.
Finally, to write a C++ program, the programmer first has to write a file con-
taining the C++ source code itself. Then, he runs the compiler, giving the name
of the source file and the name of the object file as parameters. The resulting
object file can then be run as a program itself.
1.3.2 Example
Below is an example of a shell session to create, compile and execute a program.
The emacs command is a text editor used to create the file something.cc. The
g++ command is the compiler, which generates the object file something from
the source file something.cc. Figure 1.3 shows the emacs window while editing
the source file.
1.4 Object-Oriented Programming
The “object approach”, which is the fundamental idea in the conception of C++
programs, consists of building the program as an interaction between objects :
1. In all part of the program that use a given object, it is defined by the
methods you can use on it ;
2. you can take an existing object type (called a class) and add to it data
and methods to manipulate it.
The gains of such an approach are :
1. Modularity : each object has a clear semantic (Employer or DrawingDevice),
a clear set of methods (getSalary(), getAge(), or drawLine(), drawCircle().
2. Less bugs : the data are accessed through the methods and you can use
them only the way to object’s creator wants you to.
3. Reutilisability : you can extend an existing object, or you can build a new
one which could be used in place of the first one, as long as it has all the
methods required (for example the Employer could be either the CEO o
a worker, both of them having the required methods but different data
associated to them, DrawingDevice could either be a window, a printer,
or anything else).

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++

C++ lecture Notes

You might also like