3

In my current code I have something like this

while(true) //Infinite loop
{
   char buff[60];
   .....
   ....
}

I wanted to know what would be better performance wise.

  1. Declaring the char buff (that will hold strings that contain linefeeds and new line character) before entering the infinite loop and then using memset(buff, 0, 60); or
  2. Keeping it the way it is. Does memset affect performance ?

Note:

My requirement is that I need to have the char array totally clean everytime the loop restarts.

unwind
  • 391,730
  • 64
  • 469
  • 606
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
  • memset has to do something - but as to which is quicker, try it and see? – doctorlove Aug 20 '13 at 09:28
  • Do you really need to clear it out? It must be populated in the remainder of the loop, so initialization may be unrequired. – hmjd Aug 20 '13 at 09:29
  • 3
    I'm pretty sure those aren't equivalent - after declaring `char buff[60];` the memory isn't guaranteed to be all 0s. If you need the memory to be all 0s, use `memset`, otherwise don't. – Bernhard Barker Aug 20 '13 at 09:29
  • 1
    As posted, your array is not initialized, so outside of debug builds it is roughly equivalent to declaring it outside the infinite loop and *not* calling memset. – Medinoc Aug 20 '13 at 09:30
  • and what about `char buff[60] = {0}` ? http://stackoverflow.com/a/201116/1609356 – Manu343726 Aug 20 '13 at 09:49
  • @Manu343726 this will not work in my case since buffer will have multiple line feeds and new lines "\n" – Rajeshwar Aug 20 '13 at 09:59
  • Is this a buffer for a null-terminated C string? If you want it "empty" for functions like `strlen` and `strcat` you only need to assign '\0' to buff[0]. – Blastfurnace Aug 20 '13 at 10:03
  • 1
    If using `memset` to zero out the array works, then `char buff[60] = {0};` works too. And so does `char buff[60] = {};`, as in my answer. – juanchopanza Aug 20 '13 at 10:07
  • @Rajeshwar - `char buff[60] = {0}` sets **all** the elements of the array to 0. – Pete Becker Aug 20 '13 at 10:55

3 Answers3

6

"The way it is" doesn't give you an array full of zeros. But you don't have to call memset anyway. If you are only to use buff inside of the loop, I think it is better to keep it in the scope of the loop:

while(true) //Infinite loop
{
   char buff[60] = {}; // buff is full of zeros
   .....
   ....
}
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
2

memset does some work, so it must "affect performance". It's generally heavily optimized though, because it's a common operation.

You're unlikely to find a faster way to clear the array because of this, and for comparison the code you showed does not initialize the array at all.

For reference, it should probably look like:

char buff[60];
while (true) {
    memset(buff, 0, sizeof(buff));
    ...

The only solution likely to be faster is finding a way to stop depending on the buffer being zeroed at the start of each iteration

Useless
  • 64,155
  • 6
  • 88
  • 132
2

If you are consistently using it as a C style string:

char buff[60];
buff[0] = 0;   

This will only set the first byte, but if you are using it as a simple C style string, that is all you ever need to set to make it a zero-length string. It is faster than any solution that fills the entire buffer by probably a factor of 7 on a 64-bit machine.

If you actually need the entire buffer filled with 0, then

char buff[60] = {};

will do that.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227