Initializing a huge array in ESP32's PSRAM

Hi,

I have a huge (constant values) array of [200000][4] int values I need to store in the ESP32's PSRAM.
Using the ps_malloc(800000 * siezeof(int)) function I'm able to allocate the memory, but I'm not sure how I can initialize it other that assigning each and every element manually.
Is there a way to initialize the array in one command?
For small arrays I'd just write:

int small_array[2][3] = {
                         {1,2,3} ,
                         {4,5,6}
                        };

But how can this be done with 800000 values?

Hi,

It really depends on what your initialization values are. If they can be calculated as in your example (1, 2, 3, 4, 5, 6...) then use a for loop to initialize it after allocating the memory. If the values cannot be calculated then they will have to be initialized individually. Can you give any more information on what your initial values are or represent?

you can always create a script that generates the source code to statically initialize an array. that output can be captured in a separate file that is #include'd where needed

Both options are interesting. As I cannot disclose the purpose of this project (confidential business needs), I won't show or discuss the data itself and for that I'm sorry.
I will investigate both options: to see if the data can be generated via mathematical formulas or just use a static #include file.

If the data can be computed on the fly, I'd ask myself if you'd actually need such a massive array for it.

So it is not so nice of you to search help in an open forum.

it doesn't matter what the data is, it's the size of the data that's the issue

Does that justify outsourcing commercial development to a free community?

Thanks for all the comments and suggestions.
I did not mean to "leach" on this fine forum. I asked what I found to be a (hopefully) small issue.

In any case, I found a workaround:

Since my array values are in the range of 0-1024, I can use an array of "short" instead of "int".
I also changed the partition scheme of "Huge app" which gives 3MB of memory, and since I do not need OTA, it's OK.

Right now with a normally initialized array of [200000][4], I get a scketch that uses only 59% of the program storage space.
I ran a nested loop and printed out all of the elements in the array so it seems to be working.

Why not use the constant variable type 'uint16_t'? It will be the same size across ALL architectures.

How will those values magically appear in the PSRAM? To get them in RAM, they have to be loaded from flash or other nonvolatile memory first, or they have to be generated upon initialization, in which case you can just use ps_malloc.

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