WTF之安全删除对象
2012-12-19 21:27
134 查看
在 source/javascriptcore/wtf/OwnPtrCommon.h中有这么一段代码:
template <typename T> inline void deleteOwnedPtr(T* ptr)
{
typedef char known[sizeof(T) ? 1 : -1];
if (sizeof(known))
delete ptr;
}
该函数让编译器检查不完整类型(incomplete types),以实现安全删除对象,boost中也有类
4000
似的技法:
//http://www.boost.org/doc/libs/1_35_0/boost/checked_delete.hpp
// verify that types are complete for increased safety
template<class T> inline void checked_delete(T * x)
{
// intentionally complex - simplification causes regressions
typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
(void) sizeof(type_must_be_complete);
delete x;
}
这里利用了sizeof操作符应用到不完整类型上面时,编译器会报错;即使在某些编译器上通过了编译,声明一个大小为-1的数组,也会报错。
--------------------------------------------------------------------------------------
不完整类型,包括:
1. void
2. 未知大小的数组
3. 不完整类型元素的数组
4. 没有定义的结构、联合,或者枚举
5. 声明但没有定义的类
6. 指向声明但没有定义类的指针
sizeof操作符不能用在以下操作数上:
(摘录于:http://msdn.microsoft.com/en-us/library/4s7x1k91(v=vs.71).aspx)
1.Functions (However, sizeof can be applied to pointers to functions)
2.Bit fields
3.Undefined classes
4.The type void
5.Dynamically allocated arrays
6.External arrays
7.Incomplete types
8.Parenthesized names of incomplete types
template <typename T> inline void deleteOwnedPtr(T* ptr)
{
typedef char known[sizeof(T) ? 1 : -1];
if (sizeof(known))
delete ptr;
}
该函数让编译器检查不完整类型(incomplete types),以实现安全删除对象,boost中也有类
4000
似的技法:
//http://www.boost.org/doc/libs/1_35_0/boost/checked_delete.hpp
// verify that types are complete for increased safety
template<class T> inline void checked_delete(T * x)
{
// intentionally complex - simplification causes regressions
typedef char type_must_be_complete[ sizeof(T)? 1: -1 ];
(void) sizeof(type_must_be_complete);
delete x;
}
这里利用了sizeof操作符应用到不完整类型上面时,编译器会报错;即使在某些编译器上通过了编译,声明一个大小为-1的数组,也会报错。
--------------------------------------------------------------------------------------
不完整类型,包括:
1. void
2. 未知大小的数组
3. 不完整类型元素的数组
4. 没有定义的结构、联合,或者枚举
5. 声明但没有定义的类
6. 指向声明但没有定义类的指针
sizeof操作符不能用在以下操作数上:
(摘录于:http://msdn.microsoft.com/en-us/library/4s7x1k91(v=vs.71).aspx)
1.Functions (However, sizeof can be applied to pointers to functions)
2.Bit fields
3.Undefined classes
4.The type void
5.Dynamically allocated arrays
6.External arrays
7.Incomplete types
8.Parenthesized names of incomplete types
相关文章推荐
- Iterator的remove方法可保证从源集合中安全地删除对象
- Iterator的remove方法可保证从源集合中安全地删除对象
- 为什么Iterator的remove方法可保证从源集合中安全地删除对象,而在迭代期间不能直接删除集合内元素
- Iterator的remove方法可保证从源集合中安全地删除对象(转)
- List中安全地删除对象
- 让对象removeChild时自动删除子对象/属性/侦听
- web 中关于清除session会话 中保存的对象的时候 执行删除语句操作
- 磁盘工具软件(安全删除敏感数据
- oracle 删除用户下的所有用户对象
- JPA无法删除对象【实际项目解决办法】
- 基于主体和对象的访问控制--附:计算机安全的层次
- JavaScript中对象属性的加入和删除
- 线程安全和对象共享
- mysql的binlog安全删除
- 创建数据库表后,然后删除,提示有重复的对象,需排序,压缩
- 删除list或vector中的自定义结构体对象
- 从集合List中删除对象,正确写法(异常java.util.ConcurrentModificationException
- oracle 用户添加、删除、修改,用户权限,对象特权管理
- python remove a element safely 安全地删除列表中的元素
- javascript动态添加、修改、删除对象的属性和方法