0

Why didn't pointer object point in below example get deleted at the end of program by itself, as other object abc got deleted? But for pointer object I had to delete it myself, why is that?

#include<iostream>
#include<string>
using namespace std;

class A
{
private:
    string name;

public:
    A(){
        cout << "const called\n";
    }
    ~A(){
        cout << "object deleted\n";
    }
    void setName(string a){
        name =a;
    }
    string getName(){
        return name;
    }
};

int main()
{
    A abc ;
    A* point = new A();
    abc.setName("Shafaat");
    string a =abc.getName();
    point->setName("Muhammad");
    string b = point->getName();
    cout << "name: "<<a << endl;
    cout << "name: "<<b << endl;
    delete point;
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 2
    learn difference from `stack` and `heap` allocation – Alberto Sinigaglia Jul 15 '21 at 15:48
  • 4
    Please instead learn the difference between automatic and dynamic lifetimes (https://stackoverflow.com/a/9182244/212858) – Useless Jul 15 '21 at 15:49
  • 2
    The *pointer* does indeed get destroyed, just like any other stack-object does. The *object that the pointer points to*, on the other hand, was allocated on the heap, not on the stack, and therefore it is the programmer's responsibility to delete it (unless you are using a smart-pointer like std::shared_ptr or std::unique_ptr; those have destructors defiend that will handle the heap-deletion for you automatically) – Jeremy Friesner Jul 15 '21 at 15:49
  • Good reading: [Why are the terms “automatic” and “dynamic” preferred over the terms “stack” and “heap” in C++ memory management?](https://stackoverflow.com/questions/9181782/why-are-the-terms-automatic-and-dynamic-preferred-over-the-terms-stack-and) – user4581301 Jul 15 '21 at 16:27
  • Thank you everyone. Found this website useful: https://courses.engr.illinois.edu/cs225/sp2021/resources/stack-heap/ – Muhammad Shafaat Khan Jul 16 '21 at 05:50

2 Answers2

1

A pointer is a reference to a memory address containing data (here, your A instance, point).

new create objects on the heap, which must be managed manually, and return a raw pointer (point). Raw pointer must be freed manually (using delete).

In your example, the pointer point is deleted, but not the memory its referring to, aka, your A instance.

If you want to have a pointer that frees its referring data when removed, use dedicated pointer, like std::shared_ptr.

Narann
  • 819
  • 2
  • 8
  • 20
  • 2
    Prefer std::unique_ptr unless you actually need shared ownership. shared_ptr is often misused by people that don't understand or refuse to use move semantics. The CPP Core Guidelines say: "Note: Prefer a unique_ptr over a shared_ptr if there is never more than one owner at a time. shared_ptr is for shared ownership. Note that pervasive use of shared_ptr has a cost (atomic operations on the shared_ptr’s reference count have a measurable aggregate cost)." – George Jul 15 '21 at 16:10
0

Each object got lifetime defined. A pointer is an object in automatic and A you created in dynamic memory is an object.

The exit from main function isn't an end of program, it's just that - exit from a function. Objects with process-wide lifetime will continue to live for a while, dynamically allocated ones will stay until deleted

Swift - Friday Pie
  • 12,777
  • 2
  • 19
  • 42