Advanced course of C/C++

Slides:



Advertisements
Similar presentations
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Advertisements

Chapter 17 vector and Free Store John Keyser’s Modifications of Slides By Bjarne Stroustrup
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
1 1 Lecture 4 Structure – Array, Records and Alignment Memory- How to allocate memory to speed up operation Structure – Array, Records and Alignment Memory-
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Memory Layout C and Data Structures Baojian Hua
Pointers Applications
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers OVERVIEW.
SPL – Practical Session 2 Topics: – C++ Memory Management – Pointers.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Dynamic memory allocation and Pointers Lecture 4.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
CSCI Rational Purify 1 Rational Purify Overview Michel Izygon - Jim Helm.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
12/23/2015Engineering Problem Solving with C++, second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 9 An Introduction.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
POINTERS Introduction to Systems Programming - COMP 1002, 1402.
System Programming Practical Session 7 C++ Memory Handling.
1 Debugging (Part 2). “Programming in the Large” Steps Design & Implement Program & programming style (done) Common data structures and algorithms Modularity.
1 C Basics Monday, August 30, 2010 CS 241. Announcements MP1, a short machine problem, will be released today. Due: Tuesday, Sept. 7 th at 11:59pm via.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
CSE 333 – SECTION 2 Memory Management. Questions, Comments, Concerns Do you have any? Exercises going ok? Lectures make sense? Homework 1 – START EARLY!
HP-SEE Valgrind Usage Josip Jakić Scientific Computing Laboratory Institute of Physics Belgrade The HP-SEE initiative.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Memory Leaks and Valgrind
Object Lifetime and Pointers
Intro to Pointers in C CSSE 332 Operating Systems
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Stack and Heap Memory Stack resident variables include:
Debugging Memory Issues
Introduction to Programming
Valgrind Overview What is Valgrind?
Pointers and Memory Overview
Checking Memory Management
CSCI206 - Computer Organization & Programming
This pointer, Dynamic memory allocation, Constructors and Destructor
CSCI206 - Computer Organization & Programming
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
ניפוי שגיאות - Debugging
understanding memory usage by a c++ program
Chapter 15 Pointers, Dynamic Data, and Reference Types
Pointers, Dynamic Data, and Reference Types
Dynamic Memory Allocation
Memory Allocation CS 217.
Chapter 15 Pointers, Dynamic Data, and Reference Types
Given the code to the left:
Dynamic Memory Allocation (and Multi-Dimensional Arrays)
Dynamic Memory A whole heap of fun….
Arrays an array of 5 ints is filled with 3,2,4,1,7
C (and C++) Pointers April 4, 2019.
TUTORIAL 7 CS 137 F18 October 30th.
Dynamic Memory A whole heap of fun….
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
COP 3330 Object-oriented Programming in C++
Dynamic Memory.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Pointers and dynamic objects
Valgrind Overview What is Valgrind?
Makefiles, GDB, Valgrind
Pointers, Dynamic Data, and Reference Types
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Advanced course of C/C++ E.I. Alexandrov JINR LIT Dubna, 2017

Contents list Classes of computer memory Using computer memory in the program The difference between references and pointers Framework Valgrind for finding errors when using computer memory

Classes of computer memory Classes of memory Static Stack Dynamic(heap) Static variable Global variable Local variable new malloc

