๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด/C++ ์‘์šฉ

[STL] ์Šค๋งˆํŠธํฌ์ธํ„ฐ(std::unique_ptr, std::shared_ptr, std::weak_ptr)

by ์„œ์•„๋ž‘๐Ÿ˜ 2023. 8. 5.

 

 

 

์Šค๋งˆํŠธํฌ์ธํ„ฐ

์Šค๋งˆํŠธํฌ์ธํ„ฐ๋Š” C++์— ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ ์ค‘์— ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. C++์˜ ์น˜๋ช…์ ์ธ ๋‹จ์  ์ค‘ ํ•˜๋‚˜๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋ฅผ ์ง์ ‘ํ•˜๋Š” ๋ฐ์— ์žˆ์–ด์„œ ๋ฆฌ์Šคํฌ๋ฅผ ๋– ์•ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๋ฉ”๋ชจ๋ฆฌ ๊ด€๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ํ•ด์ฃผ๋Š” ์Šค๋งˆํŠธํฌ์ธํ„ฐ๋กœ ์ธํ•ด์„œ ๋ชจ๋˜ C++์ด ํ•œ์ธต ํ’๋ถ€ํ•ด์กŒ์Šต๋‹ˆ๋‹ค. ์Šค๋งˆํŠธ ํฌ์ธํ„ฐ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜์™€ ๊ด€๋ จ๋œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋Š”๋ฐ ํฐ ๋„์›€์„ ์ฃผ๋ฉฐ, ์ž์› ๊ด€๋ฆฌ๋ฅผ ๋ณด๋‹ค ์•ˆ์ „ํ•˜๊ฒŒ ํ•˜๊ณ  ์ฝ”๋“œ์˜ ๊ฐ€๋…์„ฑ๊ณผ ์œ ์ง€๋ณด์ˆ˜์„ฑ์„ ํ–ฅ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.

 

 

std::unique_ptr

std::unique_ptr์€ ๋…์ ์ ์œผ๋กœ ์†Œ์œ ํ•˜๋Š” ํฌ์ธํ„ฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ๊ฐ์ฒด์— ๋Œ€ํ•ด ๋‹จ์ผ ์†Œ์œ ๊ถŒ์„ ๊ฐ–์Šต๋‹ˆ๋‹ค. ์ฆ‰, ์˜ค์ง ํ•˜๋‚˜์˜ std::unique_ptr๋งŒ์ด ํŠน์ • ๊ฐ์ฒด๋ฅผ ์†Œ์œ ํ•˜๊ณ , ์ดํ›„์—๋Š” ํ•ด๋‹น ๊ฐ์ฒด์˜ ์†Œ์œ ๊ถŒ์„ ๋‹ค๋ฅธ std::unique_ptr๋กœ ์ด์ „ํ•˜๊ฑฐ๋‚˜, ์†Œ์œ ๊ถŒ์„ ํฌ๊ธฐํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

#include <iostream>
#include <memory>

int main() {
    // std::unique_ptr ์ƒ์„ฑ
    std::unique_ptr<int> uniquePtr = std::make_unique<int>(42);

    // get(): ์›๋ณธ ํฌ์ธํ„ฐ์— ๋Œ€ํ•œ raw pointer๋ฅผ ๋ฐ˜ํ™˜
    int* rawPtr = uniquePtr.get();
    std::cout << "Raw Pointer: " << rawPtr << std::endl;

    // reset(): ํฌ์ธํ„ฐ๋ฅผ ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ์žฌํ• ๋‹นํ•˜๊ฑฐ๋‚˜, nullptr๋กœ ์ดˆ๊ธฐํ™”
    uniquePtr.reset(new int(99));
    std::cout << "Reset value: " << *uniquePtr << std::endl;

    // swap(): ๋‘ unique_ptr์˜ ํฌ์ธํ„ฐ๋ฅผ ๊ตํ™˜
    std::unique_ptr<int> anotherUniquePtr = std::make_unique<int>(77);
    std::cout << "Before swap - uniquePtr: " << *uniquePtr << ", anotherUniquePtr: " << *anotherUniquePtr << std::endl;
    uniquePtr.swap(anotherUniquePtr);
    std::cout << "After swap - uniquePtr: " << *uniquePtr << ", anotherUniquePtr: " << *anotherUniquePtr << std::endl;

    // release(): ํฌ์ธํ„ฐ ์†Œ์œ ๊ถŒ์„ ํฌ๊ธฐํ•˜๊ณ  raw pointer๋ฅผ ๋ฐ˜ํ™˜
    int* releasedPtr = uniquePtr.release();
    if (!uniquePtr) {
        std::cout << "uniquePtr is empty after release." << std::endl;
    }
    std::cout << "Released Pointer: " << releasedPtr << std::endl;
    
    // unique_ptr ์†Œ๋ฉธ ์‹œ์—๋Š” ์ž๋™์œผ๋กœ ๋™์  ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ•ด์ œ๋จ
    // releasedPtr๋Š” ์ด์ œ raw pointer์ด๋ฏ€๋กœ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ์ง์ ‘ ํ•ด์ œํ•ด์•ผ ํ•จ
    delete releasedPtr;

    return 0;
}


