0

Do I have to delete pointer if program will exit soon?

Like this:

#include "mainwidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWidget w;
    w.show();

    int *a = new int[5];

    delete []a;
    return a.exec();
}

Do I really have to call delete []a; or It doesn't matter because after return a.exec(); (main() funtcion finishes) all pointers will be destroyed automatically?

László Papp
  • 51,870
  • 39
  • 111
  • 135
iamnp
  • 510
  • 8
  • 23

1 Answers1

0

All reusable resources are reclaimed by the operating system when your program exits (with a few special exceptions, like perhaps sockets and sometimes shared memory). But in this case, it will be reclaimed.

If you want to get into good habits though, use std::unique_ptr<int[]> (or if the size is really a constant and is that small, just int a[5] or std::array<int, 5>) to store the pointer, then you don't have to worry about delete[] at all.

uk4321
  • 1,028
  • 8
  • 18