I am trying to dynamically allocate memory for a char pointer stored in the flash memory.
I need to read a file from flash memory using LittleFS file system and copy it to a character array which also needs to be stored in the flash memory using PROGMEM. I cannot store it in the RAM because of limited space. Also I cannot hardcode the character array directly in the sketch, because I need the ability to change the file during runtime and have it persist after reboot.
If I don't use PROGMEM for the char pointer, the sketch works fine. But adding PROGMEM causes the ESP8266 to throw an exception and reboot and become an infinite loop. This is a simplified sketch showing what I'm trying to achieve.
#include "LittleFS.h"
char* arr PROGMEM;
void setup() {
Serial.begin(115200);
LittleFS.begin();
File file = LittleFS.open("/test.txt","r");
arr = (char*)malloc(file.size()+sizeof(char));
int len = file.size()/sizeof(char);
for(int i = 0; i < len; i++){
arr[i] = file.read();
}
arr[len] = '\0';
while(1){
Serial.println(arr);
delay(1000);
}
}
void loop(){}
It is for the WifiClientSecure library. The class is BearSSL::X509List. To create an object for this class I need to send a char pointer as argument to the constructor. The char pointer should contain address to the SSL certificate string. I have my SSL certificate in the sketch data folder as "ca.crt".