Dynamic allocation of a data structure for logging purposes

I am working on a project (see my sig) where I want to log some data. In my ignorance, I thought I could use some of the program memory (32 KB EEPROM/Arduino Nano) to store my log, but alas.

I am continuing this project on the Nano, but would like to upgrade to a MEGA (with 8 KB of SRAM rather than the Nano's 2 KB) and use the extra memory seamlessly. This would also allow others to benefit from my mistake and get a MEGA from the beginning and still use the extra memory without any needs for modification.

Now I understand that I can check how much memory is available through this page: http://playground.arduino.cc/Code/AvailableMemory . But what is the best practice for a one-time at run-time allocation of memory for my array of logging data structure?

There is so little memory to work with that I think a log of any consequence would be better stored on an SD card or something. This would be the approach of a datalogger and there are lots of Arduino dataloggers in google.

I have an application that requires a minimal event log to be shipped to a base-station via SMS. What I do is keep a ring buffer of 8 single-byte event codes and a single byte event count. I can tack on the event list and counter as a 18 character string on the end of the SMS message and decode it. It is very basic, but does give some visibility into the operation of the remote end at very low overhead.

But what is the best practice for a one-time at run-time allocation of memory for my array of logging data structure?

You decide how much data you want to store, and you create static array(s) to hold the data. All global variables are allocated once at run time.
You do not need to be doing dynamic memory allocation.

JanHolbo:
But what is the best practice for a one-time at run-time allocation of memory for my array of logging data structure?

As PaulS says, best practice on Arduino ATMega systems is to avoid dynamic allocation entirely and stick to static. To make an allocation vary depending on the target part (small for ATMega328, larger for ATMega2560) you can use the #define constants set up by the IDE at compile time.

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#  define LOG_ENTRIES 20
#else
#  define LOG_ENTRIES 5
#endif

struct log_entry the_log[LOG_ENTRIES];

Another option is to use the AvailableMemory API you mentioned to work out how much space you want to dedicate, then do a one-time allocation of this size in setup().

Have you considered a ATmega1284P based board (16 KB SRAM)?

I have considered an upgrade, at least to a MEGA (8KB) but the more the better :slight_smile:
Unfortunately that is not in the books currently. Both my wife and I are studying and I have more important use for the little spending money I have the next few months (Wife's Birthday and Christmas are approaching fast :slight_smile:

It would be nice to support multiple boards with the same code base. Currently I have the number of Log entries hard coded into the source. In the future, it would be nice to be able to auto-sense available memory at boot time and allocate the maximum of available memory (with a little safety margin calculated in).

Future plans will allow the external control scripts (from the computer) to change monitoring on the fly - maybe even let the HerBot do this itself when getting close to running out of log space.

Thank You for all your inputs!