c++ 11 智能指针
程序员文章站
2024-02-29 09:42:22
...
智能指针
编译
gcc -std=c++11 test.cpp -o test
测试代码
#include <iostream>
#include <memory>
// 编译 加上 -std=c++11
int main()
{
int a = 10;
std::shared_ptr<int> ptr = std::make_shared<int>(a);
// 测试copy 构造
std::shared_ptr<int> ptr1(ptr);
std::cout << "Ptr : " << ptr << " ptr1 : "<< ptr1 <<std::endl;
// 智能指针重载操作符*
//c++ 11 智能指针一般通过引用计数来对内存进行管理,重载了* 和 -> 可以像普通指针一样使用。
// 引用分为强引用 和 弱引用
// 强引用技术为0,代表对象已被释放掉了
std::cout << "*ptr : " << *ptr <<" *ptr1: "<< *ptr1 <<std::endl;
std::cout << "ptr count : " << ptr.use_count() <<" ptr1 count : "<<ptr1.use_count() << std::endl;
// 获得原始指针
int * b = ptr.get();
std::cout << " b : " << b << std::endl;
std::cout << "*b : " << *b << std::endl;
std::cout << "Strong count : " << ptr.use_count() << std::endl;
std::weak_ptr<int> weak_ptr(ptr);
// weak_ptr 并没有重载* 和 -> 操作符 >>
// std::cout << "*weak_ptr: " << *weak_ptr << std::endl;
// std::cout << "weak_ptr : " << weak_ptr << std::endl;
// 引用计数
std::cout << "weak count: " << weak_ptr.use_count() << std::endl;
// don`t transfer shared_ptr to unique_ptr
std::unique_ptr<int> unique_ptr(b);
//std::unique_ptr<int *> unique_ptr(&a);
std::cout << "*unique_ptr : " << *unique_ptr << std::endl;
return 0;
}
程序运行结果:
Ptr : 0x1c98c30 ptr1 : 0x1c98c30
*ptr : 10 *ptr1: 10
ptr count : 2 ptr1 count : 2
b : 0x1c98c30
*b : 10
Strong count : 2
weak count: 2
*unique_ptr : 10
*** Error in `./test': munmap_chunk(): invalid pointer: 0x0000000001c98c30 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f5e171fc7e5]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x1a8)[0x7f5e17209698]
./test[0x401a0e]
./test[0x401729]
./test[0x4010bb]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f5e171a5830]
./test[0x400c49]
======= Memory map: ========
00400000-00404000 r-xp 00000000 08:08 6822803 /home/gongcm/fucking-code/c++11/smart-ptr/test
00603000-00604000 r--p 00003000 08:08 6822803 /home/gongcm/fucking-code/c++11/smart-ptr/test
00604000-00605000 rw-p 00004000 08:08 6822803 /home/gongcm/fucking-code/c++11/smart-ptr/test
01c87000-01cb9000 rw-p 00000000 00:00 0 [heap]
7f5e16e7c000-7f5e16f84000 r-xp 00000000 08:04 396780 /lib/x86_64-linux-gnu/libm-2.23.so
7f5e16f84000-7f5e17183000 ---p 00108000 08:04 396780 /lib/x86_64-linux-gnu/libm-2.23.so
7f5e17183000-7f5e17184000 r--p 00107000 08:04 396780 /lib/x86_64-linux-gnu/libm-2.23.so
7f5e17184000-7f5e17185000 rw-p 00108000 08:04 396780 /lib/x86_64-linux-gnu/libm-2.23.so
7f5e17185000-7f5e17345000 r-xp 00000000 08:04 396710 /lib/x86_64-linux-gnu/libc-2.23.so
7f5e17345000-7f5e17545000 ---p 001c0000 08:04 396710 /lib/x86_64-linux-gnu/libc-2.23.so
7f5e17545000-7f5e17549000 r--p 001c0000 08:04 396710 /lib/x86_64-linux-gnu/libc-2.23.so
7f5e17549000-7f5e1754b000 rw-p 001c4000 08:04 396710 /lib/x86_64-linux-gnu/libc-2.23.so
7f5e1754b000-7f5e1754f000 rw-p 00000000 00:00 0
7f5e1754f000-7f5e17565000 r-xp 00000000 08:04 396748 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f5e17565000-7f5e17764000 ---p 00016000 08:04 396748 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f5e17764000-7f5e17765000 rw-p 00015000 08:04 396748 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f5e17765000-7f5e178d7000 r-xp 00000000 08:05 400884 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f5e178d7000-7f5e17ad7000 ---p 00172000 08:05 400884 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f5e17ad7000-7f5e17ae1000 r--p 00172000 08:05 400884 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f5e17ae1000-7f5e17ae3000 rw-p 0017c000 08:05 400884 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f5e17ae3000-7f5e17ae7000 rw-p 00000000 00:00 0
7f5e17ae7000-7f5e17b0d000 r-xp 00000000 08:04 396682 /lib/x86_64-linux-gnu/ld-2.23.so
7f5e17cef000-7f5e17cf5000 rw-p 00000000 00:00 0
7f5e17d0b000-7f5e17d0c000 rw-p 00000000 00:00 0
7f5e17d0c000-7f5e17d0d000 r--p 00025000 08:04 396682 /lib/x86_64-linux-gnu/ld-2.23.so
7f5e17d0d000-7f5e17d0e000 rw-p 00026000 08:04 396682 /lib/x86_64-linux-gnu/ld-2.23.so
7f5e17d0e000-7f5e17d0f000 rw-p 00000000 00:00 0
7ffc98c1f000-7ffc98c40000 rw-p 00000000 00:00 0 [stack]
7ffc98d0b000-7ffc98d0e000 r--p 00000000 00:00 0 [vvar]
7ffc98d0e000-7ffc98d10000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
Aborted (core dumped)
错误分析:
首先shared_ptr 对象 free
然后unique_ptr 对象 free
导致 一个地址 free 两次
上一篇: 定义行为像指针的类(类比于智能指针)和定义行为像值的类
下一篇: shared_ptr基本用法