Using computer memory in the program. Static memory. Static variable Global variable Area of visibility All program File 1.cc Example 1.cc 2.cc static int series_num; void series(){ series_num = series_num + 23; } void main(){ series_num = 2; series(); int series_num; void main(){ series_num = 2; series(); } extern int series_num; void series(){ series_num = series_num + 23; }

Using computer memory in the program. Dynamic memory. malloc / free new / delete Returns Void* Fully typed pointer Languages С / C++ C++ On failure NULL Throws (never returns NULL) Size specified in bytes Calculated by compiler Overridable NO YES Use of (con-)/destructor NO YES

Using computer memory in the program int i=3, j; // global variables. Class of memory is static; void main(){ int a; // local memory; static float b[1000], c=2.3; // static memory; void* test = malloc(1000); // dynamic memory; ... } int f(){ int d; // local memory; static int m=2, k; // static memory; double * a = new double[1000]; // dynamic memory.

References and Pointers Reference to the memory address where the value is stored i_val void main(){ int i_val = 7; int* i_ptr = &i_val; // Display the value of the variable i_val cout << i_val << endl; // C1 cout << *i_ptr << endl; // C2 } Pointer to the memory address where the value is stored i_val

References and Pointers Main differences: The reference can not be undefined. The pointer can be undefined (not initialized); Int* i = NULL; Int& j=NULL; The reference can not be changed after initialization; Int a=1,b=2; Int* i=&a; i=&b; You can not declare an array of references: int **y = new int *[5]; There is arithmetic operation of pointers, but there is no arithmetic of references short * р = new short [5]; p++; // р is reference for second element of array of short (for memory it move 2 bytes) long * q = new long [5]; q++; // q is reference for second element of array of long (for memory it move 4 bytes)

Pointer void void* point; // Pointer to an undefined type int I; int *ptri; void* ? ? Size Structure of data point=0xb8000000; point++; point=&i; ptri=point; ptri=(int*)point; point=ptri; point=(void*)ptri;

Functions for working with memory blocks #include <string.h> Copy block of memory void *memcpy (void *destination, const void *source, size_t n); Fill block of memory (all bytes) void *memset (void *destination, int c, size_t n); Example: int position0[3]={10,10,10}; int firstValue; memcpy(&firstValue, position0, 1*sizeof(int));//copy 1 Int value memset(position0,1,3*sizeof(int)); // wrong because set //0x01010101 memset(position0,0,3*sizeof(int)); // correct

Size of common type Type Size bool, char, unsigned char, signed char, __int8 1 байт __int16, short, unsigned short, wchar_t, __wchar_t 2 байта float, __int32, int, unsigned int,  4 байта double, __int64, long1, unsigned long1  long double, long long, size_t1 8 байт __int128 16 байт 1Different type for 32 and 64 platforms.

Using pointers to work with memory blocks Example of copying a value from an array: copy_value.cc Compile command: g++ copy_value.cc Start command: sbatch script_memory Example of copying an arrays: copy_array.cc g++ copy_array.cc

Using references and pointers to pass parameters to functions The result of the variable x after the exit did not change void moveObject1(int x){ // Creates a duplicate of variable x x++; // To the duplicate of the variable x is added 1 } The result of the variable x was changed void moveObject2(int& x){ // The variable x is passed by reference x++; // To the variable x is added 1 The result of the pointer points to the new value of the variable void moveObject3(int* x){ // A pointer to x is passed int a=*x; // Creates a local variable with the value x *x=a++; // The pointer is assigned a new value //equal to x + 1

An example of using references and pointers to pass parameters to functions A point move in two-dimensional space with given boundaries. When the pointer will near the boundary it will reflected. Space dimensions, starting point, speed and number of steps are specified in the text of the program.: move_point.cc Compile command: g++ move_point.cc Start command: sbatch script_memory Output file: All coordinates are displayed on the screen. Each step is output from a new line.

Using references and pointers to pass arrays to functions Transferring an array to a function by value is not possible! Transfer of a dimensionless array void moveObject1(int[] x, int len){ … } Transfer an array of a given size void moveObject2(int x[10]){ Passing an array through a pointer void moveObject3(int* x , int len){

An example of using references and pointers to pass arrays to functions A point move in n-dimensional space with given boundaries. When the pointer will near the boundary it will reflected. Space dimensions, starting point, speed and number of steps are specified in the text of the program. : move_point2.cc Compile command: g++ move_point2.cc Start command: sbatch script_memory Output file: All coordinates are displayed on the screen. Each step is output from a new line.

Literature Стивен Прата (Stephen Prata), «Язык программирования С Лекции и упражнения», Киев 2000г. Bjarne Stroustrup, «The programming language C++»

Profiling of memory

Profiling of memory Profiling - the collection features the work program for the purpose of their further optimization. Type of profiling Gaze Manual Special tools

Profiling of memory Special tools for different languages Languages .NET JAVA Free Utils dotTrace Jconsole Valgrind (linux) Intel Parallel Inspector

Profiling of memory in c/c++ Main problem with memory for c++ Missing allocation Memory leak Invalid Memory Access Mismatched Allocation/Deallocation Uninitialized Memory Access Cross Stack Access Reading/writing to memory out of the bounds array Possible results Segmentation fault ***glibc detected*** Large size of memory

Profiling of memory in c/c++ Missing allocation char* pStr = (char*) malloc(20); free(pStr); Memory leaks char *pStr = (char*) malloc(512); return; Invalid Memory Access char *pStr = (char*) malloc(25); strcpy(pStr, .parallel programming.); Mismatched Allocation/Deallocation char *s = (char*) malloc(5); delete s;

Profiling of memory in c/c++ Uninitialized Memory Access char *pStr = (char*) malloc(512); char c = pStr[0]; void func() { int a; int b = a * 4; } Reading/writing to memory out of the bounds array Int a[10]; a[10]=0;

Profiling of memory: Valgrind Valgrind1 is a programming tool for memory debugging, memory leak detection, and profiling. Work as virtual machine so speed is down. Valgrind main tool is memcheck. The problems Memcheck can detect: Use of uninitialized memory; Reading/writing memory after it has been free'd; Reading/writing off the end of malloc’d blocks; Memory leaks. Problem of memcheck is bounds errors in the use of static or stack-allocated data. Use exp-sgcheck for finding overruns of stack and global arrays. 1http://valgrind.org/

Valgrind: other tools Massif, a heap profiler (has separate GUI); Helgrind and DRD, detect race conditions in multithreaded code Cachegrind, a cache profiler (has separate GUI); Callgrind, a callgraph analyzer(has separate GUI); exp-sgcheck/exp-ptrcheck,  an experimental tool to find stack and global array overrun errors which Memcheck cannot find; exp-dhat, dynamic heap analysis tool which analyzes how much memory is allocated and for how long as well as patterns of memory usage; exp-bbv, a performance simulator that extrapolates performance from a small sample set.

Valgrind : start Common view of start command: >valgrind [options] prog-and-args Options (main): --tool=<name> (default memcheck) --leak-check=no|summary|full (support only for memcheck) --show-reachable= no|yes (support only for memcheck) prog-and-args – command for start program with parameters Examples: >valgrind --leak-check=full --show-reachable=yes ./prog1 >valgrind --tool=exp-sgcheck ./a.out It runs 10-15 times slower.

Valgrind : start Compile command of program: >g++ -g v_copy_array.cc Add debug info Start command: >sbatch script_val

Valgrind : memcheck: result Segmentation fault: ==6993== Invalid read of size 4 ==6993== at 0x400C73: Utils::getCompositionArray(int*, int*, unsigned int) (utils.cc:25) ==6993== by 0x400B07: main (main.cc:36) ==6993== Address 0x4e7e408 is 0 bytes after a block of size 2,347,976 alloc'd ==6993== at 0x4A06A2E: malloc (vg_replace_malloc.c:270) ==6993== by 0x400BB8: Utils::getRandomArray(unsigned int, unsigned int, unsigned int) (utils.cc:11) ==6993== by 0x400A7C: main (main.cc:26)

Valgrind : memcheck: result (2) Memory leak: ==8405== HEAP SUMMARY: ==8405== in use at exit: 3,380,544 bytes in 3 blocks ==8405== total heap usage: 4 allocs, 1 frees, 6,685,268 bytes allocated ==8405== ==8405== 1,126,848 bytes in 1 blocks are definitely lost in loss record 1 of 3 ==8405== at 0x4A06A2E: malloc (vg_replace_malloc.c:270) ==8405== by 0x400BD8: Utils::getRandomArray(unsigned int, unsigned int, unsigned int) (utils.cc:11) ==8405== by 0x400AE9: main (main.cc:30) ==8405== LEAK SUMMARY: ==8405== definitely lost: 3,380,544 bytes in 3 blocks ==8405== indirectly lost: 0 bytes in 0 blocks ==8405== possibly lost: 0 bytes in 0 blocks ==8405== still reachable: 0 bytes in 0 blocks ==8405== suppressed: 0 bytes in 0 blocks