Search code examples
c++memset

Clearing out a char array best scenario - memset or not?


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.


Solution

  • "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
       .....
       ....
    }