Dynamically allocating and setting to zero an array of floats Dynamically allocating and setting to zero an array of floats arrays arrays

Dynamically allocating and setting to zero an array of floats


Use sizeof(float) * filter_len unless you are working in some odd implementation where sizeof(float) == sizeof(char).

memset(delay_line, 0, sizeof(float) * filter_len);

Edit: As Stephan202 points out in the comments, 0.0 is a particularly easy floating point value to code for memset since the IEEE standard representation for 0.0 is all zero bits.

memset is operating in the realm of memory, not the realm of numbers. The second parameter, declared an int, is cast to an unsigned char. If your implementation of C++ uses four bytes per float, the following relationships hold:

  • If you memset the float with 0, the value will be 0.0.
  • If you memset the float with 1, the value will be 2.36943e-38.
  • If you memset the float with 42, the value will be 1.51137e-13.
  • If you memset the float with 64, the value will be 3.00392.

So zero is a special case.

If this seems peculiar, recall that memset is declared in <cstring> or <string.h>, and is often used for making things like "***************" or "------------------". That it can also be used to zero memory is a nifty side-effect.

As Milan Babuškov points out in the comments, there is a function bzero (nonstandard and deprecated), available for the moment on Mac and Linux but not Microsoft, which, because it is specially tailored to setting memory to zero, safely omits a few instructions. If you use it, and a puritanical future release of your compiler omits it, it is trivial to implement bzero yourself in a local compatibility patch, unless the future release has re-used the name for some other purpose.


use

#include <algorithm>...std::fill_n( delay_line, filer_len, 0 )


The elements of a dynamically allocated array can be initialized to the default value of the element type by following the array size by an empty pair of parentheses:

float* delay_line = new float[filter_len]();