일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- JUCE라이브러리
- go channel
- C++
- tour of go
- c++ heap
- C++ library
- JUCE 튜토리얼
- 연결리스트
- OS
- LOB
- JUCE library
- C++ gui
- C언어
- 리듬게임
- 코딩
- a tour of go
- 자료구조
- 공룡책
- BOJ
- C++ gui 라이브러리
- 백준
- 알고리즘
- Docker
- gui
- 프로그래밍
- 운영체제
- Nebula
- vim-go
- JUCE
- go
Archives
- Today
- Total
CafeM0ca
[C++] smart pointer 본문
반응형
smart pointer automatically managing memory a pointer . if out of scope range, It deleted.
There are three types pointer in std namespace.
- unique_ptr
- shared_ptr
- weak_ptr
First unique_ptr code.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include <iostream> #include <memory> int main(){ // unique ptr std::unique_ptr<char> ch = std::make_unique<char>('c'); auto a = std::make_unique<int>(1); std::cout << "ch: " << *ch << std::endl; std::cout << "a: " << *a << std::endl; decltype(a) ptr = a; // compile error std::cout << "a: " << *a << std::endl; return 0; } | cs |
error message
Second shared_ptr continue code
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 <iostream> #include <memory> int main(){ // unique ptr std::unique_ptr<char> ch = std::make_unique<char>('c'); auto a = std::make_unique<int>(1); std::cout << "ch: " << *ch << std::endl; std::cout << "a: " << *a << std::endl; /* decltype(a) ptr = a; // compile error std::cout << "a: " << *a << std::endl; */ std::shared_ptr<int> num = std::make_shared<int>(1); std::cout << "num: " << *num << std::endl; std::cout << "num reference count : " << num.use_count() << std::endl; std::shared_ptr<int> nptr = num; std::cout << "nptr: " << *nptr<< std::endl; std::cout << "num reference count : " << num.use_count() << std::endl; std::cout << "nptr reference count : " << nptr.use_count() << std::endl; { std::shared_ptr<int> nptr2 = num; std::cout << "scope in" << std::endl << std::endl;; std::cout << "nptr2: " << *nptr2 << std::endl; std::cout << "num reference count : " << num.use_count() << std::endl; std::cout << "nptr reference count : " << nptr.use_count() << std::endl; std::cout << "nptr2 reference count : " << nptr2.use_count() << std::endl; } // reference count down. automatically deleted. std::cout << "scope out" << std::endl << std::endl;; std::cout << "num reference count : " << num.use_count() << std::endl; std::cout << "nptr reference count : " << nptr.use_count() << std::endl; return 0; } | cs |
Result
I'll be update more
반응형
'Programming > C++' 카테고리의 다른 글
[C++] 클래스에서 default와 delete (0) | 2018.06.30 |
---|---|
[C++] this 포인터 (0) | 2018.06.30 |
[C++]std::initializer_list (0) | 2018.06.24 |
[딥러닝]역전파 (0) | 2018.06.10 |
[딥러닝] 인공신경망 (0) | 2018.06.10 |
Comments