-1

I come from Java so when I instantiate a class, I would do this in c++:

Class myClass;
myClass.print();

But recently, I've been using the pointer and address as so:

Class *pClass = new Class;
pClass->print();

and the first advantage I can see of course is to delete pClass and free up memory.

delete pClass;

But other than that, I've been used to doing it the first way for my course. Is this frowned upon and should I make a habit of doing it with the pointer?

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
  • 3
    The first one also frees up the memory when it goes out of scope, and you don't have to remember to do it. – chris Jan 06 '18 at 01:36
  • You should avoid using `new`, there are smart pointers which will manage the memory for you. That said pointers (smart or dumb) should only be used if necessary. – Richard Critten Jan 06 '18 at 01:37
  • Start here: https://stackoverflow.com/questions/1549945/why-would-you-ever-want-to-allocate-memory-on-the-heap-rather-than-the-stack – zzxyz Jan 06 '18 at 01:43
  • Your first allocation is stack, your 2nd is heap. And like @RichardCritten said, you should use smart pointers instead of `new` these days. `shared_ptr` and `unique_ptr` – zzxyz Jan 06 '18 at 01:44
  • 1
    The first one is by far the most preferred. One of the best advantages `C++` has over `Java` is *value semantics* where you get to deal with user defined types as values rather than through pointers/references (unless you need to). Also the memory is automatically reclaimed and does not contribute to memory fragmentation (because stack). – Galik Jan 06 '18 at 02:11
  • 1
    Another advantage is that it is super fast to allocate stack objects (practically instant) whereas allocating dynamic objects can be quite slow. – Galik Jan 06 '18 at 02:15
  • *I come from Java so when I instantiate a class, I would do this in c++:* -- Java and C++ are two separate languages. You should never write C++ code using Java as a model in writing your code (and vice-versa -- write Java code using C++ as a model). Pretend Java doesn't exist and learn C++ as a new language with new paradigms. – PaulMcKenzie Jan 06 '18 at 02:41
  • keyword is class (not Class). – 2785528 Jan 06 '18 at 02:43
  • a) An auto var instance is destructed at end of the scope in which it is instantiated. b) A dynamic class instance can be used when you want the lifetime to exceed the scope. c) Both auto var and dynamic class instances, when created in 'main', can last the lifetime of the program. d) But to 'unclutter' your main (for these lifetime objects), use dynamic instances new'd in a function other than main. Avoid globals by passing lifetime objects. – 2785528 Jan 06 '18 at 02:46

3 Answers3

4

Avoid dynamic allocation (even hidden as in std::make_unique/std::make_shared) unless you actually need it for what you are doing. That being said there are plenty of situations where such allocation is very much necessary.

SoronelHaetir
  • 14,104
  • 1
  • 12
  • 23
0

No, you should not "make a habit of doing it with the pointer".

Generally speaking, you should only use pointers and dynamic memory allocation if there is a specific need. If you cannot identify a particular need, then don't do it.

Yes, there are particular cases where using dynamic memory allocation (and deallocation) is appropriate. But doing that also comes with additional requirements, such as either the programmer REMEMBERING to release the object, or using specific techniques (e.g. a smart pointer rather than a raw pointer) to ensure it is properly released. There is no point in accepting the effort to meet those additional requirements unless you have a real need to start with.

Incidentally, operator delete does not necessarily "free up memory". It causes a dynamically allocated object to no longer exist as far as your program is concerned. However, it does not necessarily cause your program to hand memory used back to, for example, the host operating system. Such things depend on details of host operating systems (e.g. how expensive is a request of the OS and its kernel mode drivers to allocate physical memory?) and details of your compiler and standard library and optimisation settings (e.g. how much is your compiler vendor willing to trade off additional memory usage of user programs versus runtime speed?)

Peter
  • 35,646
  • 4
  • 32
  • 74
0

Coming from Java, I would have expected you to say that you were using pointers in C++, since the behavior of Java is very close to using pointers (except that you don't 'delete' anything). If you're not dealing with Java primitive types, then you certainly don't have value semantics.

e.g., the following Java:

Foo f1 = new Foo();
Foo f2 = f1;

is equivalent to the following C++:

Foo *f1 = new Foo();
Foo *f2 = f1;
... delete later

As others have said, it is preferable to not use pointers in C++, but if you're converting Java code to C++, you're in for a lot of pain and code rewriting if you don't use pointers (or smart pointers).

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28