7 years, 12 months ago.

Maximum array size

Hey , I was just trying to collect data and then print them out on my board and for some reason while using 1000 as my array size it would get stuck and not work . Decreasing it to 100 fixed it . What is the limit for an array ? I am using NXP 11u24

Hi Antreas!

What type were you storing inside of the array? Also, it might help to know what kind of data was being assigned to the type along with the code you use to handle the array :)

posted by Krissi Yan 26 Jan 2014

I was using a float and an int . Also the code was a very simple while loop . I would just get a value from analog in and save it in a float or an int . And then print it out using pc.printf . I was trying to do it in a simple way to see if it works and then proceed to more complicated stuff .

posted by Antreas Antoniou 26 Jan 2014

1 Answer

7 years, 12 months ago.

Hello again Antreas!

After looking at the specs for the LPC11U24 I realized that the internal SRAM is a little on the small size compared to what I'm used to working with - so you very well might be looking at an out-of-memory issue. The bottom-line is that you have either 8 or 10 kb of ram, 2kb of which is reserved for internal use; so 6kb (LPC11U24FHI33/301) or 8kb (LPC11U24FBD48/401).

How much your array can hold depends on how you're implementing the array. Here are a few situations:

Simple array

float myFloats[1000]; 
// (4bytes)(1000 myStructs) = 4000 bytes / 1024 byes/kb = 3.9 kb
// ...which is probably fine, but you would have to be careful when using memory elsewhere
// to be safe, use this to reserve that memory upon program entry:
// float *myFloats = new float[1000];

Using array of structs (all initialized once declared)

struct myStruct {
int someInt;  // Either 2 bytes or 4 bytes, depending on the mbed library.
float someFloat; // 4 bytes.
myStruct data[1000]; // Worst case: (4bytes+4bytes)(1000 myStructs) >= 8kb -- which would instantly overrun both chips

My recommendation:

#include "darray.h" // see below for library info
darray<float> myArray;
myArray.reserve(500); // Go ahead and reserve 500 floats since we know we'll need a lot (4bytes)(500 floats) = 2.05 kb
// Will use the memory reserved for the 500 floats then expand the array at a size of 5 floats each time it runs out of space
// Once the target is out of memory, it fails a cassert rather than trying to write to reserved memory space.

I've went ahead and published my darray library incase you want to try that solution out: DynamicArrays

However, these solutions don't really solve the main problem of working with low SRAM, and if you really need a buffer that size, you will need to do something like run two large buffers and delete[] one once you're close to running out of memory. Another option is to use mbed's filesystem library to store those values into the on-board flash memory. The speed at which this would work kind of defeats the purpose of a buffer, but it would be satisfactory if you're just doing some basic data-logging.

Hope that helps :)

Ok I will post it tomorrow when I double check it . Also I did include an if statement to prevent overflowing in my code as well . So that was not the issue . Keep an eye out and I ll have it posted tomorrow . Thanks

posted by Antreas Antoniou 26 Jan 2014

Ok I got the problem now . My array size was 1000 but I also had a function saving some data in another 1000 array which made my memory full . Thanks for your help . Very informing and helpful . Have a nice day .

posted by Antreas Antoniou 27 Jan 2014