Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced course of C/C++

Similar presentations


Presentation on theme: "Advanced course of C/C++"— Presentation transcript:

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

2 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

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

4 Using computer memory in the program. Static memory.
Static variable Global variable Area of visibility All program File 1.cc Example cc 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; }

5 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

6 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.

7 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

8 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)

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

10 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 //0x memset(position0,0,3*sizeof(int)); // correct

11 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.

12 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

13 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

14 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.

15 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){

16 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.

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

18 Profiling of memory

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

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

21 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

22 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;

23 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;

24 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/

25 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.

26 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 times slower.

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

28 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)

29 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


Download ppt "Advanced course of C/C++"

Similar presentations


Ads by Google