Clearing character arrays

In one Photon application I am using six char arrays, each of them being 610 characters long, for logging application data.
I want to clear those arrays (resetting all characters to ‘0’) every once and a while and I initiated the clearing process through a Particle.function that I activate from the Particle console.
The Particle function sets a variable CLEAR to ‘1’ and when CLEAR == 1 is detected within the loop(), all char arrays are “cleared”:
for (int m = 0; m < 610; m++)
chararray1[m] = 0;
chararray2[m] = 0;
chararray3[m] = 0;
This works fine, however I found out that this process is taking a lot of time: might be several seconds.

Of course these are rather long arrays, but I assume there must be an easier, quicker way to erase them all (or rather: reset them to ‘0’).

The simplest way would be to use memset(chararray, 0, sizeof(charraray))
If you are storing strings it may also suffice to only set the first character in each array to '\0'

BTW, instead of chararray1, chararray2, … it would be better to use a two-dimensional array chararray[6][610]. Then the one instruction I provided above will clear the entire set off arrays in one go.

BTW²: I doubt that your loop approach would actually take multiple seconds either. There must be something else going on that takes that long.


Thanks for the quick response ScruffR!
I’ve implented your suggestions, however the cause of the problem was indeed something different: I constructed the strings in a part of the program where it conflicted with the clearing process…

1 Like