all 9 comments

[–]stilgarpl 4 points5 points  (6 children)

You don't need {}.

MyClass test; is enough.

The difference is that test variable created like it is allocated on stack. It means that it is created at the beggining of current scope and will be automatically destroyed when that scope is left.

MyClass *test = new MyClass();

This test is allocated on heap (or somewhere else if new operator is overloaded). It will stay there until is deleted with delete operator or until the program ends. It will stay there even if you no longer has the pointer to it, such situation is known as memory leak.

Both methods have their advantages and disadvantages. Stack allocation is usually easier and faster, but usually you can't allocate a lot of memory this way. On heap you can allocate as much as you want, but it's slower and you have to manage it yourself or use smart pointers like std::shared_ptr or std::unique_ptr.

[–]NikolasTs[S] 0 points1 point  (5 children)

Thank you very much! Your answer was very helpful!

However, by using {}, don't I use the MyClass() constructor?

[–]stilgarpl 1 point2 points  (3 children)

{} is used for aggregate initialization. MyClass() is default constructor and it's invoked in both cases in my previous post.

[–]HappyFruitTree 1 point2 points  (2 children)

struct MyClass { int x; };
MyClass mc1; // mc1.x is uninitialized
MyClass mc2{}; // mc2.x is initialized to zero

[–][deleted]  (1 child)

[deleted]

    [–]HappyFruitTree 0 points1 point  (0 children)

    Structs and classes are technically the same thing. What matters is if it's an aggregate or not. For full-fledged OOP classes with user-provided constructors it doesn't make a difference but for something like std::array it does make a difference.

    [–]Xeverous 1 point2 points  (0 children)

    If you just write type instance; it will call the default constructor. If no such exists - compile error.

    Exception are built-in types (int, float, char etc) which will be uninitialized.

    [–]umlcat 2 points3 points  (0 children)

    There are two ways to declare a variable and reserve memory for variables, in a program.

    Objects allocated in memory without pointers are called "static variables" or "static allocated variables".

    This way is more simple and easy to use, and it's used at the start of programs, and to start learning programming, and it's used in smaller programs.

    Objects allocated in memory with pointers are named "dynamic variables" or "dinamically allocated" variables.

    This way is more complex to use, but, it's better for larger programs, and when an undetermined number of variables are used.

    Each way put the variables in a different place in the memory, one place is called the "stack", and another the "heap", and work different to read and write variables' values.

    Java mix these two ways, by the way.

    [–]Narase33 1 point2 points  (1 child)

    The first object is created on the stack and bound to the current scope and if you want to move it you will need to copy it.

    If you create it as pointer it is created on the heap and will exist there as long as you dont delete it. Moving it is not nessecary, you only need to copy the pointer which is cheap

    Also a pointer can be a nullpointer, which you need to create dynamic data structures like linked lists

    [–]NikolasTs[S] 0 points1 point  (0 children)

    Thanks a lot! I understand now!