-2
#include<iostream>
using namespace std;

class car
{
    string name;
    int num;
  
    public:
    car(string a, int n)
    {
        cout << "Constructor called" << endl;
        this->name = a;
        this->num = n;
    }
  
    void enter()
    {
        cin >> name;
        cin >> num;
    }
  
    void display()
    {
        cout << "Name: " << name << endl;
        cout << "Num: " << num << endl;
    }
};
  
int main()
{
    // Using new keyword
    car *p = new car("Honda", 2017);
    p->display();
}

why don't we deallocate space for 'car *p'? If we don't deallocate memory space on the heap, won't there be a memory leak? I am a newbie trying to learn c++. I had read that I always had to deallocate space after allocating on heap... I found this code online

Chris
  • 26,361
  • 5
  • 21
  • 42
kaywin
  • 11
  • 2
  • 1
    We do, if we want to avoid memory leaks, but better not to use `new` at all. It certainly isn't needed here. Also, all memory allocated by a program is returned to the OS when it terminates, but that's something of a side issue. – Paul Sanders Oct 23 '22 at 20:05
  • 2
    Yes, that is a memory leak. When a program ends all memory is released to the operating system so some folks are lazy about cleaning up before that. That doesn't mean it is the right things to do. – Retired Ninja Oct 23 '22 at 20:05
  • 1
    We do. By doing `delete p;` More on this: [Why is it a bad idea to use 'new'? (duplicate)](https://stackoverflow.com/q/7620385) – thedemons Oct 23 '22 at 20:06
  • The one issue of relying on program termination to clean up memory is that many memory checkers (such as valgrind), will report the memory leakage. Thus you will never get a "clean" memory report when using those tools if you code in this fashion. Then you have to figure out whether the leakage is from something that really matters (like in the middle of your running program), or due to not caring and having the OS clean up the memory on program termination. – PaulMcKenzie Oct 23 '22 at 20:18
  • 3
    Who is "we"? We cannot tell why you and your collective have bad coding practices. Don't assume that all code you encounter is good code. – Clifford Oct 23 '22 at 20:35
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Oct 23 '22 at 23:26

1 Answers1

0

Most operating systems will free all memory associated with a program when it ends, so in this case, delete p; can be extraneous.

This gets a bit more uncertain the lower level you go, especially in embedded systems. Best to get in the practice of using delete everytime you use new.

Or don't use new/delete at all when possible.

In your case it is certainly not necessary to use dynamic memory allocation. You could simply write:

int main() {
    car p("Honda", 2017);
    p.display();
}

If you insisted on using dynamic memory allocation, you might use a smart pointer which will take care of the deallocation for you.

#include <memory>

// car class stuff

int main() {
    std::unique_ptr<car> p = std::make_unique<car>("Honda", 2017);
    p->display();
}

An aside on your car class: accustom yourself to using member initializer lists in your constructors.

    car(string a, int n)
    {
        cout << "Constructor called" << endl;
        this->name = a;
        this->num = n;
    }

Becomes:

    car(string a, int n) : name(a), num(n) 
    {
        cout << "Constructor called" << endl;
    }
Chris
  • 26,361
  • 5
  • 21
  • 42