Pointers have little to no use

Pages: 1... 345
I believe it's the same way pointers to objects are handled, except the object is a pointer.
@Avilius:

On vs_2013 I can't get the following to compile:

1
2
3
4
5
6
7
8
   class CPerson
   {
      ...
   };


   CPerson * pPx = new CPerson;
   std::shared_ptr<CPerson *>  ppPx = (&pPx);


This was what I actually intended to ask - does shared_ptr cater for pointer to pointers?

Should it - and if it does what behavior should object [ppPx] exhibit on destruction?

should it delete the underlying object pointed to by the pointer that points to it?

usually pointers to pointers should not be deleted.
That code is incorrect. It would result in attempting to treat the stack as part of the heap.
1
2
CPerson * pPx = new CPerson;
std::shared_ptr<CPerson *>  ppPx(new CPerson **(&pPx));

does shared_ptr cater for pointer to pointers?
No.

and if it does what behavior should object [ppPx] exhibit on destruction?
The immediate pointer will be deleted, but not the object at the end of the reference chain.

should it delete the underlying object pointed to by the pointer that points to it?
Should it? No. The class has no way of knowing if the object that the pointer points to is on the stack or on the heap. It doesn't even know if the object it was assigned is on the heap; it just assumes it is.

usually pointers to pointers should not be deleted.
Usually pointers to pointers are a bad idea. Smart pointers to pointers to pointers are never a good idea.
Since shared pointers delete memory when it goes out of scope, it attempts to delete an object that was allocated on the stack, which in turn crashes your program.
Last edited on
@helios and Avilius

thanks for the replies.

i was aware of these facts and wanted to get validation that i was not overlooking my understanding that shared_ptr with pointer to pointer doesn't make sense in that i may be using it incorrectly w.r.t. pointer to pointer.

just to add to your point helios:

The immediate pointer will be deleted


i get your associated point about attempting to treat stack as part of heap and understand your attempting to create a pointer to a pointer heap based style by your snippet:

std::shared_ptr<CPerson *> ppPx(new CPerson **(&pPx));

however, this is also incorrect - but i get what you are attempting to do and my point is that using smart_ptr for pointers to pointers are never a good idea as you pointed out.

so even though pointers to pointers are a bad idea in first place i just would like to add it as one more item that smart pointers cannot cater for i.t.o raw pointers.
The problem is, uses for pointers to pointers are replaced by containers and references. So, I am not aware of any valid use for pointers to pointers in C++11.
closed account (N36fSL3A)
What if you want to write your own container?
What if I don't want to repeat something I've explained multiple times already?
closed account (N36fSL3A)
Whoops, I forgot that I asked that already.
Sorry for not reading all 5 pages of this thread, but what's wrong with these examples?

1. Don't you need pointers to utilize polymorphism?
1
2
std::vector<BaseClass*> myVec;
myVec.push_back( new ChildClass() );


2. I like to use pointers to differentiate between using existing data and copying data:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template <class T>
class MyData
{
private:
  T m_internal_data;
protected:
  T* m_data;
public:
  MyData(T& p_ref) : m_internal_data(p_ref), m_data(&m_internal_data) {}
  MyData(T* p_pnt) : m_data(p_pnt) {}

  void Link(T* p_pnt) { m_data = p_pnt; }
  void Unlink() { m_data = &m_internal_data; }

  T GetData() { return *m_data; }
  void SetData(T p_data) { *m_data = p_data; }
};
Last edited on
1) std::unique_ptr/shared_ptr/reference_wrapper
2) std::unique_prt;
I think that it's good that C++ gives you the choice to use pointers, or not use them, whatever you want to do. There is always that rare use case, but the important thing is that if you don't want to use pointers in c++, there are many good alternatives that give you all the advantages of raw pointers without some of the disadvantages.
Topic archived. No new replies allowed.
Pages: 1... 345