0

Good day! Little question about smart pointers. In general I have pointer to BYTE array and I want to use std::shared_ptr instead of plain pointer. Here my pointer example

shared_ptr<BYTE> pointer(new BYTE[100]);
LPBYTE *old_pointer = pointer;

Of course this is invalid example but how I can assign my smartpointer to other general pointer?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
user922871
  • 435
  • 2
  • 6
  • 17
  • 2
    why don't you use a vector? – Daniel A. White Jul 30 '14 at 22:46
  • Sorry but I don't ask about vector or other containers. This is not real program example. I just try to understand usage of shared_ptr. LPBYTE = long pointer to BYTE = unsigned char far – user922871 Jul 30 '14 at 22:50
  • 3
    Why would you want to assign the content of a smart pointer to a raw pointer and not another `shared_ptr`? Nevertheless use `std::shared_ptr::get()` – 101010 Jul 30 '14 at 22:50
  • 1
    It is not possible to have shared_ptr of `[]`, [see here](http://stackoverflow.com/questions/8947579/why-isnt-there-a-stdshared-ptrt-specialisation) – M.M Jul 30 '14 at 22:50
  • I think `shared_ptr< std::array >` would be OK – M.M Jul 30 '14 at 22:51

3 Answers3

4

You are probably asking for

std::shared_ptr<std::array<BYTE,100>> pointer(make_shared<std::array<BYTE,100>>());
LPBYTE old_pointer = pointer.get()->data();

To have another reference you can simply have a statement like this

// increases reference count
std::shared_ptr<std::array<BYTE,100>> pointer2 = pointer;

See the detailed documentations here please: std::shared_ptr constructor and std::shared_ptr::get().

Though remember:
Accessing the smart pointer's pointee using std::shared_ptr::get() bypasses the semantics and features provided by using the std::shared_ptr. That's highly discouraged, unless you very well know what you're doing with old_pointer, and if it's still in scope.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • 2
    You should add that the OP shouldn't do this because it defeats the purpose of the shared pointer. – Fantastic Mr Fox Jul 30 '14 at 22:52
  • Yep, but as I know get() method will not rise reference counter, right? – user922871 Jul 30 '14 at 22:54
  • @user922871 no, but if you want to do that then why are you trying to get a raw pointer to it? – Fantastic Mr Fox Jul 30 '14 at 22:54
  • Actually I don't try to get raw pointer, I just want to understand shared_ptr usage. So, if I want to increase reference counter then I must assign my pointer to another shared_ptr with same type? Otherwise RC will not be increased? – user922871 Jul 30 '14 at 22:58
1

You can access the raw pointer using get_pointer(shared_ptr<>)

BYTE* raw_pointer = get_pointer(pointer);

As others point out, grabbing the raw pointer means you now have a reference that isn't counted and have a potentially dangerous situation if not careful.

Also, if you use a shared_ptr<> to hold a pointer to a raw array, you need to call delete[] on the raw pointer rather than delete. That means you'll need a custom deleter. I realize your example is likely illustrative and not literal, but still thought it worthy to point this out.

Chris Cleeland
  • 4,760
  • 3
  • 26
  • 28
0

Recent versions of boost::shared_ptr can hold a dynamically allocated array. That feature is being added to the Library Fundamentals TS (part of the standardization process).

Nevin
  • 4,595
  • 18
  • 24