How to clear table (with multiple operands)?

Hello - I have a table with strings, floats, and ints. I'd like to just empty everything...I have a method but I was wondering if there were something less clunky.

Here's the table:

struct Saves
    int stOrder;
    String stName;
    String stType;
    float stGauge;
    int stDir;
} SaveTable[101] =

The arrays themselves are empty when the MCU starts, but are filled from the SPIFFS system. Is there a quick way to get it back down to an empty state like when it's first declared?


OK - I'm using this now and it's working:

memset(SaveTable, 0, sizeof SaveTable);

Thank you. My issue was trying a method setting all array values to zero and getting compile errors for the multiple operands.

What about the heap storage you allocated for those String objects? I think you wouldn't get the memory back, for future use. To me, this looks like a memory leak.

I wouldn't be able to add new arrays after that memset?

Yes, but only a few times. Every time you do it, you would reduce the size of your memory pool, until none is left, then it would crash.

Remember, these go in pairs:
malloc() -> free()
new -> delete

It's because you aren't allowing the system the chance to automatically delete String objects when they are no longer in use. You've destroyed the pointers to them.

With dynamic memory, you have to release data, not just stop using it.

This goes for global variables too?

No, only for objects that allocate dynamic memory like the String class. A global variable always takes up a predefined amount of space and you can only change the value.

If you are emptying your table multiple times, @anon57585045 is correct and you will run out of memory. It would be best to set those Strings to and empty string.

OK thank you guys - I have something that's working out now just cycling through and changing the array values.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.