1

In the program I'm working on, this particular operation is definitely not going to be the bottleneck, but it did get me thinking. From the answers to questions such as this one and this one I've learned two ways to easily (efficiently) set all the elements of an array to zero in C:

double myArray[3];
static const double zeroes[3] = {0};
memcpy(myArray, zeroes, sizeof(zeroes));

and

double myArray[3];
memset(myArray, 0, numberOfElementsInMyArray * sizeof(myArray[0]));

Before I move onto my real question: I'm not entirely sure but based on the information I've read, I assume this method would, at least in principle, fill the array with int zeroes (well, unsigned char's but these seem to be fairly equivalent). Is that correct? If so, is an explicit conversion of the int zeroes to double zeroes necessary or is it done implicitly if myArray is declared as an array of double's?

Anyway, my real question is this: if the array isn't very big at all (like the myArray I've declared above), is either of these methods still preferred over a little loop? What if you have a few arrays of the same small size that all need to be assigned zeroes? If commented properly, do you think readability is a factor in the decision and favours a particular solution?

Just to be entirely clear: I am not looking to initialize an array to zeroes.

Community
  • 1
  • 1
Wouter
  • 161
  • 1
  • 6
  • If you are concerned about readability, memset is the way code. Or, if you are working on the Windows platform using the native API, ZeroMemory function is used – santahopar Jun 10 '14 at 00:21

2 Answers2

2

If it's just a small array (like three elements), it probably won't make much difference whether you use mem* functions, or a loop, or three distinct assignments. In fact, that latter case may even be faster as you're not suffering the cost of a function call:

myArry[0] = myArray[1] = myArray[2] = 0;

But, even if one is faster, the difference would probably not be worth worrying about. I tend to optimise for readability first then, if needed, optimise for space/storage later.

If it was a choice between memcpy and memset, I'd choose the latter (assuming, as seems to be the case, that the all-zero bit pattern actually represented 0.0 in your implementation) for two reasons:

  • it doesn't require storage of a zeroed array; and
  • the former will get you into trouble if you change the size of one array and forget the other.

And, for what it's worth, your memset solution doesn't need to have the multiplication. Since you can get the size of the entire array, you can just do:

memset (myArray, 0, sizeof (myArray));
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 1
    Of course that assumes that the floating point format on the specific hardware is such, that all zero bytes actually result in a value of 0.0. – Rudy Velthuis Jun 10 '14 at 00:26
  • @Rudy, yes. But that _is_ the case for the OP since they mentioned the use of memset. You'd be hard-pressed trying to find a C implementation nowadays that doesn't use IEEE754 for floating point :-) But I'll update for completeness, thanks. – paxdiablo Jun 10 '14 at 00:27
  • Thanks, I actually didn't know you could do a triple assignment on one line like that in C! – Wouter Jun 10 '14 at 01:16
  • Wouter, keep in mind it's probably no more efficient than three assignment statements, it's more just syntactic sugar. Well, it _is_ more efficient if what you're trying to reduce is space taken up by your source code rather than running time :-) – paxdiablo Jun 10 '14 at 01:19
  • If the scope of double myArray[3]; is within the file. I will use static. The static will take care of assigning it to zero. All the static variables are assigned to zero by default. Remember? – dexterous Jun 10 '14 at 04:12
  • @dexterous_stranger, it's storage duration rather than scope that decides that but, in any case, that only covers the initialisation at program startup. OP specifically stated they were interested in subsequent assignment rather than initialisation. – paxdiablo Jun 10 '14 at 04:37
0

i think the first method of setting without using a loop is better for performance

what happen is that the merroy of array is bitwised by 0 (& 0) so it faster than using a loop for each element in the array.

KarimS
  • 3,812
  • 9
  • 41
  • 64