Reliable C++ development - session 1: From C to C++ (and some C++ features)

Similar documents
CS Basics 15) Compiling a C prog.

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

CS201 - Introduction to Programming Glossary By

2 Compiling a C program

Short Notes of CS201

CSE 303: Concepts and Tools for Software Development

#include <stdio.h> int main() { printf ("hello class\n"); return 0; }

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CS2141 Software Development using C/C++ Compiling a C++ Program

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CS201 Some Important Definitions

CS 376b Computer Vision

assembler Machine Code Object Files linker Executable File

CPSC 427: Object-Oriented Programming

INTERMEDIATE SOFTWARE DESIGN SPRING 2011 ACCESS SPECIFIER: SOURCE FILE

CAAM 420 Fall 2012 Lecture 15. Roman Schutski

COMP322 - Introduction to C++ Lecture 01 - Introduction

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Scientific Computing

CSE 333. Lecture 9 - intro to C++ Hal Perkins Department of Computer Science & Engineering University of Washington

Basic C Programming. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

PRINCIPLES OF OPERATING SYSTEMS

Intermediate Programming, Spring 2017*

Separate Compilation of Multi-File Programs

Topic 6: A Quick Intro To C

Fast Introduction to Object Oriented Programming and C++

CSCI-243 Exam 2 Review February 22, 2015 Presented by the RIT Computer Science Community

Come and join us at WebLyceum

Discussion Week 1. TA: Kyle Dewey. Sunday, September 25, 11

Names, Scope, and Bindings

Arizona s First University. More ways to show off--controlling your Creation: IP and OO ECE 373

High Performance Computing and Programming, Lecture 3

Lab 1: First Steps in C++ - Eclipse

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CSE 333 Midterm Exam 7/27/15 Sample Solution

CSE au Midterm Exam Nov. 2, 2018 Sample Solution

Intermediate Programming, Spring 2017*

CSE 333 Lecture 9 - intro to C++

Topic 6: A Quick Intro To C. Reading. "goto Considered Harmful" History

CS240: Programming in C. Lecture 2: Overview

a data type is Types

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

CS

CS240: Programming in C

CS 11 C++ track: lecture 1

Names, Scope, and Bindings

Memory management COSC346

Object-Oriented Programming for Scientific Computing

GEA 2017, Week 4. February 21, 2017

In Java we have the keyword null, which is the value of an uninitialized reference type

Run Time Environment

EL2310 Scientific Programming

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

Pointers, Dynamic Data, and Reference Types

Compiler Theory. (GCC the GNU Compiler Collection) Sandro Spina 2009

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

the gamedesigninitiative at cornell university Lecture 6 C++: Basics

CE221 Programming in C++ Part 1 Introduction

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

Week 9 Part 1. Kyle Dewey. Tuesday, August 28, 12

For Teacher's Use Only Q No Total Q No Q No

UEE1303(1070) S 12 Object-Oriented Programming in C++

Heap Management portion of the store lives indefinitely until the program explicitly deletes it C++ and Java new Such objects are stored on a heap

Chapter 2. Procedural Programming

C Introduction. Comparison w/ Java, Memory Model, and Pointers

Names, Scope, and Bindings

C++ PROGRAMMING LANGUAGE: DYNAMIC MEMORY ALLOCATION AND EXCEPTION IN C++. CAAM 519, CHAPTER 15

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

High-performance computing and programming I. Introduction to C programming in *NIX

Motivation was to facilitate development of systems software, especially OS development.

StackVsHeap SPL/2010 SPL/20

Run-time Environments - 3

Computer Systems Lecture 9

Distributed Real-Time Control Systems. Lecture 17 C++ Programming Intro to C++ Objects and Classes

Object-oriented Programming in C++

ANITA S SUPER AWESOME RECITATION SLIDES

Lectures 5-6: Introduction to C

Tutorial-2a: First steps with C++ programming

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

C++ Mini-Course. Part 1: Mechanics Part 2: Basics Part 3: References Part 4: Const Part 5: Inheritance Part 6: Libraries Part 7: Conclusion. C Rulez!

FROM C TO C++ CRASH COURSE FOR C PROGRAMMERS -PART I- APPLAB

CSE 333 Midterm Exam 5/9/14 Sample Solution

PHY4321 Summary Notes

