将new_pointer设置为old_pointer + offset的shared_ptr [英] Set shared_ptr with new_pointer that is old_pointer + offset

查看:80
本文介绍了将new_pointer设置为old_pointer + offset的shared_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个智能指针: std :: shared_ptr< char>p(new char [size])表示用原始二进制文件内容填充的数组.在整个数组从文件复制到RAM之后(也只有在此之后),我可以对其进行解析,在此期间,我会检索一些标头信息(一些第一个双字).然后是实际数据.

Here is a smart pointer: std::shared_ptr<char> p(new char[size]) which represents array filled with raw binary file content. After (and only after) the whole array is copied from file to RAM, I can parse it, and during this I retrieve some header information (a few first dwords). Then actual data follows.

在不提供更多上下文的情况下,我很方便地将提到的共享指针设置为以实际数据开头的新地址.该地址仍在分配的内存中.但是如何设置而不丢失它呢?

Without giving much more context, it's handy for me to to set mentioned shared pointer to new address that is beginning of actual data. This address is still in alocated memory. But how to set without losing it?

一个问题是(是/否):是否可以在不调用数据删除的情况下将 p 设置为上一个指针的偏移量?

A question is (yes/no): Is it possible to set p to offset of prevous pointer, without invoking deletion of data?

推荐答案

是可以的.您可以从以下参考资料中使用构造函数 8 alias构造函数:

Yes this is possible. You can use constructor 8, the aliasing constructor from this reference: https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr

// make sure you use an array deleter
std::shared_ptr<char> osp(new char[1024], std::default_delete<char[]>());

// load the data into your buffer at osp.get()

// Find the offset in the data by parsing
auto const offset = parse_buffer_for_offset(osp.get());

// Now set a new offset into the data
std::shared_ptr<char> nsp(osp, osp.get() + offset);

现在, nsp.get()返回偏移地址,但是原始数组将被正确删除.

Now nsp.get() returns the offset address but the original array will get deleted properly.

注意: offset 每个 shared_ptr 的属性,因此,如果您复制 shared_ptr nsp ,您将获得另一个 shared_ptr ,且具有相同的偏移量.无论您构建新副本还是将副本分配给现有的 shared_ptr .

Note: The offset is a property of each shared_ptr so if you copy the shared_ptr nsp you get another shared_ptr with the same offset. This works whether you construct a new copy or assign a copy to an existing shared_ptr.

这意味着您可以具有不同的具有不同偏移量的 shared_ptr ,它们都管理相同的基础资源,这些资源只会在 all shared_ptr 之后清除>被摧毁.

This means you can have different shared_ptr with different offsets that all manage the same, underlying resource which will only be cleaned up after all shared_ptr are destroyed.

要查看运行中的代码,请考虑以下代码:

To see this in operation consider the following code:

std::shared_ptr<char> original_sp(new char[1024], std::default_delete<char[]>());

std::shared_ptr<char> offset_100_sp1(original_sp, original_sp.get() + 100);
std::shared_ptr<char> offset_100_sp2 = offset_100_sp1;

std::shared_ptr<char> offset_200_sp1(original_sp, original_sp.get() + 200);
std::shared_ptr<char> offset_200_sp2 = offset_200_sp1;

std::cout << "\nPointers managing the array: " << original_sp.use_count() << '\n';

std::cout << "\nOffset 100 pointers:" << '\n';
std::cout << std::distance(original_sp.get(), offset_100_sp1.get()) << '\n';
std::cout << std::distance(original_sp.get(), offset_100_sp2.get()) << '\n';

std::cout << "\nOffset 200 pointers:" << '\n';
std::cout << std::distance(original_sp.get(), offset_200_sp1.get()) << '\n';
std::cout << std::distance(original_sp.get(), offset_200_sp2.get()) << '\n';

输出:

Pointers managing the array: 5

Offset 100 pointers:
100
100

Offset 200 pointers:
200
200

这篇关于将new_pointer设置为old_pointer + offset的shared_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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