using memset

was just suggested that I look into the use of memset to fill an array with a single value. I set out to test it and I seem to be using it incorrectly, its as if the function isn’t recognized (its also not highlighted in the editor) do I need to include another library to use it in Arduino ide? The error occured right at line 2 as soon as it hit memset.

//    test of memset
int ch1[4];
memset (ch1, 0, sizeof(ch1));

void setup()
{
  while (!Serial) ;          // while the serial stream is not open, do nothing:
  uint16_t time = millis();
  Serial.print(time, DEC);   //DEBUG checking to make sure serial is working
  Serial.println (F("milis from start"));  
  delay(1500);
  time = millis() - time;
  Serial.print(time, DEC);   //DEBUG checking to make sure serial is working
  Serial.println (F("milis from start"));  

}
   
void loop()
{
  Serial.println(" ");
  for (i=0, i<5, i++)
  {
    Serial.print(ch1(i));
    Serial.print(" ");
  }
  Serial.println(" ");
  memset(ch1, 0, sizeof(ch1));
  for (i=0, i<5, i++)
  {
    Serial.print(ch1(i));
    Serial.print(" ");
  }
}

error code

[color=maroon]memset_test_01:3: error: expected constructor, destructor, or type conversion before '(' token
memset_test_01.ino: In function 'void loop()':
memset_test_01:21: error: 'i' was not declared in this scope
memset_test_01:21: error: expected `;' before ')' token
memset_test_01:27: error: expected `)' before ';' token
memset_test_01:28: error: expected `;' before ')' token
memset_test_01:33: error: expected primary-expression before '}' token
memset_test_01:33: error: expected `;' before '}' token
memset_test_01:33: error: expected primary-expression before '}' token
memset_test_01:33: error: expected `)' before '}' token
memset_test_01:33: error: expected primary-expression before '}' token
memset_test_01:33: error: expected `;' before '}' token[/color]

You’re using it right, your problem is as the error says, you use variable “i” (in the for loops) but you didn’t declare it.

Also, to access an index of an array you have to use array[index], not array(index) :wink:

So:

for (uint8_t i = 0; i < 5; i++)
    Serial.print( ch1[i] );

Edit: I didn’t notice the first error, it’s because you must use it inside setup() or whatever. An array is always filled with 0’s when you declare it (if you don’t fill it manually, of course), so you don’t need that first memset.

Last thing, you declare your array with a size of 4 elements, that is: 0, 1, 2 and 3. Yet your for loop will do: 0, 1, 2, 3 and 4. You will read outside of your array bounds!

Haven’t noticed pYro_65’s reply…

Only declarations and preprocessor commands can go outside of functions, memset needs to be called from inside a function, you could move it inside setup();

The Arduino will set your global variables to zero for you on reset, so there is really no need for an initial memset call.

If you want to do it without memset, you can do it in the initialization:

int ch1[4] = {}; //Default initialization

//Or you can provide a set of default parameters

int ch1[4] = { 1,2,3,4 };

You read past the array in the following example, the range [0 to 4] is 5 elements, not 4 like you declared. Also array subscripts are denoted using square brackets ch1[i], not like you have here:

 for (i=0, i<5, i++)
  {
    Serial.print(ch1(i));
    Serial.print(" ");
  }

http://www.cplusplus.com/doc/tutorial/arrays/

Thank you all for your great help and for the links to tutorials. Sorry about the sloppy errors, i certainly appreciate the help.

An array is always filled with 0's when you declare it (if you don't fill it manually, of course), so you don't need that first memset.

That is not true. Arrays with global scope are initialized to 0, but that is not required at other scope levels (e.g., function, statement-block). So, if you define an array within a function or statement block and you want all values to be a certain value, you should use memset().

Actually, I started coding in C in the late 1970's prior to any of the ANSI standards so I got into the habit of assuming that all data contained garbage values. While redundant in most cases, I think not assuming the compiler performs certain initializations has saved me a lot of grief over the years.

econjack:
While redundant in most cases, I think not assuming the compiler performs certain initializations has saved me a lot of grief over the years.

Agreed! It's always best to be explicit. Never make assumptions about what the compiler will or will not do, and those assumptions will never come back to bite you. I always explicitly initialize my data. All it takes to really mess things up is to move some code around and change its scope, and not realize that it also changes the behavior (like moving an array declaration from global to local.)

Initialization is so fast, that it's a false optimization to skip it because you think it's already been done.

Actually, I started coding in C in the late 1970's prior to any of the ANSI standards so I got into the habit of assuming that all data contained garbage values. While redundant in most cases, I think not assuming the compiler performs certain initializations has saved me a lot of grief over the years.

My first experience programming in C was using a compiler that initialized everything to 3087, unless you explicitly defined another value. Made bizarre things happen. The company that provided that compiler is no longer in business.

I think I used to work with a compiler that initialized memory (on a malloc anyway) to 0x0BAD (ie. "bad") and if you tried to use that memory as a pointer (eg. it was a struct and the struct contained pointers) you would get an access violation because 0x0D is odd, and pointers had to point to even addresses.