Variable scope, libraries

TomLeMort:
I need to store a list of A objects, however they may use a lot of memory (regarding the available memory space). I don't know the length of the list in advance, but I set a limit. I need to perform operations using these, according to events coming from the serial port.
...
Anyone has an hint?

The Standard Template Library (STL) may help you here. I did a brief write-up here:

The original library can be downloaded from:

Amongst other things, that library gives you new and delete operators. The overhead isn't too bad for a vector (which sounds like what you want). Using the vector code consumed around 1000 bytes of program memory (which you may have spare) and an overhead of something like 6 bytes of RAM per vector (on top of the memory needed by the vector elements themselves of course).

The good thing about vectors is that you can decide their size at runtime, and indeed add to an existing one dynamically. Ultimately you still need the RAM, of course, but you can save it by not having to pre-allocate more than you really need.

Resizing vectors could be expensive, leading to fragmented memory, probably the last thing you want. Another alternative would be a list structure, which is fast and easy to extend, and since lists are by nature fragmented, adding to the list shouldn't cause more fragmentation.

I don't totally agree that malloc is bad (or new for that matter). Certainly if it gets out of control you will run out of memory very quickly. However fixed-size arrays can be worse, if you have to keep allocating the maximum size that you might get.

If you have a situation where you might have X of object A, and Y of object B, and aren't sure until runtime which you need a lot of, then dynamic allocation is about the only way of solving it.

Also consider adding a RAM chip (eg. using SPI) for a couple of dollars. That might help if you need to store a lot of stuff.