COMP6771 Advanced C++ Programming

CS201 Latest Solved MCQs

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

CS3157: Advanced Programming. Outline

CS3215. Outline: 1. Introduction 2. C++ language features 3. C++ program organization

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

COMP322 - Introduction to C++

Reminder: compiling & linking

CPSC 427: Object-Oriented Programming

C++ Programming for Non-C Programmers. Supplement

EL6483: Brief Overview of C Programming Language

TDDE18 & 726G77. Functions

Transcription:

Reliable C++ development - session 1: From C to C++ (and some C++ features) Thibault CHOLEZ - thibault.cholez@loria.fr TELECOM Nancy - Université de Lorraine LORIA - INRIA Nancy Grand-Est From Nicolas Rougier s C++ crash course 22/09/2014

Plan 1 Introduction 2 Reminder on C compiler 3 Reminder on memory managment 4 Some features 5 Exercices 2 / 27

Plan 1 Introduction 2 Reminder on C compiler 3 Reminder on memory managment 4 Some features 5 Exercices 3 / 27

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 Five sessions of two hours (within the next three weeks) For each session : 30 min talk on C++ concepts, then short partical exercices on computer 4 / 27

First words Program From C to C++ : reminder C++ features and object programming Functionnalities for reliable programming Optimizations and performance Exam : few theoritical questions + pratical exercices on computer Environment Basic Linux OS and tools : g++, console, text editor No Integrated Development Environment (hides complexity) 5 / 27

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)! 6 / 27

Pros Cons Why not JAVA for embedded software? 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) 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 CPU is probably register-based (RISC) (that s why Dalvik was created) Unpredictability of JVM is an issue for real time system : you don t want the garbage collector to work during during a critical time 7 / 27

C / C++ Pros Cons 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 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 8 / 27

Plan 1 Introduction 2 Reminder on C compiler 3 Reminder on memory managment 4 Some features 5 Exercices 9 / 27

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 <header file> 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 10 / 27

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 11 / 27

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 12 / 27

CXX=g++ CXXFLAGS=-Wall LDFLAGS= EXEC=hello all: $(EXEC) Example of Makefile 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 13 / 27

Plan 1 Introduction 2 Reminder on C compiler 3 Reminder on memory managment 4 Some features 5 Exercices 14 / 27

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 automaticallt freed Limits : the stack has strong size limits, stack variables only exist while the function that created them is running 15 / 27

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) 16 / 27

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 17 / 27

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 <stdlib.h> void*malloc(int size) void free(void*p) void*realloc(void*p,int size) 18 / 27

New/Delete Memory management in C++ 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; 19 / 27

Plan 1 Introduction 2 Reminder on C compiler 3 Reminder on memory managment 4 Some features 5 Exercices 20 / 27

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 << x << std::endl; 21 / 27

Namespaces Namespace allows to group classes, functions and variable under a common scope name that can be referenced elsewhere. There exists some standard namespace in the standard template library such as std. The example below should give values 3 and 5 : namespace first { int var = 5; } namespace second { int var = 3; } cout << first::var << endl << second::var << endl; 22 / 27

Default parameters You can specify default values for function parameters. When the function is called with fewer parameters, default values are used. Watch out, it can create ambiguity. The code below should obtain values 4, 5 and 6. float foo( float a=0, float b=1, float c=2 ) {return a+b+c;} cout << foo(1) << endl << foo(1,2) << endl << foo(1,2,3) << endl; 23 / 27

Overloading Function overloading refers to the possibility of creating multiple functions with the same name as long as they have different parameters (type and/or number). It is not legal to overload a function based on the return type! The code below should obtain values 4, 5 and 6. float add( float a, float b ) {return a+b;} int add( int a, int b ) {return a+b;} 24 / 27

Const and inline functions Defines and macros are bad if not used properly as illustrated below For constants, prefer the const notation (it adds type to the constant) #define SQUARE(x) x*x int result = SQUARE(3+3); const int two = 2; int inline square(int x) {return x*x;} 25 / 27

Plan 1 Introduction 2 Reminder on C compiler 3 Reminder on memory managment 4 Some features 5 Exercices 26 / 27

Up to you C++ Hello World #include<iostream> int main() { std::cout << "Hello, new world!\n"; } Compilation and execution g++ hello.cpp -o hello./hello 27 / 27