0

Supposing that I have the following two classes:

#ifndef CLASS1_H
#define CLASS1_H

#include <QDebug>

class Class1
{
public:
    Class1(){}

    void printName()
    {
        qDebug() << "Class1";
    }
};

#endif // CLASS1_H

and

#ifndef CLASS2_H
#define CLASS2_H

#include "class1.h"

class Class2
{
public:
    Class2(Class1 *pointerToClass1)
    {
        this->pointerToClass1 = pointerToClass1;
    }

    void doSomething()
    {
        this->pointerToClass1->printName();
    }

private:
    Class1 *pointerToClass1;
};

#endif // CLASS2_H

And the main function looks like this:

#include <QCoreApplication>

#include "class1.h"
#include "class2.h"

void func1()
{
    Class1 class1;

    Class2 *class2_1 = new Class2(&class1);
    Class2 class2_2(&class1);

    class2_1->doSomething();
    class2_2.doSomething();

    delete class2_1;
    class2_1 = NULL;
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    func1();

    return a.exec();
}

What is the main difference of creating an object in the heap (e.g. Class2 *class2_1 = new Class2(&class1);) and just create the object on the stack (e.g. Class2 class2_2(&class1);)?

As far as I know, it is preferred to create the object on the stack when it is declared locally and has a short life. Also, the data is lost when the object exits the stack and in the heap, we need to call delete to free the memory. Is that correct?

Therefore, there is some advantage in using one or another?

KelvinS
  • 2,870
  • 8
  • 34
  • 67
  • 1
    The only difference is that when you allocate on the heap the memory is allocated dynamically at run-time, while local variables (which really don't have to be on "the stack") are allocated by the compiler at compile-time. They are both *constructed* the same and at run-time. Oh and of course you have to manage the destruction of the heap-allocated object yourself. – Some programmer dude Jun 07 '17 at 16:19
  • I really don't think it is duplicated, I'm not interested in know how the OS controls it. I want to know mainly if there's some advantage of using one of them. – KelvinS Jun 07 '17 at 16:37
  • Memory is memory is memory. It doesn't matter where your objects are located, and if you want to know more about how an operating system handles memory allocations then you're asking on a much to high level. The C++ object model and the low-level OS memory allocation functions are on a very different level. – Some programmer dude Jun 07 '17 at 16:45
  • Thanks @Someprogrammerdude. No, I'm not interested in how the operating system handles memory allocations. I just want to know if there is some advantage in using one of them when developing (high level). – KelvinS Jun 07 '17 at 16:51
  • 1
    In general you should avoid to use pointers and (direct) dynamic allocation as much as possible in C++. Most of the time it isn't really needed. Use [standard containers](http://en.cppreference.com/w/cpp/container) with direct objects (not pointers) instead of `new[]`. If you need to link to another object, first try to use references, otherwise plain simple pointers are okay. If you need polymorphism pointers are often a must, but then use [owning smart pointers](http://en.cppreference.com/w/cpp/memory) introduced in C++11. – Some programmer dude Jun 07 '17 at 17:03
  • 1
    With this, avoiding pointers as much as possible, comes the obvious answer to your question: Don't use objects allocated on the heap, unless you really can't avoid it, and then use the smart pointers. And with modern compilers, copy elision and movement semantics you should not need to worry that much about passing or returning objects by value. – Some programmer dude Jun 07 '17 at 17:04
  • Thank you so much @Someprogrammerdude, this could be the answer. – KelvinS Jun 07 '17 at 17:07

1 Answers1

2

With the pointer and the new keyword you create an object on the heap and a assign it to a pointer which is on the stack. Thus when the pointer goes out of scope and it gets "popped" from the stack you won't be able to access your object anymore. (Which is memory leaking) If you create the object on the stack you don't have to worry about freeing the memory (in this case deleting the object that is pointed to by the pointer)

For normal use i would go with the stack version. There are some old-school implementations of design patterns where you start using inheritance and there you use ptrs.

Ph03n1x
  • 794
  • 6
  • 12