본문 바로가기

전체 글

(158)
c++ heap 메모리 생성, 해제, unique_ptr, shared_ptr unique ptr은 exclusive ownership을 보장함. c++ heap 메모리 생성, 해제 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 #include #include #include class Cat { public: explicit Cat(int age) :mAge{ age } { std::cout
c++ const, explicit const는 붙일 수 있다면 무조건 붙이자. explicit도 붙일 수 있다면 무조건 붙이자(형 강제) friend는 oop를 해치므로 웬만하면 쓰지 말자. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 #include #include // Frined는 웬만하면 쓰지 말자 // Const와 explicit은 붙일 수 있으면 무조건 붙이자 class Cat { public: // 무조건 explicit하게 name과 age를 넣어줘야 함 explicit Cat(std::str..
c++ class function overloading, 연산자(operator) 임의 지정 연산자 operator, function overloading은 매우 중요하다. 특히 operator는 stl과 결합하여 아주 강력한 코딩을 만들 수 있다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 #inclu..
C++ class copy move constructor, instructor class 내부에서 포인터로 heap을 조작하고자 하는 경우에는 개발자가 직접 copy constructor, move constructor를 활용해서 직접 구현해야 함. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 #inclu..
static function, static variable static variable은 object가 생성되었을때 object들이 stack 공간에 저장되는 반면에 static은 static 이라는 별도의 메모리에 저장 된다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 #include #include class Cat // 메모리를 32byte의 배수로 만든다 { public: void speak() { count++; static int static_count = 0; // Static variable in a function... // 더 안전함 static_coun..
c++ object memory alignment 1l cache memory 등을 고려하여 코드를 짜야 한다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 #include #include class alignas(32) Cat // 메모리를 32byte의 배수로 만든다 { public: void speak(); private: // 위치에 따라 class 크기가 다름 // 가장 큰 변수의 배수에서 메모리 할당이 끝나야 한다 // 따라서 24byte 나올 수 있음 // char를 늘려도 더 올라감 char c1; int i4a; double d8; int i4b; }; Cat staticCat; ..
c++ copy elison, Retrun value optimization 보통은 l value reference로 인자를 보내주면 충분하다 그리고 함수에 인자는 pass by value object의 인자는 pass by reference가 좋다. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 // RVO // COPY ELISON #include #include std::string getString() { // RVO 개입 std::string s = "nocodeprogram"; return s; } std::string getString(std::string a, bool b) { if (b) { a = "nocodeprogram"; return a; } return..
c++ lvalue rvalue zero copy 공부 기록을 남긴다. lvalue rvalue zero copy 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 #include #include #include #include class Cat { public: void s..