Transcript
Page 1: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

An Introduction to C++

Lecture 01: Introduction

Roger Wolf15. Mai 2018

Page 2: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 1:

Say “hello” to your laptop!

1/25

Page 3: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

From bits ‘n’ bytes

● In computers information is encoded in Bits (=binary digit), logical units that can take two values:

● 1 Bit (2 states: 0/1) ● 1 Byte = 8 Bit (28=256 states: e.g. 10010101)

● 1024 Bit = 1 kBit (=128 Byte)

● 1024 kByte = 1 kByte

● 1024 kByte = 1 MByte

● 1024 MByte = 1 GByte

● 1024 GByte = 1 TByte

● 1024 TByte = 1 PByte

● Single information unit (“word”) in the memory of your computer

2/25

Page 4: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The guts of your computer

→ usually tens of GHz.

CPU (1)

RAM (2)

HDD (3)

→ usually hundreds of GByte.

→ usually 4-8 GByte.

(1) central processing unit.(2) random access memory.(3) hard disc drive.

3/25

Page 5: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The guts of your computer

→ usually tens of GHz.

CPU (1)

RAM (2)

HDD (3)

→ usually hundreds of GByte.

→ usually 4-8 GByte.

(1) central processing unit.(2) random access memory.(3) hard disc drive.

Your data live here:Input to/output from computer programs.

3/25

Page 6: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The guts of your computer

→ usually tens of GHz.

CPU (1)

RAM (2)

HDD (3)

→ usually hundreds of GByte.

→ usually 4-8 GByte.

(1) central processing unit.(2) random access memory.(3) hard disc drive.

Your data live here:Input to/output from computer programs.

Your software lives here: Programs working with inputs and producing outputs.

3/25

Page 7: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

The guts of your computer

→ usually tens of GHz.

CPU (1)

RAM (2)

HDD (3)

→ usually hundreds of GByte.

→ usually 4-8 GByte.

(1) central processing unit.(2) random access memory.(3) hard disc drive.

Your data live here:Input to/output from computer programs.

Your software lives here: Programs working with inputs and producing outputs.

During the course RAM will have many names among those:

Heap, stack, active memory, …

3/25

Page 8: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

64 Bit vs 32 Bit

• This refers to your CPU’s (more general arithmetic logic units’ (=ALUs))…

● You might have heard that your computer has a 32 Bit or 64 Bit architecture.

• … register size, to which your CPU applies logical operations.

• … size of the address bus, which determines the size of memory addresses.

• … size of the data bus, which determines the maximal size of processable integer numbers.

● In C(++) this has also consequences for the size of your built-in data types. A few examples are given below:

→ a “word”→ four “words”→ four or eight “words”

4/25

Page 9: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

How your computer program physically works

● Your program/binary/executable in the RAM/memory of your laptop:

1. Byte

Memory address of 1. Byte

5/25

Page 10: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

How your computer program physically works

● Your program/binary/executable in the RAM/memory of your laptop:

1. Byte 2. Byte

Memory address of 1. Byte

Memory address of 2. Byte

5/25

Page 11: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

How your computer program physically works

● Your program/binary/executable in the RAM/memory of your laptop:

1. Byte 2. Byte 3. Byte

Memory address of 1. Byte

Memory address of 2. Byte

Memory address of 3. Byte

4. Byte

Memory address of 4. Byte

5/25

Page 12: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

How your computer program physically works

● Your program/binary/executable in the RAM/memory of your laptop:

1. Byte 2. Byte 3. Byte

Memory address of 1. Byte

Memory address of 2. Byte

Memory address of 3. Byte

Memory address of a uint

4. Byte

Memory address of 4. Byte

5/25

Page 13: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

How your computer program physically works

● Your program/binary/executable in the RAM/memory of your laptop:

1. Byte 2. Byte 3. Byte

Memory address of 1. Byte

Memory address of 2. Byte

