0

I know shared pointers are implied to share the same memory. But what if my shared pointer points to an element which is not the first in the memory of another shared pointer?

Consider a raw pointer example:

int* array = new int[10];
int* segment = &array[5];

Can I make the same thing with array and segment being shared pointers? Will they count references in this case?

Kaiyakha
  • 1,463
  • 1
  • 6
  • 19
  • Does this answer your question? [How can I change the pointer in shared\_ptr without losing the ability to delete the memory?](https://stackoverflow.com/questions/68364460/how-can-i-change-the-pointer-in-shared-ptr-without-losing-the-ability-to-delete) – JaMiT Aug 13 '22 at 21:23
  • 1
    Yeah look up `aliasing constructor` for shared_ptr – Mike Vine Aug 13 '22 at 21:23
  • @MikeVine can I have aliasing with `make_shared`? I only managed to find aliasing with a constructor – Kaiyakha Aug 13 '22 at 22:06
  • @Kaiyakha `make_shared()` cannot create an aliased `shared_ptr`, you must call the aliasing constructor directly. But you can make such a `shared_ptr` that aliases a `shared_ptr` that was created with `make_shared()`. – Remy Lebeau Aug 13 '22 at 22:27
  • @JaMiT yes, but actually no as the headline of the post you suggested is not explicit enough – Kaiyakha Aug 13 '22 at 22:31
  • @Kaiyakha The headline is not what answers your question. Many people are downright awful at writing titles for their questions. Please read the question body to see if it asks what you are asking (or is close enough), and the answer to see if it answers your question. (It might not, but please give it a fair shot.) – JaMiT Aug 13 '22 at 22:43
  • @JaMiT You didn't quite get my point. Awful headlines is one of the major reasons for duplicates. How am I supposed to find an already existing question if its title does not reflect its content? How other people would find it? – Kaiyakha Aug 14 '22 at 08:55
  • @Kaiyakha *"How am I supposed to find an already existing question if [...]"* -- you're not necessarily supposed to. Flagging as a duplicate is not saying "you should have found this"; it's saying "let's get all the answers in one place". That way someone searching and finding any of the duplicates can be directed to the same set of answers. – JaMiT Aug 15 '22 at 04:35
  • @JaMiT Reasonable, but posting duplicates may entail lots of downvotes and I will eventually get banned for asking new questions, so... – Kaiyakha Aug 15 '22 at 08:52
  • @Kaiyakha *"posting duplicates may entail lots of downvotes"* -- why would it? Being a duplicate should be a non-factor in up/down voting. Well-asked duplicates do not warrant down votes, and badly-asked duplicates do warrant down votes. Even if they are not closed as duplicates. *Example of a question closed as duplicate (not of this question) with plenty of up votes: [What will happen when I call a member function on a NULL object pointer?](https://stackoverflow.com/questions/2533476/what-will-happen-when-i-call-a-member-function-on-a-null-object-pointer).* I believe your fear is misplaced. – JaMiT Aug 16 '22 at 01:45

1 Answers1

5

std::shared_ptr has an aliasing constructor for exactly this kind of situation:

template< class Y >
shared_ptr( const shared_ptr<Y>& r, element_type* ptr ) noexcept;

The aliasing constructor: constructs a shared_ptr which shares ownership information with the initial value of r, but holds an unrelated and unmanaged pointer ptr. If this shared_ptr is the last of the group to go out of scope, it will call the stored deleter for the object originally managed by r. However, calling get() on this shared_ptr will always return a copy of ptr. It is the responsibility of the programmer to make sure that this ptr remains valid as long as this shared_ptr exists, such as in the typical use cases where ptr is a member of the object managed by r or is an alias (e.g., downcast) of r.get()

For example:

auto array = std::make_shared<int[]>(10);
auto segment = std::shared_ptr<int>(array, &array[5]);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770