开发者

Clear an Array to NULL without knowing the type

开发者 https://www.devze.com 2023-01-05 07:23 出处:网络
I have to create a function that can take in an array of pointers with a known size, and set all the pointers to NULL. The 开发者_Go百科caveat is that I don\'t know the type beforehand. This is what I

I have to create a function that can take in an array of pointers with a known size, and set all the pointers to NULL. The 开发者_Go百科caveat is that I don't know the type beforehand. This is what I have tried so far:

    template <typename T>
  static void Nullify(T** _Array, int _Size, unsigned int _SizeOf)
  {
   for (int i = 0; i < _Size; i++)
   {
    _Array[i * _SizeOf] = NULL;
   }
  }

Which is giving me errors (not the function itself, but I am trampling on memory I should not be trampling on resulting in a memory error later on). I am passing in the array (_Array - already initialized to _Size), its size (_Size), and the sizeof(Pointer*) as _SizeOf.

Any help would be greatly appreciated :)


You don't need _SizeOf. This is what you want:

template <typename T>
static void Nullify(T** _Array, int _Size)
{
   for (int i = 0; i < _Size; i++)
   {
      _Array[i] = NULL;
   }
}

The compiler knows the size of a pointer, and does the math for you during the array dereference.


template <class T>
void Nullify(T** the_array, unsigned int size)
{
    std::fill(the_array, the_array + size, static_cast<T*>(0) );
}

template <class T, unsigned int N>
void Nullify(T* (&the_array)[N])
{
    Nullify(the_array, N);
}

The second allows you to pass an array of pointers and nullify it without even passing in a size argument. This requires that you have an actual array to pass in and not a pointer:

Foo* x[10] = {...}
Nullify(x); // sets all pointers in x to 0 (NULL)

... or if you just want to write it out yourself in a loop (this is generally not going to be as fast as std::fill which can even do the actual filling through very efficient assembly code for PODs):

template <class T>
void Nullify(T** the_array, unsigned int size)
{
    for (unsigned int j=0; j < size; ++j)
        the_array[j] = 0;
}

Note that I also avoid using NULL for the same reason Stroustrup does this (avoids the need to include cstddef). When C++0x is more widely implemented, the nullptr keyword will make a nice replacement.

BTW: the static keyword is ignored for function templates. They already have internal linkage so it's superfluous to add it.


You shouldn't be doing _Array[i * _SizeOf] = NULL, but rather _Array[i] = NULL.


I agree with Reed's answer, of course you also should remove the '* _SizeOf' in your assignment.

If possible, I would also suggest you move away from "raw" arrays and use a std::vector in your code. It's a safer structure with array-like semantics, and imposes only a little overhead.


You shouldn't rely on the _SizeOf your value to asses it's type, you should use a function to determine it's type.

At what point do you declare your T** _Array? Perhaps here you should make sure your method guarantees a particular type of Array for any part of your program that calls it, right?

0

精彩评论

暂无评论...
验证码 换一张
取 消