/*
์ถœ๋ ฅ๊ฒฐ๊ณผ: 
Raw Pointer: 0x...
Reset value: 99
Before swap - uniquePtr: 99, anotherUniquePtr: 77
After swap - uniquePtr: 77, anotherUniquePtr: 99
uniquePtr is empty after release.
Released Pointer: 0x...

*/

 

 

 

std::shared_ptr

std::shared_ptr์€ C++ ์Šค๋งˆํŠธ ํฌ์ธํ„ฐ ์ค‘ ํ•˜๋‚˜๋กœ, ์—ฌ๋Ÿฌ ๊ฐœ์˜ std::shared_ptr๋“ค์ด ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋ฅผ ๊ณต์œ  ์†Œ์œ ํ•˜๋Š” ํ˜•ํƒœ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ std::shared_ptr๋“ค์€ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ ์นด์šดํŠธ๋ฅผ ์œ ์ง€ํ•˜๊ณ , ๊ฐ์ฒด์˜ ์ฐธ์กฐ ์นด์šดํŠธ๊ฐ€ 0์ด ๋˜๋ฉด ์ž๋™์œผ๋กœ ๊ฐ์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ํ•ด์ œ๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ณต์œ  ์†Œ์œ  ๋ฐฉ์‹์œผ๋กœ ์ธํ•ด ๊ฐ์ฒด์˜ ์ˆ˜๋ช…์„ ํšจ๊ณผ์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


std::shared_ptr์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์ฃผ์š” ๋ฉ”์„œ๋“œ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

std::shared_ptr ์ƒ์„ฑ ๋ฐ ์ดˆ๊ธฐํ™”

std::shared_ptr<T>: ๊ธฐ๋ณธ ์ƒ์„ฑ์ž๋กœ ์ดˆ๊ธฐํ™”๋œ ๋นˆ std::shared_ptr ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
std::shared_ptr<T>(args...): args...๋กœ ์ „๋‹ฌ๋œ ์ธ์ž๋“ค๋กœ T ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” std::shared_ptr ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
std::make_shared<T>(args...): args...๋กœ ์ „๋‹ฌ๋œ ์ธ์ž๋“ค๋กœ T ํƒ€์ž…์˜ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” std::shared_ptr ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. std::make_shared๋Š” ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น๊ณผ ๊ฐ์ฒด ์ดˆ๊ธฐํ™”๋ฅผ ํ•˜๋‚˜์˜ ๋‹จ์ผ ์—ฐ์‚ฐ์œผ๋กœ ์ˆ˜ํ–‰ํ•˜์—ฌ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์†Œ์œ ๊ถŒ ๋ฐ ์ฐธ์กฐ ์นด์šดํŠธ

use_count(): ํ˜„์žฌ ๊ฐ์ฒด์˜ ์ฐธ์กฐ ์นด์šดํŠธ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
unique(): ์ฐธ์กฐ ์นด์šดํŠธ๊ฐ€ 1์ด๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
get(): ์›๋ณธ ํฌ์ธํ„ฐ์— ๋Œ€ํ•œ raw pointer๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์†Œ์œ ๊ถŒ ์ด์ „

reset(): std::shared_ptr์˜ ์†Œ์œ ๊ถŒ์„ ํฌ๊ธฐํ•˜๊ณ , ์ฐธ์กฐํ•˜๋˜ ๊ฐ์ฒด์˜ ์ฐธ์กฐ ์นด์šดํŠธ๋ฅผ ๊ฐ์†Œ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๋กœ ์ธํ•ด ๊ฐ์ฒด์˜ ์ฐธ์กฐ ์นด์šดํŠธ๊ฐ€ 0์ด ๋˜๋ฉด ์ž๋™์œผ๋กœ ๊ฐ์ฒด๊ฐ€ ์‚ญ์ œ๋ฉ๋‹ˆ๋‹ค.
reset(T* ptr): std::shared_ptr์˜ ์†Œ์œ ๊ถŒ์„ ์ƒˆ๋กœ์šด ํฌ์ธํ„ฐ ptr๋กœ ์ด์ „ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฏธ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ๊ฐ์ฒด์˜ ์ฐธ์กฐ ์นด์šดํŠธ๊ฐ€ ๊ฐ์†Œํ•˜๊ณ , ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๊ณต์œ  ๋ฐ ๋น„๊ณต์œ  std::shared_ptr์˜ ์ƒ์„ฑ ๋ฐ ๋น„๊ต

