cleaning the memory

hello
I was told that it is best to clean the memory of the pointers\chars\in-coming every time I finish with them to avoid problems.
I wanted to know what is there a different between

for (int i=0;i<10,i++)
{
temp[i]='\n';
}

and this function

memset(temp,'\n',10)

thanks ,

I wanted to know what is there a different between...and this function

No. Either method effectively puts 10 stop signs in the array where only one is needed. All that is needed is to set the 0th element to NULL, and then keep the array NULL terminated by adding a NULL after adding each character to the array.

can you show me how to this? an example?

because I think I didn't understand.

can you show me how to this? an example?

This code reads data from the serial port. When start of packet marker arrives (<), the inData array is cleared. Subsequent characters are added to the array, followed by a NULL, until the end of packet marker (>) arrives.

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
{
   Serial.begin(57600);
   // Other stuff...
}

void loop()
{
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
  {
    char inChar = Serial.read();
    if(inChar == SOP)
    {
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    }
    else if(inChar == EOP)
    {
       ended = true;
       break;
    }
    else
    {
      if(index < 79)
      {
        inData[index] = inChar;
        index++;
        inData[index] = '\0';
      }
    }
  }

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
  {
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';
  }
}

Notice that at no time is the inData array ever filled with NULLs using either of the techniques you describe. It simply isn’t necessary. The NULL is a stop sign. It means that you have reached the end of the array. One is sufficient. 10 or 80 or 100 means no more than 1.

The usual reason to 'clear' a char array is to ensure that the empty array is null terminated and always remains null terminated, so it is a valid c-string. PaulS shows you how you would achieve that by putting a null character after the last character stored in the array; an empty array has a null in the first position.

Note that the newline character '\n' you used in your original examples is not the same as null and doesn't null-terminate the array. Both the examples you showed would leave the array unterminated, which would cause undefined behaviour if you ever passed that array to a function that expected it to contain a null-terminated string.

Back in the '80's my company produced C compilers for PC and MSDOS. Virtually all of our C Standard Library routines were written in assembler for speed, including all of the mem?() functions. My guess is that may still be true with the Gnu compiler, but I don't know for sure. If it is true, memset() should be faster and perhaps smaller than using a for loop. In line with PeterH's comment, it would be better to use:

memset(temp, '\0', 10);

for the call. A more portable way to call the code if you wish to null out the entire array:

memset(temp, '\0', sizeof(temp) / sizeof(temp[0]);

That way, the compiler does the math to figure out how many bytes of memory to null out.

econjack:
That way, the compiler does the math to figure out how many bytes of memory to null out.

Since memset applies to bytes, it would be more correct to call:

memset(temp, '\0', sizeof(temp));

If the size of an element in the array was ever greater than one, you would want to zero all memory occupied by the array, and not just as many bytes as there were elements in the array.

PaulS:

I wanted to know what is there a different between...and this function

No. Either method effectively puts 10 stop signs in the array where only one is needed. All that is needed is to set the 0th element to NULL, and then keep the array NULL terminated by adding a NULL after adding each character to the array.

NULL or NUL or \0 ??

NULL is the name. '\0' is the value. nul is some bastardized construct for people that can't spell or follow anyone else's standard.

PaulS:
NULL is the name. '\0' is the value. nul is some bastardized construct for people that can't spell or follow anyone else's standard.

I'm going to save that analogy for future reference. :slight_smile: