Lesson of 17 February 2011¶
Union and big/little endian
File biglittle.cc
1// to compile on unix/OSX use
2// g++ -I. biglittle.cc
3//
4
5// include header for C++ I/O
6// search stdio.h in standard C/C++ include directory e.g. /usr/include
7#include <iostream> // for the definition of cin, cout, ...
8#include <iomanip> // for manipulator setw, setprecision ...
9
10// include header for STL vector and algorithm
11#include <vector>
12#include <algorithm>
13
14#include "pstdint.h" // for uint64_t
15
16#include <cstdlib> // near equivalent to <stdlib.h> for exit, rand, ...
17#include <cmath> // near equivalent to <math.h> for sin, cos, log, ..
18
19using namespace std ;
20
21int
22main() {
23
24 // declare a struct called pluto
25 struct pluto {
26 float x, y ;
27 } ;
28
29 // declare a struct called pluto
30 typedef struct { float x, y ; } typeStructWithXYField ;
31
32 // declare a struct called pippo
33 struct pippo {
34 double a ;
35 double b ;
36 double c ;
37 int d ;
38 struct { // no name struct
39 float x, y ;
40 } ABC ;
41 } ;
42
43 union checkBigEndian {
44 uint64_t a_number ;
45 uint8_t a_list[8] ;
46 } ;
47
48 // define X of type struct pippo
49 struct pippo X ;
50 typeStructWithXYField Z ;
51
52 X . ABC . x = 1 ;
53
54 union checkBigEndian check ;
55
56 check . a_number = 0x0102030405060708 ;
57 cout << hex
58 << "a[0] = " << (int)check . a_list[0] << '\n'
59 << "a[1] = " << (int)check . a_list[1] << '\n'
60 << "a[2] = " << (int)check . a_list[2] << '\n'
61 << "a[3] = " << (int)check . a_list[3] << '\n'
62 << "a[4] = " << (int)check . a_list[4] << '\n'
63 << "a[5] = " << (int)check . a_list[5] << '\n'
64 << "a[6] = " << (int)check . a_list[6] << '\n'
65 << "a[7] = " << (int)check . a_list[7] << '\n' ;
66
67 uint64_t a_value = 0x0102030405060708 ;
68 //uint8_t * a_pointer = (uint8_t*)(&a_value) ;
69 uint8_t * a_pointer = reinterpret_cast<uint8_t*>(&a_value) ;
70
71 cout << hex
72 << "a[0] = " << (int)a_pointer[0] << '\n'
73 << "a[1] = " << (int)a_pointer[1] << '\n'
74 << "a[2] = " << (int)a_pointer[2] << '\n'
75 << "a[3] = " << (int)a_pointer[3] << '\n'
76 << "a[4] = " << (int)a_pointer[4] << '\n'
77 << "a[5] = " << (int)a_pointer[5] << '\n'
78 << "a[6] = " << (int)a_pointer[6] << '\n'
79 << "a[7] = " << (int)a_pointer[7] << '\n' ;
80
81
82 return 0 ;
83}
Dynamic allocation on the stack
File Dallocation.cc
1// to compile on unix/OSX use
2// g++ -I. Dallocation.cc
3//
4
5// include header for C++ I/O
6// search stdio.h in standard C/C++ include directory e.g. /usr/include
7#include <iostream> // for the definition of cin, cout, ...
8#include <iomanip> // for manipulator setw, setprecision ...
9
10#include <cstdlib> // near equivalent to <stdlib.h> for exit, rand, ...
11#include <cmath> // near equivalent to <math.h> for sin, cos, log, ..
12
13using namespace std ;
14
15#ifdef USE_ALLOCA
16 // alloca use stack for allocation
17 #include <alloca.h>
18#endif
19
20int
21main( int argc, char const * argv []) {
22
23 int dim ;
24 sscanf( argv[1], "%d", &dim ) ;
25
26 {
27 #ifdef USE_ALLOCA
28 unsigned * A = (unsigned*)alloca(dim*sizeof(unsigned)) ;
29 #else
30 unsigned A[dim] ;
31 #endif
32 cout << "A size = " << sizeof( A ) << endl ;
33 for ( unsigned i = 0 ; i < sizeof(A)/sizeof(A[0]) ; ++i )
34 A[i] = i ;
35 }
36
37 {
38 #ifdef USE_ALLOCA
39 unsigned * B = (unsigned*)alloca(dim*sizeof(unsigned)) ;
40 #else
41 unsigned B[dim] ;
42 #endif
43 cout << "B size = " << sizeof( B ) << endl ;
44 for ( unsigned i = 0 ; i < sizeof(B)/sizeof(B[0]) ; ++i )
45 B[i] = i ;
46 {
47 #ifdef USE_ALLOCA
48 unsigned * C = (unsigned*)alloca(dim*sizeof(unsigned)) ;
49 #else
50 unsigned C[dim] ;
51 #endif
52 cout << "C size = " << sizeof( C ) << endl ;
53 for ( unsigned i = 0 ; i < sizeof(C)/sizeof(C[0]) ; ++i )
54 C[i] = i ;
55 }
56 }
57
58 return 0 ;
59}
The C way of dynamic allocation
File Dallocation2.cc
1// to compile on unix/OSX use
2// g++ -I. Dallocation.cc
3//
4
5// include header for C++ I/O
6// search stdio.h in standard C/C++ include directory e.g. /usr/include
7#include <iostream> // for the definition of cin, cout, ...
8#include <iomanip> // for manipulator setw, setprecision ...
9
10#include <cstdlib> // near equivalent to <stdlib.h> for exit, rand, ...
11#include <cmath> // near equivalent to <math.h> for sin, cos, log, ..
12
13
14using namespace std ;
15
16// use dynamic allocation of C
17
18int
19main( int argc, char const * argv []) {
20
21 int dim ;
22 sscanf( argv[1], "%d", &dim ) ;
23
24 {
25 unsigned * A = (unsigned*)malloc( dim*sizeof(unsigned) ) ;
26 if ( A == NULL ) {
27 cerr << "Memory allocation failed\n" ;
28 exit(0) ;
29 }
30
31 cout << "A size = " << dim*sizeof(unsigned) << endl ;
32 for ( unsigned i = 0 ; i < dim ; ++i )
33 A[i] = i ;
34
35 free( A ) ; A = NULL ; // free used memory
36 free( A ) ; // free used memory
37 }
38
39 {
40 unsigned * B = (unsigned*)malloc( dim*sizeof(unsigned) ) ;
41 if ( B == NULL ) {
42 cerr << "Memory allocation failed\n" ;
43 exit(0) ;
44 }
45 cout << "B size = " << dim*sizeof(unsigned) << endl ;
46 for ( unsigned i = 0 ; i < dim ; ++i )
47 B[i] = i ;
48 free( B ) ; // free used memory
49 {
50 unsigned * C = (unsigned*)malloc( dim*sizeof(unsigned) ) ;
51 if ( C == NULL ) {
52 cerr << "Memory allocation failed\n" ;
53 exit(0) ;
54 }
55 cout << "C size = " << dim*sizeof(unsigned) << endl ;
56 for ( unsigned i = 0 ; i < dim ; ++i )
57 C[i] = i ;
58 }
59 }
60
61 return 0 ;
62}
The C++ way of dynamic allocation
File Dallocation3.cc
1// to compile on unix/OSX use
2// g++ -I. Dallocation.cc
3//
4
5// include header for C++ I/O
6// search stdio.h in standard C/C++ include directory e.g. /usr/include
7#include <iostream> // for the definition of cin, cout, ...
8#include <iomanip> // for manipulator setw, setprecision ...
9
10#include <cstdlib> // near equivalent to <stdlib.h> for exit, rand, ...
11#include <cmath> // near equivalent to <math.h> for sin, cos, log, ..
12
13
14using namespace std ;
15
16// use dynamic allocation of C++
17
18int
19main( int argc, char const * argv []) {
20
21 if ( argc != 2 ) {
22 cerr << " expect an argument!\n" ;
23 exit(1) ;
24 }
25
26 int dim ;
27 sscanf( argv[1], "%d", &dim ) ;
28
29 {
30 unsigned * A = new unsigned[ dim ] ;
31
32 cout << "A size = " << dim*sizeof(unsigned) << endl ;
33 for ( unsigned i = 0 ; i < dim ; ++i )
34 A[i] = i ;
35
36 delete [] A ;
37 }
38
39 {
40 unsigned * B = new unsigned[ dim ] ;
41
42 cout << "B size = " << dim*sizeof(unsigned) << endl ;
43 for ( unsigned i = 0 ; i < dim ; ++i )
44 B[i] = i ;
45 delete [] B ; // free used memory
46 {
47 unsigned * C = new unsigned[ dim ] ;
48 cout << "C size = " << dim*sizeof(unsigned) << endl ;
49 for ( unsigned i = 0 ; i < dim ; ++i )
50 C[i] = i ;
51 delete [] C ; // free used memory
52 }
53 }
54 return 0 ;
55}