std::shared_ptr<T>(const std::shared_ptr<T>& r): r์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด๋ฅผ ๊ณต์œ  ์†Œ์œ ํ•˜๋Š” std::shared_ptr์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
std::shared_ptr<T>(std::shared_ptr<T>&& r) noexcept: r์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด๋ฅผ ๊ณต์œ  ์†Œ์œ ํ•˜๋Š” std::shared_ptr์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด๋™ ์ƒ์„ฑ์ž๋กœ, noexcept ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.
std::shared_ptr<T>(std::weak_ptr<T> r): r์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด๋ฅผ ๊ณต์œ  ์†Œ์œ ํ•˜๋Š” std::shared_ptr์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ด ๋•Œ, r์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ์ฒด๊ฐ€ ์œ ํšจํ•˜์ง€ ์•Š์œผ๋ฉด ๋นˆ std::shared_ptr์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.
operator==, operator!=: std::shared_ptr ๊ฐ์ฒด๋“ค์„ ๋น„๊ตํ•ฉ๋‹ˆ๋‹ค.

๋‹ค๋ฅธ ์Šค๋งˆํŠธ ํฌ์ธํ„ฐ์™€์˜ ์ƒํ˜ธ์ž‘์šฉ

std::shared_ptr์€ std::weak_ptr๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜์–ด ๋ฃจํ”„๋ฅผ ๋ฐฉ์ง€ํ•˜๊ณ  dangling pointer๋ฅผ ์˜ˆ๋ฐฉํ•˜๋Š” ๋ฐ์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.
std::shared_ptr์€ std::unique_ptr๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

#include <iostream>
#include <memory>

int main() {
    std::shared_ptr<int> sharedPtr1 = std::make_shared<int>(42);
    std::cout << "sharedPtr1 use_count: " << sharedPtr1.use_count() << std::endl; // 1

    std::shared_ptr<int> sharedPtr2 = sharedPtr1;
    std::cout << "sharedPtr1 use_count: " << sharedPtr1.use_count() << std::endl; // 2
    std::cout << "sharedPtr2 use_count: " << sharedPtr2.use_count() << std::endl; // 2

    sharedPtr1.reset();
    std::cout << "sharedPtr1 use_count: " << sharedPtr1.use_count() << std::endl; // 0 (sharedPtr1์€ ๋” ์ด์ƒ ๊ฐ์ฒด๋ฅผ ์†Œ์œ ํ•˜์ง€ ์•Š์Œ)
    std::cout << "sharedPtr2 use_count: " << sharedPtr2.use_count() << std::endl; // 1

    return 0;
}

/*
์ถœ๋ ฅ๊ฒฐ๊ณผ:
sharedPtr1 use_count: 1
sharedPtr1 use_count: 2
sharedPtr2 use_count: 2
sharedPtr1 use_count: 0
sharedPtr2 use_count: 1

*/

 

์œ„ ์˜ˆ์ œ์—์„œ๋Š” std::shared_ptr์˜ ์ƒ์„ฑ, ์†Œ์œ ๊ถŒ ์ด์ „, ์ฐธ์กฐ ์นด์šดํŠธ ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ๋ณด์—ฌ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. ๋‘ ๊ฐœ์˜ std::shared_ptr ๊ฐ์ฒด๊ฐ€ ํ•˜๋‚˜์˜ ๊ฐ์ฒด๋ฅผ ๊ณต์œ  ์†Œ์œ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ, ํ•ด๋‹น ๊ฐ์ฒด์˜ ์ฐธ์กฐ ์นด์šดํŠธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์†Œ์œ ๊ถŒ์„ ์ด์ „ํ•˜๋ฉด์„œ ์ฐธ์กฐ ์นด์šดํŠธ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

 

std::weak_ptr

std::weak_ptr์€ ๊ฐ์ฒด๋ฅผ ๊ณต์œ ํ•˜์ง€๋งŒ ์ฐธ์กฐ ์นด์šดํŠธ๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค์ง€ ์•Š๋Š” ์Šค๋งˆํŠธ ํฌ์ธํ„ฐ์ž…๋‹ˆ๋‹ค. std::weak_ptr์€ ์ฃผ๋กœ std::shared_ptr์™€ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋ฉฐ, std::weak_ptr๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋™์•ˆ ์ฐธ์กฐ ์นด์šดํŠธ๋Š” ์ฆ๊ฐ€ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ๊ฐ์ฒด์˜ ์ˆ˜๋ช…์ด std::weak_ptr์— ์˜ํ–ฅ์„ ๋ฐ›์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

weak_ptr์˜ ํŠน์ง•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

๊ฐ์ฒด ์†Œ์œ ๊ถŒ์ด ์—†์Œ

