auto_ptr的大小是多少? [英] What is the size of an auto_ptr?

查看:103
本文介绍了auto_ptr的大小是多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

auto_ptr是否具有与指针相同的大小?

Does an auto_ptr have the same size as a pointer?

我必须用boost::scoped_ptr代替它,我想知道这两种数据类型的大小是否相同.

I have to substitute it with a boost::scoped_ptr, and I was wondering if these two data types have the same size.

推荐答案

您只需执行以下操作即可轻松找出尺寸:

You can pretty easily find out what the sizes are by simply doing this:

#include <iostream>
#include <memory>

#include <boost/shared_ptr.hpp>
#include <boost/scoped_ptr.hpp>

int main() {
  std::cout << "raw pointer: " << sizeof(int*) << std::endl;
  std::cout << "auto-ptr: " << sizeof(std::auto_ptr<int>) << std::endl;
  std::cout << "unique-ptr: " << sizeof(std::unique_ptr<int>) << std::endl;
  std::cout << "shared-ptr: " << sizeof(std::shared_ptr<int>) << std::endl;
  std::cout << "boost scoped-ptr: " << sizeof(boost::scoped_ptr<int>) << std::endl;
  std::cout << "boost shared-ptr: " << sizeof(boost::shared_ptr<int>) << std::endl;
  return 0;
};

在我的平台(64位)上,使用GCC 4.8.2和Boost 1.55,我得到以下输出:

On my platform (64bit), with GCC 4.8.2 and Boost 1.55, I get this output:

raw pointer: 8
auto-ptr: 8
unique-ptr: 8
shared-ptr: 16
boost scoped-ptr: 8
boost shared-ptr: 16

当然,不能严格保证这些结果在任何地方都是相同的.换句话说,标准不要求auto-ptr或unique-ptr(C ++ 11)的大小与常规(原始)指针的大小相同.据我所知,Boost文档没有为scoped-ptr明确提供该保证,但确实提供了以下声明:

Of course, there is no strict guarantee that these results will be the same anywhere. In other words, the standard does not require that auto-ptr or unique-ptr (C++11) be of the same size as a regular (raw) pointer. As far as I can see, the Boost documentation does not explicitly give that guarantee either for scoped-ptr, but they do give the following statement:

因为scoped_ptr很简单,在其通常的实现中,每个操作都与内置指针一样快,并且没有内置指针比其他空间更多的开销.

Because scoped_ptr is simple, in its usual implementation every operation is as fast as for a built-in pointer and it has no more space overhead that a built-in pointer.

我想说,假设以上陈述对于auto-ptr,unique-ptr或scoped-ptr的任何体面实现都是成立的,这是非常安全的.如果您在某个平台上对其进行测试,并获得缩小该结果的结果,那么我会认为该实现是毫无价值的无法使用的垃圾.没有明智的理由使这些智能指针中的任何一个指针都包含比单个指针更多的内容(唯一性指针除外,唯一性指针可能必须放置一个非空的自定义删除对象,但否则,其大小应与其他两个).

I would say that it is pretty safe to assume that the above statement holds true for any decent implementation of either auto-ptr, unique-ptr or scoped-ptr. If you test it on some platform and get a result that contracts that, then I would consider that implementation to be worthless unusable garbage. There is no sensible reason for either one of these smart pointers to contain anything more than a single pointer (except for unique-ptr which could have to make place for a non-empty custom deleter object, but otherwise, should have the same size as the other two).

顺便说一句,如果您也好奇为什么我的测试中shared-ptr的大小为16.这是因为shared-ptr的功能使其必须包含两个指针:一个是指向相关对象的(可能是别名)指针,一个是指向引用计数共享数据结构的指针,所有其关联的共享或弱指针使用.共享指针可以指向除引用计数实际管理的对象之外的其他东西(或一些相关但不同的指针,例如指向成员或基类/派生类的指针),这是它需要它的原因除了指向引用计数的共享数据的指针之外,该本地指针(而且,当可以直接获取指向对象的指针而不必通过共享数据结构时,它的性能也更好).但这对于需要引用计数的人来说仍然是合理的开销.

And by the way, if you are also curious about why shared-ptr has a size of 16 in my tests. That's because the features of shared-ptr make it so that individual shared-pointers must contain 2 pointers: one is a (possibly aliasing) pointer to the object in question, and one is a pointer to the reference-counting shared data structure that all its associated shared- or weak- pointers use. The fact that shared-pointers can point to something other than what is actually being managed by the reference-counting (or some related but different pointer, like a pointer to a member or base- / derived- class) is the reason why it needs that local pointer in addition to the pointer to the reference-counting shared data (and also, it also performs better when the pointer to the object can be obtained directly, without having to go through the shared data structure). But this is still a reasonable overhead for when you need reference-counting.

这篇关于auto_ptr的大小是多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