STL memory.cpp
memory.cpp
# // Filename: memory
#
# // Comment By: 凝霜
# // E-mail: mdl2009@vip.qq.com
# // Blog: http://blog.csdn.net/mdl13412
#
# // 智能指针在STL中只有一个auto_ptr, 用于对原生指针的生命周期进行管理,
# // 但是其本身有许多另其不安全的特性, 例如以一个auto_ptr去构造另一个
# // auto_ptr会导致对象所有权的转移, 另外如果两个只能指针同时指向一个
# // 原声指针就可能导致指针被意外的提前释放, 另一个auto_ptr对其解引用时,
# // 就会导致错误
# //
# // C++0x已经通过了::Boost::scoped_ptr的决案, 所以任何使用auto_ptr的
# // 情景都应该使用scoped_ptr替代, 因为其更安全, 但是仍然不能解决多个
# // 智能指针同时拥有一个对象导致的提前释放问题, 要解决这个问题, 请使用
# // ::Boost::shared_ptr
#
# // 我博客中还有一个我实现的auto_ptr, 大家可以参考
# // http://blog.csdn.net/mdl13412/article/details/6244631
#
# /*
# * Copyright (c) 1997
# * Silicon Graphics Computer Systems, Inc.
# *
# * Permission to use, copy, modify, distribute and sell this software
# * and its documentation for any purpose is hereby granted without fee,
# * provided that the above copyright notice appear in all copies and
# * that both that copyright notice and this permission notice appear
# * in supporting documentation. Silicon Graphics makes no
# * representations about the suitability of this software for any
# * purpose. It is provided "as is" without express or implied warranty.
# *
# */
#
# #ifndef __SGI_STL_MEMORY
# #define __SGI_STL_MEMORY
#
# #include <stl_algobase.h>
# #include <stl_alloc.h>
# #include <stl_construct.h>
# #include <stl_tempbuf.h>
# #include <stl_uninitialized.h>
# #include <stl_raw_storage_iter.h>
#
# // Note: auto_ptr is commented out in this release because the details
# // of the interface are still being discussed by the C++ standardization
# // committee. It will be included once the iterface is finalized.
#
# #if 0
# #if defined(_MUTABLE_IS_KEYWORD) && defined(_EXPLICIT_IS_KEYWORD) && \
# defined(__STL_MEMBER_TEMPLATES)
#
# __STL_BEGIN_NAMESPACE
#
# template <class X> class auto_ptr
# {
# private:
# X* ptr; // 托管的原生指针
# mutable bool owns; // 是否拥有托管指针
# public:
# typedef X element_type;
#
# // 显式构造函数, 防止隐式转换
# // 通常接收一个原生指针进行构造
# // 构造函数不能失败, 故不能抛出异常
# explicit auto_ptr(X* p = ) __STL_NOTHROW : ptr(p), owns(p) {}
#
# // auto_ptr 可以以相同类型的 auto_ptr 进行构造
# // 注意: 对象所有权发生转移, 用于构造的只能指针释放对象所有权
# auto_ptr(const auto_ptr& a) __STL_NOTHROW : ptr(a.ptr), owns(a.owns) {
# a.owns = ;
# }
#
# // auto_ptr 可以以另一种相关类型的 auto_ptr 进行构造
# // 注意: T 必须能转换成 X 类型, 对象所有权发生转移
# template <class T> auto_ptr(const auto_ptr<T>& a) __STL_NOTHROW
# : ptr(a.ptr), owns(a.owns) {
# a.owns = ;
# }
#
# // 重载operator =, 首先判断是否是本身, 如果不是则进行对象所有权的转移
# auto_ptr& operator=(const auto_ptr& a) __STL_NOTHROW {
# if (&a != this) {
# if (owns)
# delete ptr;
# owns = a.owns;
# ptr = a.ptr;
# a.owns = ;
# }
# // 个人感觉应该在此加上
# // return *this;
# }
#
# // 和上面的operator =功能一样, 但是提供了兼容类型的转换操作
# template <class T> auto_ptr& operator=(const auto_ptr<T>& a) __STL_NOTHROW {
# if (&a != this) {
# if (owns)
# delete ptr;
# owns = a.owns;
# ptr = a.ptr;
# a.owns = ;
# }
#
# // 个人感觉应该在此加上
# // return *this;
# }
#
# // auto_ptr生命周期结束, 释放对象所有权, 实现资源释放目的
# ~auto_ptr() {
# if (owns)
# delete ptr;
# }
#
# // 提供和原生指针一样的操作
# X& operator*() const __STL_NOTHROW { return *ptr; }
# X* operator->() const __STL_NOTHROW { return ptr; }
#
# // 获取原生指针的地址, 主要用于一些只接受原生指针的函数
# X* get() const __STL_NOTHROW { return ptr; }
# // 释放指针所有权, 并返回原生指针
# // 主要用于取消指针托管
# X* release const __STL_NOTHROW { owns = false; return ptr }
# };
#
# __STL_END_NAMESPACE
# #endif /* mutable && explicit && member templates */
# #endif /* 0 */
#
#
# #endif /* __SGI_STL_MEMORY */
#
#
# // Local Variables:
# // mode:C++
# // End:
STL memory.cpp的更多相关文章
- STL之Errors and Exceptions
Error Handling STL设计的目标是性能最优化,而不是最安全. 错误检查是极其浪费时间的,因此,STL对于错误处理几乎没有做处理,因此,这对STL的使用者的要求就非常高. 为什么不采取错误 ...
- STL之set && multiset
一.set 在了解关联容器set之前,让我们先来看看下面这个例子,并猜测该例子输出什么: // stl/set1.cpp #include <iostream> #include < ...
- C++ STL源码剖析
stl_config.h defalloc.h stl_alloc.h memory.cpp stl_construct.h stl_uninitialized.h stl_iterator.h ty ...
- 在android app中使用STL库(转)
1.在jni目录下新建Application.mk; 加入 APP_STL := stlport_static右边的值还可以换成下面几个: system - 使用默认最小的C++运行库,这样生成的应用 ...
- NDK编译STL
方法: 1.在jni目录下新建Application.mk; 加入 APP_STL := stlport_static 右边的值还可以换成下面几个: system - 使用默认最小的C++运行库, ...
- 怎么样android app正在使用STL库
方法: 1.在jni文件夹下新建Application.mk; 增加 APP_STL := stlport_static右边的值还能够换成以下几个: system - 使用默认最小的C++执行库,这样 ...
- C/C++代码静态分析工具调研
C/C++代码静态分析工具调研 摘自:https://www.jianshu.com/p/92886d979401 简述 静态分析(static analysis)是指在不执行代码的情况下对其进行分析 ...
- 通过OCCI连接oracle(C++)
OCCI介绍 OCCI:Oracle C++调用接口(OCCI),即Oracle的C++API,允许你使用面向对象的特性.本地类.C++语言的方法来访问Oracle数据库. OCCI优势 基于标准C+ ...
- RTABMAP-ROS RGB-D的建图原理
CoreNode.cpp: new CoreWrapper -- CoreWrapper.cpp: process() -- mapsManager_.updateMapCaches MapsMana ...
随机推荐
- saltstack内置state模块user
user 模块是用来创建用户和管理用户设定的,用户可以被设置成 present 状态或者 absent 状态. hwg: user.present: - fullname: Jim - shell: ...
- Windows 7 里进程管理器里面的各列是什么含义?主要是和内存有关的内存-专用工作集,内存-工作集,内存-提交大小???
内存 - 工作集:私人工作集中的内存数量与进程正在使用且可以由其他进程共享的内存数量的总和. 内存 - 峰值工作集:进程所使用的工作集内存的最大数量. 内存 - 工作集增量:进程所使用的工作集内存中的 ...
- 编辑器模式下如何实例化Prefab
当我们在EditMode下需要用脚本批量添加prefab时,可以用 PrefabUtility.InstantiatePrefab(prefab) as GameObject; 注意:如果用GameO ...
- xcrun: error: unable to find utility "instruments", not a developer tool or in PATH
xcrun: error: unable to find utility "instruments", not a developer tool or in PATH 用web ...
- vue2+element组件库开发
Vue2:https://cn.vuejs.org/v2/guide/single-file-components.html element组件库:http://element-cn.eleme.io ...
- python 基础 9.7 创建表
一. 创建表 #/usr/bin/python #-*- coding:utf-8 -*- #@Time :2017/11/22 18:05 #@Auther :liuzhenchuan #@Fi ...
- windows10系统自带输入法不能切换中文如何解决
具体如下: 1.打开计算机管理软件,右击“开始”按钮,在弹出的菜单中选择“计算机管理”: 2.或在桌面右击“此电脑”图标,在弹出的菜单中选择“管理”: 3.在打开的计算机管理软件中,选择“系统工具”- ...
- HBase核心技术点
表的rowkey设计核心思想: 依据rowkey查询最快 对rowkey进行范围查询range 前缀匹配 预分区创建的三种方式 create 'ns1:t1', 'f1', SPLITS => ...
- visual studio2017 无法添加引用 未能加载包ReferenceManagerPackage not such interface support 解决方法
安装完visual studio 2017 后添加引用总是提示 未能加载包ReferenceManagerPackage, 这个问题困扰了两天,直到在网上看到了下面这一段 I just got thi ...
- vue项目创建流程和使用
vue项目的创建 npm run dev 让项目执行起来 #下载vuex npm install vuex --save#下载axiosnpm install axios --save 当我们生成项目 ...