std::weak_ptr์€ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์†Œ์œ ๊ถŒ์„ ๊ฐ–์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ฆ‰, std::weak_ptr๋งŒ์œผ๋กœ๋Š” ๊ฐ์ฒด๊ฐ€ ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์‚ญ์ œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ๋‹จ์ง€ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ๊ด€์ฐฐํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

์ฐธ์กฐํ•˜๋Š” ๊ฐ์ฒด์˜ ์ˆ˜๋ช… ์ฒดํฌ

std::weak_ptr๋Š” std::shared_ptr๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ์Šค๋งˆํŠธ ํฌ์ธํ„ฐ์˜ ์†Œ์œ ์ž์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— std::weak_ptr๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ์—ฌ์ „ํžˆ ์œ ํšจํ•œ์ง€ ํ™•์ธํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

std::weak_ptr๋กœ๋ถ€ํ„ฐ std::shared_ptr ์ƒ์„ฑ

std::weak_ptr๋กœ๋ถ€ํ„ฐ std::shared_ptr์„ ์ƒ์„ฑํ•˜์—ฌ ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ž„์‹œ ์†Œ์œ ๊ถŒ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด ๊ฐ์ฒด๊ฐ€ ์—ฌ์ „ํžˆ ์œ ํšจํ•œ์ง€ ์ฒดํฌํ•œ ๋’ค ์œ ํšจํ•œ ๊ฒฝ์šฐ์—๋งŒ ๊ฐ์ฒด์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

std::weak_ptr์˜ ์ƒ์„ฑ๊ณผ ์†Œ๋ฉธ

std::weak_ptr์€ std::shared_ptr์œผ๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋˜๊ฑฐ๋‚˜, std::shared_ptr๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋œ ํ›„ std::weak_ptr๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. std::weak_ptr์ด ๋” ์ด์ƒ ์œ ํšจํ•˜์ง€ ์•Š๋‹ค๋ฉด ๊ฐ์ฒด์— ๋Œ€ํ•œ ์†Œ์œ ๊ถŒ์ด ๋” ์ด์ƒ ์—†๋‹ค๋Š” ๋œป์ด๋ฉฐ, ๊ฐ์ฒด๊ฐ€ ์‚ญ์ œ๋˜์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

#include <iostream>
#include <memory>

int main() {
    std::shared_ptr<int> sharedPtr = std::make_shared<int>(42);
    std::weak_ptr<int> weakPtr = sharedPtr;

    // weakPtr๋กœ๋ถ€ํ„ฐ sharedPtr ์ƒ์„ฑ
    if (auto lockedSharedPtr = weakPtr.lock()) {
        std::cout << "Weak pointer is valid. Value: " << *lockedSharedPtr << std::endl;
    } else {
        std::cout << "Weak pointer is expired." << std::endl;
    }

    // sharedPtr๋ฅผ resetํ•˜๊ณ  weakPtr๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑํ•œ sharedPtr ์ ‘๊ทผ ์‹œ๋„
    sharedPtr.reset();
    if (auto lockedSharedPtr = weakPtr.lock()) {
        std::cout << "Weak pointer is valid. Value: " << *lockedSharedPtr << std::endl;
    } else {
        std::cout << "Weak pointer is expired." << std::endl;
    }

    return 0;
}

/*
์ถœ๋ ฅ๊ฒฐ๊ณผ:
Weak pointer is valid. Value: 42
Weak pointer is expired.

*/

 

std::weak_ptr์„ ์ด์šฉํ•˜์—ฌ std::shared_ptr๋ฅผ ๊ด€์ฐฐํ•ฉ๋‹ˆ๋‹ค. lock() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ std::weak_ptr๊ฐ€ ์—ฌ์ „ํžˆ ์œ ํšจํ•œ์ง€ ์ฒดํฌํ•˜๊ณ , ์œ ํšจํ•œ ๊ฒฝ์šฐ์—๋งŒ std::shared_ptr๋กœ๋ถ€ํ„ฐ ์ž„์‹œ ์†Œ์œ ๊ถŒ์„ ์–ป์–ด์™€ ๊ฐ์ฒด์— ์ ‘๊ทผํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ std::weak_ptr์ด ๋” ์ด์ƒ ์œ ํšจํ•˜์ง€ ์•Š๋‹ค๋ฉด, ์ฆ‰ ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์‚ญ์ œ๋˜์—ˆ๋‹ค๋ฉด lock() ํ•จ์ˆ˜๋Š” nullptr๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ lock() ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ์ˆœํ™˜ ์ฐธ์กฐ๋ฅผ ๋ฐฉ์ง€ํ•˜๋ฉด์„œ ๊ฐ์ฒด๋ฅผ ์•ˆ์ „ํ•˜๊ฒŒ ๊ด€์ฐฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋Œ“๊ธ€