1

Need to null check cached pointer from multiple sources, like so:

double pointer:

int *ptr = new int(10); 
int **pptr = &ptr; // from another source
ptr = nullptr;
cout << *pptr << endl; //nullptr

shared_ptr:

shared_ptr<int> sptr = make_shared<int>(10); 
weak_ptr<int> wptr = sptr; //from another source
sptr.reset();
cout << wptr.lock() << endl; //nullptr

Concerns:

I'm not especially fond of the double pointer syntax not to mention it can be unsafe, but I keep hearing that shared_ptr is really slow due to reference counting which I don't really need because only one source is supposed to be in charge of object scope. Is the loss of whatever performance worth it with managed memory?

Preferably, are there any alternatives to either of these?

3 Answers3

2

... but I keep hearing that shared_ptr is really slow due to reference counting ...

Yes, there's a performance overhead due to reference counting. Though it is pretty small.

... which I don't really need because only one source is supposed to be in charge of object scope.
...
Preferably, are there any alternatives to either of these?

If proper and exception safe memory management is your only concern, and you have only a single owner, you can always use a std::unique_ptr and the get() function:

unique_ptr<int> uptr = make_unique<int>(10); 
uptr.reset();
cout << uptr.get() << endl; //nullptr
user0042
  • 7,917
  • 3
  • 24
  • 39
  • That was my original design but I need to cache the pointer. My objects exist in a hierarchy and I can't afford to traverse the entire tree every time I need to retrieve an offspring. Each node manages their own children so deleting a node requires null checking from every file that references said pointer. – LinguistTroubadour Sep 23 '17 at 09:42
  • @LinguistTroubadour Then you really have a shared model, as opposed of what you're writing in your question. Go with `std::shared_ptr` then, I'd neglect the performance overhead in favor of overall memory consistency. – user0042 Sep 23 '17 at 09:46
  • Thanks for the help. It's just that I only need weak_ptrs to check whether I can dereference from valid objects. There should only be one shared_ptr in charge of the object's scope so in essence it behaves like a unique_ptr. I was just hoping there could be a method to address this particular case. – LinguistTroubadour Sep 23 '17 at 09:53
1

It seems to me that your design or whatever we call it shouts for the second example. And it is much safer. Go with that one.

newhouse
  • 1,152
  • 1
  • 10
  • 27
  • Actually, if he uses it one place and weak ptr on others, what are we talking about? One ref counting? And that's all? – newhouse Sep 25 '17 at 10:42
1

First of all, pre optimization is the root of all evil and you should NOT spare some of the best features in a programming language because you are afraid of minor performance penalty!

Now that we have this issue out of our way, I'll recommend you use managed objects as much as possible and if and when performance becomes an issue use an actual benchmarking tool to find your bottlenecks...

Regarding your actual problem, It is not clear enough what you are doing with your pointer so its hard to suggest alternatives, but I'd recommend taking a look at this link for a comprehensive description of smart pointers in the standard library.

Daniel Trugman
  • 8,186
  • 20
  • 41