32

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?

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

Christophe
  • 68,716
  • 7
  • 72
  • 138
Alexey S. Larionov
  • 6,555
  • 1
  • 18
  • 37
  • 3
    Unfortunately, shared pointers don't work this way, and this cannot be done. You will need to find some other way to accomplish this goal. The short answer is: "no". – Sam Varshavchik Feb 17 '19 at 20:50
  • 5
    This sounds like a case where you'd want to pass a raw pointer instead of a `shared_ptr`. – Jeremy Friesner Feb 17 '19 at 20:55
  • ^^^ that's what it seems. Or tote around a offset value and base your data-read from `p.get() + header_len`. Trying to change the shared pointer `p` itself seems odd here. – WhozCraig Feb 17 '19 at 20:57
  • @JeremyFriesner I know and I will handle this situation, because I coded this shared pointer myself and futhermore I can read from file and parse simultaneously (lied in question). Just was wondering are smart pointers that good for low-level binary work. Thanks for suggestion. – Alexey S. Larionov Feb 17 '19 at 21:00
  • Use `vector` instead of a raw array. – Ulrich Eckhardt Feb 17 '19 at 21:14
  • @AlexLarionov smart pointers are meant to represent an ownership-relationship, and a pointer that is pointing into the middle of an allocation doesn't own that allocation, so there's not a good fit there. (You could use a smart pointer to control the allocation's lifetime *in addition to* passing raw pointers into the allocation's interior, though; or alternatively pass a smart pointer and also an offset argument) – Jeremy Friesner Feb 17 '19 at 21:20

1 Answers1

47

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);

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

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.

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';

Output:

Pointers managing the array: 5

Offset 100 pointers:
100
100

Offset 200 pointers:
200
200
Galik
  • 47,303
  • 4
  • 80
  • 117
  • 5
    Yes this is exactly what the aliasing constructor is for! – n. m. could be an AI Feb 17 '19 at 21:12
  • Of course, while this shares ownership with the `shared_ptr` used during creation (and all its clones), the offset is not applied to any of the existing clones. So it's not clear to meet whether this meets the question's stated goal of "setting" the shared_ptr – Ben Voigt Feb 18 '19 at 04:09
  • @BenVoigt I have made a note to the effect that the *offset* belongs to each `shared_ptr` and that *offsets* are transferred to another `shared_ptr` when copied (either copy constructed or copy assigned). – Galik Feb 18 '19 at 04:48