-1

I was looking at this tutorial about the stack and heap:

When to use new operator in C++ and when it should not be used?

And in the heap example, it uses the new keyword, but he began by initializing int* ptr1 to NULL. Is that important, and if so why? Or, is it just wasting space?

I looked at the code and tried thinking why use NULL first when you could just use int* ptr1 = new int(28);?

Side note: If someone could also explain the stack and heap in laymans terms, it would be appreciated.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • 3
    C++ should be learnt using a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of online tutorials. These basic things are explained in any beginner c++ book. – Jason Jan 26 '23 at 10:45
  • Most people would think the same. (That's not a good source to learn from. There are no known good online C++ tutorials. Get a good book.) – molbdnilo Jan 26 '23 at 10:47
  • There's no good reason. – HolyBlackCat Jan 26 '23 at 10:47
  • There's also the issue that, in C++, one would use`nullptr` and not `NULL`. But there's no need for it here as you and others have pointed out. – Friedrich Jan 26 '23 at 10:53
  • 1
    The new operator should hardly be used anymore in current C++. When dynamic memory allocation is needed use [std::make_unique<>](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique) for polymorphic objects (objects with virtual functions). Or use containers like [std::vector](https://en.cppreference.com/w/cpp/container/vector). – Pepijn Kramer Jan 26 '23 at 10:53
  • 1
    There are many outdated and bad tutorials around the 'net. Stay away from any one which uses `using namespace std;` or uses `NULL` (instead of the modern `nullptr`). – chi Jan 26 '23 at 10:55
  • The big problem for you is that there is a lot of (outdated) C++ material out there. Most up-to-date information can be found on https://en.cppreference.com/w/, Any current book from this [list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Or if you want something more accessible which is still good : https://www.learncpp.com/ – Pepijn Kramer Jan 26 '23 at 10:56
  • 1
    @chi Don't forget those that `#include `, geeksforgeeks, and ALL of the competitive coding sites – Pepijn Kramer Jan 26 '23 at 10:57
  • Stack and heap are in a way just implementation details. C++ works on a higher level, you have variables that live as long as a scope (usually local variables) and variables you can allocate for control over the lifecycle (new/std::make_unique). Stacks and heaps are just one way of managing those requirements. – Pepijn Kramer Jan 26 '23 at 10:59
  • @PepijnKramer in no way are they 'just implementation details', the behavior is precisely stated in the c++ specs. A c++ programmer must 100% understand what those behaviors are, along with static data – pm100 Jan 26 '23 at 18:19
  • Indeed the behaviors are precisely stated and should be understood. The way those behaviors are implemented are / can be hardware specific. And do not require for example to use a processor stack. – Pepijn Kramer Jan 27 '23 at 05:53

1 Answers1

0

Initializing a pointer to nullptr on declaration is an excellent practice to adhere to.

But, what they did in the tutorial could be done better. I think it is more of an example of syntax than anything else.

If you can initialize when declaring, that is better and faster. Use nullptr instead of NULL, which is just a macro for 0 and that can be problematic.

If you want to learn more about pointers, learning smart pointers is recommended. They can be a really powerful way to avoid bugs and memory leaks.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Isak True
  • 13
  • 2