views:

184

answers:

6

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  A: 

int foo1[42] = { 0 }; // init array of int to all zeroes

char * foo2 = NULL; // init pointer to NULL

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

Paul R
+1  A: 
void* p[10] = { 0 };
kotlinski
+2  A: 

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] = {NULL}

and the pointers should be initialized to NULL.

Billy ONeal
Pointers are not guaranteed to be initialized to NULL, just like ints are not initialized to 0, in many contexts. Note that your example is equivalent to `{NULL, 0}` and `{}` (with the latter being preferred), rather than specifying one value for all of the items in the array.
Roger Pate
+3  A: 

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.

Vlad
Unless the OP does not want a dynamically sized array.
Billy ONeal
@Billy: Well, the OP could just ignore the possibility for dynamic sizing. C++-style arrays are generally a Good Thing.
Vlad
@Vlad: If size is known at compile time though, the builtin arrays are better. It's better to avoid dynamic allocation when possible. `vectors` should be used whenever the size constant is not known at compile time though.
Billy ONeal
@Billy: not sure about that. With `vector` one has at least decent index-out-of-range checking, as well as copy constructors etc. Which usually leads to a less error-prone code. IMHO the need for C-style arrays is justified only by the need of profound manual code optimizations. Modern compilers are usually smart enough to optimize out `vector`'s overhead in most cases.
Vlad
@Billy vectors have many, many advantages over arrays, which have nothing to do with dynamic allocation. For example, vectors carry their size around with them and do not decay into pointers. And one should avoid dynamic allocation - but in one's own code, not in library code.
anon
A: 

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.

wilhelmtell
+3  A: 

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.

AndreyT
Yes! That's exactly what I wanted, thank you :)
+1 - Just as an aside, C requires there to be at least one value between the initializer braces. C++ added The empty brace initializer (`{ }`) specifically because there's no reason that a object's default value has to be anything resembling `0`, and the code asking for initialization of an object might have no idea what a sensible default might be (particularly in templates).
Michael Burr