use of PSRAM / declare Array at runtime

Mike0675

Member
I try to declare a 2 dimensional Array at runtime:

class my_LOG
{
public:

private:
byte *linePointer;
String *message;
};

void my_LOG::begin(byte NumberOfLogs, byte NumberOfLines)
{
linePointer = new byte[NumberOfLogs]; \\ this works
message = new String[NumberOfLogs][NumberOfLines]; \\ this don´t work
}

How can this be done?

And further: How can this allocated to PSRAM?

Ive tried to do this with std::vector but this used all of RAM1.
 
You have to allocate the memory from PSRAM using extmem_malloc and then use placement new to initialize it.

Code:
void* ptr = extmem_malloc(sizeof(String)*NumberOfLogs*NumberOfLines);
message = new (ptr) String[NumberOfLogs][NumberOfLines];
 
Thanks for your reply,

The compiler now trows this error:

error: array size in new-expression must be constant (regarding the second variable [NumberOfLines]).
 
Are they really required to be variable values? Seems like it would be easier to put the arrays as class members and dynamically create the class object rather than dynamically create the arrays.
 
Back
Top