C++ How do you set an array of pointers to null in

2019-03-08 19:32发布

问题:

I am aware you cannot use an initialiser list for an array. However I have heard of ways that you can set an array of pointers to NULL in a way that is similar to an initialiser list.

I am not certain how this is done. I have heard that a pointer is set to NULL by default, though I do not know if this is guaranteed/ in the C++ standard. I am also not sure if initialising through the new operator compared to normal allocation can make a difference too.

Edit: I mean to do this in a header file/constructor initialisation list. I do not want to put it in the constructor, and I do not want to use a Vector.

回答1:

In order to set an array of pointers to nulls in constructor initializer list, you can use the () initializer

struct S {
  int *a[100];

  S() : a() {
    // `a` contains null pointers 
  }
};

Unfortunately, in the current version of the language the () initializer is the only initializer that you can use with an array member in the constructor initializer list. But apparently this is what you need in your case.

The () has the same effect on arrays allocated with new[]

int **a = new int*[100]();
// `a[i]` contain null pointers 

In other contexts you can use the {} aggregate initializer to achieve the same effect

int *a[100] = {};
// `a` contains null pointers 

Note that there's absolutely no need to squeeze a 0 or a NULL between the {}. The empty pair of {} will do just fine.



回答2:

You can switch from array to std::vector and use

std::vector<T*> v(SIZE);

The values will be initialized by NULLs automatically. This is the preferred C++ way.


Update: Since C++11, there is one more way: using

std::array<T*, SIZE> array = {};

This behaves more like a corrected version of C-style array (in particular, avoids dynamic allocations), carries its size around and doesn't decay to a pointer. The size, however, needs to be known at compile time.



回答3:

Normally an array will not be initialised by default, but if you initialise one or more elements explicitly then any remaining elements will be automatically initialised to 0. Since 0 and NULL are equivalent you can therefore initialise an array of pointers to NULL like this:

float * foo[42] = { NULL }; // init array of pointers to NULL


回答4:

I am not certain how this is done. I have heard that a pointer is set to NULL by default, though I do not know if this is guaranteed/ in the C++ standard.
It is not guaranteed by the C++ standard. Built in types ( like pointers ) are filled with garbage unless set otherwise.

I am also not sure if initialising through the new operator compared to normal allocation can make a difference too.
What do you mean by "normal allocation" ? If you're talking about an automatic variable, then you can do this:

MyType * pointers[2] = {}

and the pointers should be initialized to NULL.



回答5:

void* p[10] = { 0 };


回答6:

If you have a member array then there is no way to initialize, unless it's a static member. If the array isn't a static member then you'll have to fill it inside the constructor's body.

That said, chances are you're really better off using a std::vector. Other than for technical reasons such as unavailability of a standard STL for your platform, or the slightly lesser performance a std::vector is better than an array by any and all criteria. If performance is the issue then make sure you profiled and know by numbers that it is an issue.