Memory address of 3. Byte

Memory address of a uint

Memory address of a function call

4. Byte

Memory address of 4. Byte

5/25

Page 14: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

How your computer program physically works

● Your program/binary/executable in the RAM/memory of your laptop:

1. Byte 2. Byte 3. Byte

Memory address of 1. Byte

Memory address of 2. Byte

Memory address of 3. Byte

Memory address of a uint

Memory address of a function call

● Whenever your computer crashes with a segmentation violation (=seg fault) your processor is sent to a memory address it should not be and where it will screw the execution of your program.

● Whenever the footprint of your program in your RAM grows and grows with execution time it is likely that your program dynamically allocates memory that is not freed after use (→ memory leak)

● Whenever a hacker attacks your computer (s)he finds loop wholes in your programs, which allow him/her e.g. by address shifts to access sensitive data in your computer.

6/25

Page 15: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 2:

Warm-up – the bottom up approach

7/25

Page 16: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Source file → executable

● C(++) is written as plain text in a source file (conventionally with ending .cc or .C).

● The source file needs to be translated into machine language (=01100101… ).

● For C(++) this is done in a two-step process:

● Step-1: source.cc (source file) → source.o (object file = machine language + function addresses).

● Step-2: All function calls, which are not defined inside source.cc are linked from external libraries (→ see Lecture­02).

● This translation process is provided by a compiler. The compiler defines(!) the exact implementation of C(++).

● This implementation may vary especially across operation systems, but international standards exist to define the language (→ ISO C++). For my demonstrations I will use UNIX g++ from the gcc project, as an installed standard of my Ubuntu 16.04 (w/o any extras).

8/25

Page 17: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

One-slide primer to C(++)

● The computer language C(++) is organized in functions, indicated by braces, e.g. “myFunction( … )”.

● Each function can have an arbitrary number of arguments and has a return value.

● The return value can be void (=nothing).

● The function that starts the executable is called main(...).

● Everything that should be executed within main is organized in a command block and separated by curly brackets “{ … }” from any other code.

● Within a command block an arbitrary number of statements can be executed, separated by semicolons “;”. Each semicolon leads to the immediate execution of the command.

9/25

Page 18: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

One-slide primer to C(++)

● The computer language C(++) is organized in functions, indicated by braces, e.g. “myFunction( … )”.

● Each function can have an arbitrary number of arguments and has a return value.

● The return value can be void (=nothing).

● The function that starts the executable is called main(...).

● Everything that should be executed within main is organized in a command block and separated by curly brackets “{ … }” from any other code.

● Within a command block an arbitrary number of statements can be executed, separated by semicolons “;”. Each semicolon leads to the immediate execution of the command.

● Note: A function cannot have any arbitrary name. E.g. it may not start with a number; also the name is case sensitive. Usually your compiler will tell you when you use names that your are not supposed to use.

9/25

Page 19: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Variable declaration and definition

● Within statements all objects (lvalues) need to be declared and defined:

● myLetter is an lvalue – ‘a’ is an rvalue.

10/25

Page 20: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Variable declaration and definition

● Within statements all objects (lvalues) need to be declared and defined:

● myLetter is an lvalue – ‘a’ is an rvalue.

Type declaration

10/25

Page 21: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Variable declaration and definition

● Within statements all objects (lvalues) need to be declared and defined:

● myLetter is an lvalue – ‘a’ is an rvalue.

Type declaration

Value definition

10/25

Page 22: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Variable declaration and definition

● Within statements all objects (lvalues) need to be declared and defined:

● myLetter is an lvalue – ‘a’ is an rvalue.

● Note: Other languages e.g. like python and FORTRAN know implicit type declaration for lvalues. For C(++) this is supported for rvalues, but not for lvalues.

Type declaration

Value definition

10/25

Page 23: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Variable declaration and definition

● Within statements all objects (lvalues) need to be declared and defined:

