31-01-2013, 04:34 PM
C++ Notes
1C++.pdf (Size: 261.65 KB / Downloads: 85)
Introduction
This is a very brief introduction to the C++ programming language, to be used in courses held at the
D´epartement d’Informatique and the D´epartement de Math´ematiques Appliqu´ees of the ´ Ecole Polytechnique.
The aim of the course is to teach the basics of the C++ language in a practical, hands-on way,
without many of the more technical details concerning the more advanced topics.
Generalities
This C++ course is based on the GNU C++ compiler v. 4.0 and above running under the Unix operating
system. Essential Unix tools, beside the compiler, are: make, tar, gzip/gunzip. As debuggers, we use
gdb with the ddd front-end, and valgrind.
By “computer”, here, we do not mean a theoretical computer science model such as a Turing machine,
but a real, physical desktop or laptop computer. Although any architecture capable of running a Unix
and the above-mentioned tools will do, the actual C++ course is given on desktop PCs with a 32 bit
Intel processor running a mainstream Linux distribution.
Definitions
The CPU (Central Processing Unit) of a computer is where all the logical and arithmetic tests, loops
and decisions take place, and where control commands and data exchanges are issued to devices such as
memory, disk, screen, etc. The behaviour of the CPU is determined by its state, which is described by
the content of all its registers and internal memory caches. Letting S be the set of possible CPU states,
the CPU acts like a deterministic function f : S ! S. According to this model, to each state s 2 S there
corresponds a next state s′ = f(s). The rate at which the CPU changes state is governed by the system
clock (usual rates are between 1 and 3 GHz). Thus, around every billionth of a second, the CPU changes
its state.
Data
Computers can only perform arithmetic and logic operations in the field F2 = {0, 1}. A set of 8 bits is
called a byte. The 8086 and 80286 had a 16-bit bus (the bus is the data transfer capacity per clock tick);
modern Intel-based processors have 32- or 64-bit wide buses. An n-bit wide memory chunk can hold 2n
different values. These are either indexed from −2n−1 to 2n−1 − 1 or from 0 to 2n − 1. An integer in
C/C++ on an Intel architecture is usually stored in 32 bits (=4 bytes). Floating point values are stored
(usually in 8 bytes) according to a specific code. Any type of data that can be represented by an encoding
of finite-sized integers can be stored in a computer memory. In C/C++ data are categorized according to
their types. Data types can be elementary: bool (C++ only), char, short, int, long, float, double or
user-defined: union, struct, class (C++ only). A user-defined data type is a finite sequence of existing
data types and occupies either the sum (struct, class) or the maximum (union) of the sizes of the
sequence elements. Memory addresses, by contrast, normally occupy a fixed amount of storage (32 bits
in 32-bit CPUs).
Memory
Computers rely on different types of memory to store data (this means: values and addresses). Read-Only
Memory (ROM) can only be read. Random-Access Memory (RAM) can be read and written to. Other
types of memory include EEPROMs, Flash RAM, disks, CD-ROMs, tapes and so on. Within the scope
of these notes, we shall only be concerned with RAM type memory. This is very fast (in modern PCs
RAM chips are refreshed at frequencies that are around one tenth as that of the CPU), comparatively
expensive, and loses information if the power is switched off. The model of memory that best fits these
C++ notes is that of a linear array, as depicted in Fig. 1.
The operating system
An operating system is a set of running programs that handle the interactions between the computer (in
all its parts) and its users. The operating system assigns memory to each program and protects each
assigned memory segment from unauthorized access (so that a malfunctioning program cannot bring the
whole computer to a halt); it shares the CPU times amongst all the running processes; it operates all
data exchanges with external devices (keybord, screen, network, printers, disks). Operating systems have
three main parts.
Program execution
When2 a program is loaded into memory, it is organized into three areas of memory, called segments:
the text segment, stack segment, and heap segment. The text segment (sometimes also called the code
segment) is where the compiled code of the program itself resides. This is the machine language representation
of the program steps to be carried out, including all functions making up the program, both
user defined and system.
The remaining two areas of system memory is where storage may be allocated by the compiler for
data storage. The stack is where memory is allocated for variables within functions. A stack is a Last In
First Out (LIFO) storage device where new storage is allocated and deallocated at only one “end” (the
top of the stack).
Every C/C++ program begins executing at a function called main(): space is allocated on the stack
for all variables declared within main(). If main() calls a function, say myFunction(), additional storage
is allocated for the variables in myFunction() at the top of the stack. Notice that the parameters passed
by main() to myFunction() are also stored on the stack. If myFunction() were to call any additional
functions, storage would be allocated at the new top of stack. When myFunction() returns, storage for
its local variables is deallocated, and the top of the stack is resumed at the old position. As can be seen,
the memory allocated in the stack area is used and reused during program execution. It should be clear
that memory allocated in this area will contain garbage values left over from previous usage.