OCI - session 1: From C to C++ (and some C++ ... - Thibault Cholez

Feb 16, 2017 - 1 Introduction. 2 Reminder on C compiler ... Learning more advanced features used in embedded systems. Structure of the class ... C and C++ are still the most widely used programming languages. For operating systems ...
155KB taille 1 téléchargements 190 vues
OCI - session 1: From C to C++ (and some C++ features)

Thibault CHOLEZ - [email protected] TELECOM Nancy - Universit´ e de Lorraine LORIA - INRIA Nancy Grand-Est From Nicolas Rougier’s C++ crash course

16/02/2017

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Plan

1

Introduction

2

Reminder on C compiler

3

Reminder on memory managment

4

Some features

5

Exercices

OCI - session 1: From C to C++ (and some C++ features) 2 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Plan

1

Introduction

2

Reminder on C compiler

3

Reminder on memory managment

4

Some features

5

Exercices

OCI - session 1: From C to C++ (and some C++ features) 3 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

First words Goal of the class Hands-on C++ ! Learning similarities and differencies regarding C and JAVA Learning basic concepts and features of C++ Learning more advanced features used in embedded systems

Structure of the class Three sessions of two hours For each session : 30 min talk on C++ concepts, then short practical exercices on computer

OCI - session 1: From C to C++ (and some C++ features) 4 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

First words Program From C to C++ : reminder C++ features and object programming Tools for reliable programming Optimizations and performance

Environment Basic Linux OS and tools : g++, console, text editor No Integrated Development Environment (hides complexity)

OCI - session 1: From C to C++ (and some C++ features) 5 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Why a class on C++ ?

C and C++ are still the most widely used programming languages For operating systems (SLOC in debian : 49% in C, 21% in C++) For general purpose software (SourceForge statistics : 18% of software in C++, 16% in C) ... and also for embedded software (with an emphasis on real time or low ressource constraints) !

OCI - session 1: From C to C++ (and some C++ features) 6 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Why not JAVA for embedded software ? Pros Programmer friendly : features for reliability, garbage collector, etc. Portable ”write once, run anywhere” : same byte-code works everywhere (not tied to hardware but to JVM, great advantage to deal with smartphones heterogeneity)

Cons JRE simulates hardware : slower execution (not as bad as it used to be thanks to ”just-in-time” compilation but still) Waste of ressources (memory, CPU) Odd coupling with hardware : JRE as a stack machine while Dalvik is register-based Unpredictability of JVM is an issue for real time system : you don’t want the garbage collector to work during during a critical time OCI - session 1: From C to C++ (and some C++ features) 7 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Stack VS Registers Add register a to register b and store the result in c Stack based (Java) push a push b add pop c

Register based (Dalvik) add a,b,c

Which wins ? a program written for a stack based machine takes much more in memory (not fitted to embedded systems) a program written for a register based machine tends to be slower than its equivalent for a stack based machine. But fast enough for embedded devices OCI - session 1: From C to C++ (and some C++ features) 8 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

C / C++ Pros C is the closest high-level language to the machine, C++ is ”just” an improved version with object-oriented features Close to the hardware, efficient execution and low ressource usage (with proper code) No interference of VM to disturb execution Possibility to mix C++ with asm code

Cons Hard to write efficient and reliable code without strong knowledge and experience (that’s why you are here) Specific compilation for each hardware architecture needed OCI - session 1: From C to C++ (and some C++ features) 9 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Plan

1

Introduction

2

Reminder on C compiler

3

Reminder on memory managment

4

Some features

5

Exercices

OCI - session 1: From C to C++ (and some C++ features) 10 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Pre-processor Just like in C, very dumb ”text editing” tool, still useful Features #define MACRO NAME value find and replace, (in C) good to define constant in UPPER CASE #include
line replaced by whole content of the header file #ifdef macro_name /* Code to use if macro defined */ #else /* Code to use otherwise */ #endif #ifndef SOME_UNIQUE_NAME #define SOME_UNIQUE_NAME #endif OCI - session 1: From C to C++ (and some C++ features) 11 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Compilation and Link edition Simplest way gcc -o binary source1.c source2.c source3.c

Better way We want to approach modular programming for many reasons (separation of concerns, team work, maintenance) Source code organized in different meaningful files and folders, as autonomous as possible Do not recompile everything for a small change : recompile only the modified file(s) and redo the link edition The linker creates the exec file from multiple object files / librairies gcc -c source1.c gcc -o binary source1.o source2.o source3.o OCI - session 1: From C to C++ (and some C++ features) 12 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Separate compilation and Makefile Makefile Simple automated tool dealing with file dependencies during separate compilation : target: dependencies commands additionnal rules are commonly used : ”all” to generate the main executable, ”clean” to remove object files, etc. also, additionnal variables are commonly used : CC for the compiler, CFLAGS for the compilation options LDFLAGS for the linker options, EXEC for the name of the executable file

OCI - session 1: From C to C++ (and some C++ features) 13 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Example of Makefile CXX=g++ CXXFLAGS=-Wall LDFLAGS= EXEC=hello all: $(EXEC) hello: hello.o main.o $(CXX) -o hello hello.o main.o $(LDFLAGS) hello.o: hello.c $(CXX) -o hello.o -c hello.c $(CXXFLAGS) main.o: main.c hello.h $(CXX) -o main.o -c main.c $(CXXFLAGS) clean: rm -rf *.o OCI - session 1: From C to C++ (and some C++ features) 14 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Plan

1

Introduction

2

Reminder on C compiler

3

Reminder on memory managment

4

Some features

5

Exercices

OCI - session 1: From C to C++ (and some C++ features) 15 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Memory : stack vs heap The memory of each process is split in 3 big segment : Data (Code+Globals), Stack, Heap. Their place / size is constrained by the OS. The stack The stack stores temporary variables created by each function which pushes a temporary stack frame for its execution. The stack is a ”FILO” (first in, last out) data structure, that is managed and optimized by the CPU (fast read and write to stack variables). Another advantage of using the stack to store variables : no need to allocate memory by hand, memory is automatically freed Limits : the stack has strong size limits, stack variables only exist while the function that created them is running OCI - session 1: From C to C++ (and some C++ features) 16 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Memory : stack vs heap

The heap Heap is the dynamic memory (manually managed by you) Variables can be accessed globally and ”no” limit on memory size Memory may become fragmented and wasted (memory leaks) if not properly managed Try to gather mallocs and frees in specific parts of your code (initialization and stop phases)

OCI - session 1: From C to C++ (and some C++ features) 17 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Pointers

Pointers are variable storing a memory address : Pointer value = rank of a memory cell. int *p declares a pointer variable p which is a pointer to an integer value *p is then the pointed value, interpreted according to the pointer type the & operator retrieves the adress of something

OCI - session 1: From C to C++ (and some C++ features) 18 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Memory management in C

malloc and free to use the heap memory ”High level” API to deal with chunks in heap There is only 3 functions to know : request a new memory chunk, return a memory chunk, expend a memory chunk

#include void*malloc(int size) void free(void*p) void*realloc(void*p,int size)

OCI - session 1: From C to C++ (and some C++ features) 19 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Memory management in C++ New/Delete The new and delete keywords are used to allocate and free memory. They are ”object-aware” so you’d better use them instead of malloc and free (no more sizeof(struct toto)) Never cross the streams (new/free or malloc/delete) Delete does two things : it calls the destructor and it deallocates the memory.

int *a = new int; delete a; int *b = new int[5]; delete [] b; OCI - session 1: From C to C++ (and some C++ features) 20 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

Plan

1

Introduction

2

Reminder on C compiler

3

Reminder on memory managment

4

Some features

5

Exercices

OCI - session 1: From C to C++ (and some C++ features) 21 / 28

Introduction

Reminder on C compiler

Reminder on memory managment

Some features

Exercices

References A reference allows to declare an alias to another variable. As long as the aliased variable lives, you can use indifferently the variable or the alias. References are extremely useful when used with function arguments since it saves the cost of copying parameters into the stack when calling the function.

int x; int& foo = x; foo = 42; std::cout