Teensy 4 memory usage

Daved

Member
Hi - The power of the Teensy 4 is amazing, blazing fast. But I wonder about using it for building arrays on the fly, I know there is no memory manager like windows. Does anyone know the best way of adding a new element to a 1D array everytime around a loop? I'm thinking about limiting the size of arrays by tracking & capping its size at runtime rather than pre-allocating maximum size arrays all over the place that are going to waste precious memory. Is this unwise due to memory fragmentation etc? Should I use malloc() / new? I'm thinking with the power of the Teensy it may be OK to do this but I'm not sure how to best manage the memory (or even if its a bad idea), if the array starts with a single element and it grows by 1 element every time around the loop, after 1000 iterations would I endup with 1000 blocks of memory used that can't use again unless I explicitly free all the blocks of memory?

Sorry for such a question on my first post!
 
Normally you'd use realloc() and increase it by a certain sized chunk if it needs to grow. However if your program is simple enough there's no reason not to just preallocate your arrays as long as you have the space. Memory management in C has a long and sordid history so you should be able to find lots of non Teensy specific advice.
 
Thanks for the realloc() suggestion, I guess this does not leave unused memory blocks behind like creating a new variable every time the array increases in size might. I'm going to do some testing and it really helpful to know what other, more experienced people do before wasting too much time. Your advice is very much appreciated.
 
Back
Top