When should I use nullptr and why not declare a pointer?

When should I use nullptr and why not declare a pointer?

I'm learning C++ and I was wondering when should I use Null Pointers and why? Why would I declare it later?

Thank you very much <3

1
2
3
unsigned char *pField = nullptr; 

pField = new unsigned char[FieldHeight * FieldWidget];


Edit: I appreciate all the comments and it helped me to understand more about it. I spent the whole afternoon trying to understand that and you clarified everything. Thank you very much ;D
Last edited on
All variables should be initialised when defined. The default is nullptr for pointers. If possible, variables should be initialised with their required value when defined. So:

 
auto pField {new unsigned char[FieldHeight * FieldWidget]};


PS. Don't forget to delete the memory:

 
delete [] pField;

Last edited on
> When should I use nullptr and why not declare a pointer?
> I'm learning C++

As far as possible, never; particularly when one is learning C++. Use std::vector<> instead.

This is so much better: std::vector<unsigned char> Field( FieldHeight * FieldWidth ) ;

As Sutter and Alexandrescu point out in 'C++ Coding Standards: 101 Rules, Guidelines, and Best Practices':
Use vector and string instead of arrays

Why juggle Ming vases? Avoid implementing array abstractions with C-style arrays, pointer arithmetic, and memory management primitives. Using vector or string not only makes your life easier, but also helps you write safer and more scalable software.
...
Here are some reasons to prefer the standard facilities over C-style arrays:
They manage their own memory automatically ...
They have a rich interface ...
They are compatible with C's memory model ...
They offer extended checking ...
They don't waste much efficiency for all that ...
They foster optimizations ...


tutorial: https://cal-linux.com/tutorials/vectors.html
For times when you don't know yet what the pointer will point to, and times when the pointer will change its value many times and sometimes won't be pointing at anything valid.

Last edited on
That covered, lets look at something else:
1
2
3
type name = dummy; // declaration and initialization of variable 'name'

name = value; // assignment to variable 'name' 

Should we initialize a variable with known value when we declare it? Yes.
If you have variables with unknown values, you might forget that and use them anyway.

Can we always initialize with "real" value? No. Getting the real value might be delayed or conditional.
But you still need a value that you can show (with a test) to be "not real" but known. The "dummy".
Also, instead of using new, consider using a managed pointer - unique_ptr or shared_ptr. See http://www.cplusplus.com/reference/memory/ (bit not auto_ptr).

So:

 
auto pFiled {std::make_unique<unsigned char[]>(FieldHeight * FieldWidget)};


Now no delete [] is needed. The allocated memory will automatically be deleted when pFiled goes out of scope.
As far as possible, avoid using dynamically allocated arrays with either raw pointers or managed pointers.

Dynamically allocated arrays are best represented by std::vector<>. They keep track of their current size; they help us avoid out-of-bound access errors (a range-based loop or a v.begin() v.end() loop is inherently bounds-safe). std::vector<> is actually smarter than the 'smart' pointer std::unique_ptr; using a smart pointer where std::vector<> is a better choice is not a particularly smart thing to do.
Unless for learning/exercise etc, the order of choice is:

1) Use a container
2) Use a managed pointer
3) Use a raw pointer

If 1) can't be used then justification for not using should be provided and if not 1) is justified, then not using 2) should also be justified.

In modern C++ code, I wouldn't expect to see 3).
If you want to measure the size of your array and iterate your array at given some point, then vector offers lot of useful functionality but in pointer version you need to manage manually. There is also a performance cost involves in vector during instantization.

Below links describes performance data’s for vectors when it handling with integers types. This also help you some level to get some more insight.

https://lemire.me/blog/2012/06/20/do-not-waste-time-with-stl-vectors/
Topic archived. No new replies allowed.