0

So, I have a class with some functions as follows

class MaxHeap{
    private:
        int _size = 0;
        vector<int> arr = {-1};
        int p(int i){return i/2;};
        int l(int i){return i*2;};
        int r(int i){return (i*2)+1;}

    public:
        bool isEmpty() const {return _size == 0;};
        int getMax() const {return arr[1];}; 
        void insertItem(int val);
        void shiftUp(int i);
        int extractMax();
        void shiftDown(int i);
};

and in the main function, if I create two variables like this

MaxHeap* pq1 = new MaxHeap();

MaxHeap pq2;

Will these work same? The arrow operator for the pointer pq1 is the only thing I see as a difference.

I can add elements as follows pq1->insertItem(100); and pq2.insertItem(100).

Is there any difference betweent these two declarations except the arrow operator to access the methods inside the class?

  • 6
    The difference is in the object lifetime. The former dies when you explicitly `delete` it, while the latter dies whene you exit the enclosing braces. – HolyBlackCat Jan 29 '22 at 15:48
  • @HolyBlackCat Oh, Thanks! I didn't know that. – iwrestledthebeartwice Jan 29 '22 at 15:50
  • 1
    `MaxHeap* pq1 = new MaxHeap();` pointer to dynamic allocation. YOU have to manage it's lifetime and `delete` it when done. It is allocated from the freestore where the amount of memory available is effectively limited by your hardware. `MaxHeap pq2;`automatically allocated. Lifetime management is handled automatically for you. Allocated from Automatic storage and that's typically an adjustment of the stack pointer, so nearly free of cost, but the amount of automatic storage is usually very limited (a few megabytes). – user4581301 Jan 29 '22 at 15:51
  • 1
    You might want to read some of [good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – 273K Jan 29 '22 at 15:51
  • 1
    You can easily create a pointer to the latter to make the arrow syntax work: `MaxHeap* pq2Pointer = &pq2; pq2Pointer->insertItem(100);` – fabian Jan 29 '22 at 15:51
  • @user4581301 That sums it up. I was going to search for heap and stack allocation differences but you just summarized it. Thanks man! – iwrestledthebeartwice Jan 29 '22 at 15:52
  • 2
    Good additional reading: [Why should C++ programmers minimize use of 'new'?](https://stackoverflow.com/questions/6500313/why-should-c-programmers-minimize-use-of-new) – user4581301 Jan 29 '22 at 15:56
  • 1
    Be careful of object lifetimes. Many new users get into the trap of creating an automatic storage duration variable then use `&` to get the address and put a pointer to that variable in some list, vector ... but then scope of such a variable ends and you end up saving a pointer to an object that no longer exists. – drescherjm Jan 29 '22 at 16:01
  • 1
    @drescherjm yes, I read it some where, and if that is the case for a program is it viable to use a `shared_ptr` to prevent these dangling pointers? – iwrestledthebeartwice Jan 29 '22 at 16:03
  • 1
    With shared_ptr and make_shared the object is allocated on the freestore – drescherjm Jan 29 '22 at 16:05
  • 1
    Note about `shared_ptr`: it represents an object that has multiple [owners](https://stackoverflow.com/questions/49024982/what-is-ownership-of-resources-or-pointers) each with a more-or-less equal say in the object's lifetime. You can use a `shared_ptr` where only a single owner is necessary, but it has additional overhead to support sharing and people reading the code may come to incorrect conclusions about its behaviour and how it should be used and introduce errors into the code or new code using it based on that bad understanding. See if `unique_ptr` is appropriate first. – user4581301 Jan 30 '22 at 17:11

0 Answers0