● myLetter is an lvalue – ‘a’ is an rvalue.

Type declaration

Value definition

Note: You can add an arbitrary number of comment lines to your code. It is good practice to make use of this possibility:

● Note: Other languages e.g. like python and FORTRAN know implicit type declaration for lvalues. For C(++) this is supported for rvalues, but not for lvalues.

11/25

Page 24: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 3:

Say “hello” to C++

12/25

Page 25: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Say “hello world”

● Your first C++ program:

13/25

Page 26: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Say “hello world”

Preprocessor statement (keyword identified by “#”)

13/25

● Your first C++ program:

Page 27: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Say “hello world”

Preprocessor statement (keyword identified by “#”)

Declared type of return value of main() (in this case int)

13/25

● Your first C++ program:

Page 28: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Say “hello world”

Preprocessor statement (keyword identified by “#”)

Actual return value of the function (in this case 0), return is a C(++) keyword

Declared type of return value of main() (in this case int)

13/25

● Your first C++ program:

Page 29: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Data types and operators

● A data type is an object with a set of operations that can be applied to it.

● Example: int is a built-in data type to which several operations can be applied:→ binary operators: •*•, •+•, •­•, •/•, •=•, •+=•, •­=•, •*=•, •/=•, … → unary operators: ++•, ­­•, *•, &•, [•].

● Examples:

● Operator binding: in general “unary” binds stronger than “binary”, “binary” in mathematical sense. In case of doubts try it out or set braces.

14/25

Page 30: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Type arrays

● You can define arrays of all kinds of data types:

15/25

Page 31: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Type arrays

● You can define arrays of all kinds of data types:

This is the index operator. [i] indicates position i in the array

15/25

Page 32: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Type arrays

● You can define arrays of all kinds of data types:

This is the index operator. [i] indicates position i in the array

● The declaration/initialization of higher dimensional arrays works in logic extension of one-dimensional arrays.

15/25

Page 33: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Type arrays

● You can define arrays of all kinds of data types:

This is the index operator. [i] indicates position i in the array

● Note: If you choose an index outside the declared range of the array your program will “point” just somewhere in your memory.

● The declaration/initialization of higher dimensional arrays works in logic extension of one-dimensional arrays.

15/25

Page 34: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

C structs

● You can also declare user-defined groups of data types in a C struct:

16/25

Page 35: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

C structs

● You can also declare user-defined groups of data types in a C struct:

With typedef you can redefine and thus simplify each variable name that has been declared before

16/25

Page 36: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

C structs

● You can also declare user-defined groups of data types in a C struct:

With typedef you can redefine and thus simplify each variable name that has been declared before

16/25

Page 37: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

C structs

● You can also declare user-defined groups of data types in a C struct:

With typedef you can redefine and thus simplify each variable name that has been declared before

This is how to access the individual elements of the C struct

16/25

Page 38: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

C structs – NB: Bit fields –

● You can use C structs to make the access to bit patterns more human readable:

This notation indicates the bit length of the field

17/25

Page 39: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS FACULTY

Part 4:

Pointers & references

18/25

Page 40: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Pointers & references

● Note: the unary operators *• and &• are not mathematical but C(++) specific operations:

19/25

Page 41: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Pointers & references

● Note: the unary operators *• and &• are not mathematical but C(++) specific operations:

● & is the address operator: it returns the address of the actual date in memory

● * is the indirection operator: it returns the date at the specified address in memory

19/25

Page 42: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Pointers & references

● Note: the unary operators *• and &• are not mathematical but C(++) specific operations:

● & is the address operator: it returns the address of the actual date in memory

● * is the indirection operator: it returns the date at the specified address in memory

Note: an array points to its first element. You can access its elements also via pointer:

19/25

Page 43: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Dynamic memory allocation

● With the help of the statements new and delete memory can be allocated dynamically, at the position, where a pointer is pointing to:

20/25

Page 44: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Dynamic memory allocation

● With the help of the statements new and delete memory can be allocated dynamically, at the position, where a pointer is pointing to:

20/25

pointer

Page 45: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Dynamic memory allocation

● With the help of the statements new and delete memory can be allocated dynamically, at the position, where a pointer is pointing to:

20/25

pointer

Allocate new int

Page 46: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Dynamic memory allocation

● With the help of the statements new and delete memory can be allocated dynamically, at the position, where a pointer is pointing to:

Allocate memory and immediately initialize it with value

20/25

Page 47: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Dynamic memory allocation

● With the help of the statements new and delete memory can be allocated dynamically, at the position, where a pointer is pointing to:

Allocate the memory for n integers (uninitialized)

Allocate memory and immediately initialize it with value

20/25

Page 48: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Dynamic memory allocation

● With the help of the statements new and delete memory can be allocated dynamically, at the position, where a pointer is pointing to:

21/25

Page 49: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Dynamic memory allocation

● Dynamic memory allocation is a powerful tool (see rest of course).

● It opens the door to the most effective usage of your memory.

● BUT you must be extremely careful: you take over full responsibility for the management of the memory that you allocate (→ object ownership).

● If you do not free the memory that you allocated you create memory leaks.

● If you allocate more memory than you have left in your RAM new will throw a bad_alloc exception during runtime.

● If you try to delete memory twice your program will crash.

● All these problems will occur at runtime. There is no assistance from your compiler during compile time.

21/25

Page 50: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

“Call by reference” ↔ “call by value”

● References play a larger role in function calls:Example: “call by value”

22/25

Page 51: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

“Call by reference” ↔ “call by value”

● References play a larger role in function calls:Example: “call by reference”

22/25

Page 52: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

“Call by reference” ↔ “call by value”

● References play a larger role in function calls:Example: “call by reference”

● Note: the return value of arithmeticMean could also be void in this case. The modified value can be found in var1.

● Note: references can only hold properly defined lvalues.

22/25

Page 53: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Function pointers

● You can even have pointers to functions:

23/25

Page 54: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Function pointers

● You can even have pointers to functions:

● A typical use cases is e.g. a “listener” function to be passed as argument to another function: whenever the other function turns the program into a certain state the listener will be executed.

● Another important use case in science is a fit function that is evaluated by a minimizer algorithm.

● If you do not explicitly need it, better keep you fingers off. Otherwise you can easily lookup more documentation in the web.

23/25

Page 55: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Passing arguments to main(...)

● An executable has always at least one argument → the executable name (argc=1 and argv[0]=executable name).

● An equivalent notation for char **argv is char *argv[] (argv[] indicates an array of char).

● This is done in a pre-defined way to pass arguments:

24/25

Page 56: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

● This is done in a pre-defined way to pass arguments:

● An executable has always at least one argument → the executable name (argc=1 and argv[0]=executable name).

● An equivalent notation for char **argv is char *argv[] (argv[] indicates an array of char).

We will discuss this for loop in a later lesson.

Passing arguments to main(...)24/25

Page 57: An Introduction to C++ Lecture 01: Introductionekprwolf/teaching/... · Priv. Doz. Dr. Roger Wolf karlsruhe.de/~rwolf/ INSTITUTE OF EXPERIMENTAL PARTICLE PHYSICS (IEKP) – PHYSICS

Priv. Doz. Dr. Roger Wolf http://ekpwww.physik.uni­karlsruhe.de/~rwolf/

Summary

● Very simplistic look into your computer to understand how C++ works on your computer.

● First dive into C(++):

● Variable declaration vs definition/initialization.

● Data types (char, int, float, double, … ).

● Type arrays.

● Statements/comments.

● C structs.

● Pointers and references.

● “Call by value” ↔ “call by reference”.

● Dynamic memory allocation.

● Pointers to functions.

25/25


Recommended