C++ осень 2013 лекция 1

  • View
    677

  • Download
    2

Embed Size (px)

Transcript

  • 1. C++

2. . 1 1. C. 1. . C11. C 1. . . 2. C 2. C. . 2 3. . 2 2. ++. 3. 4. . . (RTTI) 3. 5. . . 4. 3 4. . 3 3. : STL, Boost 6. STL. C++11 7. Boost 5. STL Boost4 5. . 4 4. - . 8. - . , GoF 6. - 9. C++. . 7. 5 6. 1. . C11. C 1. C . C11. 2. . 3. . 4. . 5. C. 6. . . 7. 1. 6 7. , () , . 9 , 7 . : 2013 10 , 6 ; 2012 12 , 4 . 64 . : 36 . ; 28 . .7 8. ? (1 / 2) : UML- ; C / C++; ; ; STL, Boost; GoF; ; . 8 9. ? (2 / 2) : ; UML-; C++11; ; POSIX- ; Qt.9 10. : 1, 2, 3, 4, 6, 8 9 ( !). : 4 . 1 2 10 ; : ( !), : ; ( !). : (C, C++, Java); , . 10 11. (1 / 5) ., ., ., . . . , 2007. 366 . ., . C++. -, 2009. 800 . . ++. . , 2012. 264 . . . , 2006. 400 . ., . C. , 2012. 304 . . C++. , 2005. 480 .11 12. (2 / 5) ., . C++. . , . 1104 . ., ., . C++. . , 2007. 4- . 896 . . STL. . , 2005. 188 . . . -. , 2012. 896 . . C++. 35 . , 2012. 298 . . C++. 55 . , 2006. 300 .12 13. (3 / 5) ., ., . C++ STL. . , 2010. 432 . . C. . , 2013. 960 . . C++. . , 2012. 6- . 1248 .: . . Qt. . C++. -, 2011. 560 . . C++. , 2005. 272 . . C++. , 2008. 400 . ., . C++. , 2008. 224 . . C++. , 2011. 1056 . 13 14. (4 / 5) . . C++. , 2011. 1248 . . C++. , 2011. 1136 . . . . -, 2008. 432 . . C++: . , 2008. 624 . . C++. . , 2009. 480 . . C. , 2009. 704 . . C++. , 2007. 800 . . Qt 4.8. C++. , 2012. 894 . 14 15. (5 / 5) Abrahams, D., Gurtovoy, A. C++ Template Metaprogramming: Concepts, Tools, and Techniques from Boost and Beyond (Addison Wesley Professional, 2004). Demming, R., Duffy, D.J. Introduction to the Boost C++ Libraries; Volume I Foundations (Datasim Education BV, 2010). Demming, R., Duffy, D.J. Introduction to the Boost C++ Libraries; Volume II Advanced Libraries (Datasim Education BV, 2012). Drepper, U. What Every Programmer Should Know About Memory (2007). URL: http://people.redhat.com/drepper/cpumemory.pdf. King K. C Programming: A Modern Approach, 2nd ed. (W. W. Norton & Co., 2008). Meyers, S. CPU Caches and Why You Care. URL: http://aristeia.com/TalkNotes/PDXCodeCamp2010.pdf Musser, D.R., Saini, A. STL Tutorial and Reference Guide: C++ Programming with the Standard Template Library (Addison-Wesley, 1995). 15 16. Web- - Web- Boost: http://www.boost.org/. Web- Eclipse: http://www.eclipse.org/. C / C++: http://ru.cppreference.com/w/, http://en.cppreference.com/w/. C Programming: http://en.wikibooks.org/wiki/C_Programming. Google C++ Style Guide: http://googlestyleguide.googlecode.com/svn/trunk/cppguide.xml More C++ Idioms: http://en.wikibooks.org/wiki/More_C%2B%2B_Idioms. Schling, B. The Boost C++ Libraries: http://en.highscore.de/cpp/boost/.16 17. : http://tp.mail.ru/blog/cpp/ : ; ; ; .17 18. C 1970- . C - : : Linux, Windows NT, z/OS; : gdb, vi / vim, gedit; : MySQL, Oracle Database; Web-: Apache, nginx; Mail.Ru Group: ICQ, tarantool . *nix- . 18 19. , . : # (); . : #include ; #define ( #undef); #if, #ifdef, #ifndef, #else, #elif, #endif , ; #error ; #pragma , . 19 20. : #define #define SIZE 100 #define PRINT_X printf("X:t%7dn", &x) #define CUBE(N) (N) * (N) * (N) int a[SIZE]; // : int a[100];PRINT_X; // : printf("X:t%7dn", &x); printf("%dn", CUBE(SIZE)); // : // printf("%dn", 100 * 100 * 100);20 21. : #error // #ifdef ANSI_C_LIKE #define A 1103515245 #define C 12345#else #define A 22695477 #define C 1#endif // , C++ #ifndef __cplusplus #error A C++ compiler is required! #endif 21 22. : ? . , .22 23. C () , : ; , ; , . C : , (. ), , for, while, do while if; ; . 23 24. , , ( , ). , (), : ( !); .24 25. : // : int foo(double *d, int n); // : for(int i = 0; i < n; i++) { int bar; // ...} // : , static int count = 0; // : , double accuracy = 0.001; 25 26. : ; . , , , . static , . , . 26 27. (0, '0), . , . : charspacesize_t int_sz= 0x20;// = sizeof(int);// size_t int10_sz = 10 * int_sz;// 27 28. C , auto , register, , static, , , static28 29. : // int foo(unsigned u) { auto int bar = 42; // ... } // int get_total(register int n) { // ... for(register int i = 0; i < n; i++) // ... }29 30. register : , C (gcc 1987 .); ( x86/x86-64: AX, EBX, RCX ..), 1- 2- ; , ; , (, , ). register , . 30 31. : // : // int callee(int n) { static int counter = 0; // ! // } // : // static double epsilon = 0.001; int foo(double accuracy) { if(accuracy < epsilon) // ... }31 32. : // : // (" ") double time; // long int fib[100]; // extern char space; // // ( ) int main(void) { extern double time; extern long int fib[];// // ; // // ... }32 33. : ( ) ; static , . : int one(void); // // static int two(void); // extern int three(void); 33 34. sizeof size_t sizeof: ( ) : sizeof a sizeof(T); , , size_t, (. int32_t .). . sizeof(T) , .34 35. . ptrdiff_t T* C , . p, p2 T*, n ( ptrdiff_t). : p + n n + p , p n sizeof(T) (); p n , p n sizeof(T) (); p++ ++p, p-- --p p + 1 p 1, ; p p2 , ptrdiff_t. , p p2. 35 36. () T a[N] C : , ; ; , ; char c[N] , c[sizeof(c) 1] == 0; sizeof(a) ( !); sizeof(a[0]) . . 36 37. (): // int a[] = {1, 2, 3}; // int a[3] = {1, 2, 3}; // int b[5] = {1, 2, 3}; // : // int b[5] = {1, 2, 3, 0, 0}; // int c[7] = {1, [5] = 10, 20, [1] = 2}; // : // int c[7] = {1, 2, 0, 0, 0, 10, 20};37 38. () T a[N] . : 0- : a == &a[0]; : &a[i] == a + i a[i] == *(a + i); : int foo(double [], int); int foo(double *, int); , , : 0- N- ( a[N] ): int foo(double *, double *); 38 39. NULL NULL , , (0L 0), void* (. (void*)0). NULL - : if (p != NULL) // ... if (q == NULL) // ...39 40. : ; 0, (void*)0 NULL. : ; ; .40 41. - POSIX?int scanf(const char *restrict format, ... ); scanf stdin. - (EILSEQ) (EINVAL)int printf(const char *restrict format, ...); printf stdout. EILSEQ, EINVAL .41 42. (1 / 2) POSIX? (ENOMEM) (ENOMEM)void *malloc(size_t size); malloc size , void *calloc(size_t nelem, size_t elsize); calloc nelem elsize 42 43. (2 / 2) POSIX? (ENOMEM)void *realloc(void *ptr, size_t size); realloc , ptr, size. ptr , malloc. size == 0, void free(void *ptr); free , ptr, . ptr 43 44. , . GCC- aligned (1 / 2) , - . , , - 1- (L1d). , . : ; , , 0; C ., GCC aligned. 44 45. , . GCC- aligned (2 / 2) , . , , . , .45 46. , . aligned: // , // qwd 64 uint64_t qwd __attribute((aligned(64))); // , // al128int_t ( int)// 128 typedefint __attribute((aligned(128))) al128int_t;al128int_t aln;46 47. , . posix_memalign posix_memalign: POSIX 1003.1d; int posix_memalign(void **memptr, size_t alignment, size_t size); size , alignment, memptr; free(). alignment: sizeof(void*); 2. (EINVAL, ENOMEM): alignment sizeof(void*) 2; . 47 48. posix_memalign: (1 / 2)int b[7] = {1, [5] = 10, 20, [1] = 2}; // - int *p = NULL, errflag;// - // posix_memalign// - 1- // (L1d); : 64 long l1dcls = sysconf(_SC_LEVEL1_DCACHE_LINESIZE); // , sysconf() if (l1dcls == -1) // sysconf() , // l1dcls = sizeof(void*); 48 49. posix_memalign: (2 / 2) // L1d errflag = posix_memalign((void**)&p, l1dcls, sizeof b); if(!errflag)// posix_memalign 0 {printf("nL1d cache line size is %ldn", l1dcls); printf("p and &p are %p and %pn", p, &p); p = memcpy(p, b, sizeof(b));// ... free(p); } else printf("posix_memalign error: %dn", errflag);49 50. 1 1 2 . : ; .50 51.