0

I'm trying to parse with tinyxml2 a modest XML (3.4kb) to retrieve some data.

My ESP8266 crashes every time I try to use the toCharArray method.

here's my code

// here I'm getting an http request
String payload = https.getString();
// checking the payload, it seems fine in console but I get some garbage characters when àèéùìò are printed
Serial.println(payload);
// Length (with one extra character for the null terminator)
int str_len = payload.length() + 1;
// Prepare the character array
char xml[str_len];
// Copy it over
payload.toCharArray(xml, str_len);
// there I'll parse xml with tinyxml2 but the MCU crashes before getting here

the length of the string is something around 3900, which I'm not sure can fit this kind of array on this device. If it's the case, what will be a better strategy ?

As you can imagine I'm just starting with the c language :)

EDIT: https.getString().c_str() + less variables was the right way to save memory

asked Apr 25, 2019 at 22:35
2
  • variables declared inside functions are created on the stack, the total size of these variables is 4kb - 3900 is getting pretty close - you'll want to use malloc (and free) to use heap memory instead Commented Apr 25, 2019 at 22:44
  • another option is, if you know the absolute maximum payload could be, create a global buffer to use Commented Apr 25, 2019 at 22:45

1 Answer 1

0

Function local variable are created on the stack, and in the case of an ESP8266, the total size is limited to 4096 Bytes - so, 3900 for one variable is cutting it close.

I would recommend you either use malloc/free

int str_len = payload.length() + 1;
// Prepare the character array
char *xml = (char *) malloc(str_len);
// Copy it over
payload.toCharArray(xml, str_len);
// there I'll parse xml with tinyxml2 but the MCU crashes before getting here
//
// when done, free the memory malloc'd
free(xml);

or, if you know an absolute maximum size for the string, e.g. 5000, make char xml[5001]; in the global scope

answered Apr 26, 2019 at 0:19
7
  • tried declaring a char with a size of 6000 in global and it reboots <<<stack<<< ets Jan 8 2013,rst cause:2, boot mode:(3,6) load 0x4010f000, len 1384, room 16 tail 8 chksum 0x2d csum 0x2d v951aeffa, the size of the string is ~5700 Commented Apr 26, 2019 at 6:28
  • got a compile error with malloc -> src/main.cpp:74:35: error: invalid conversion from 'void*' to 'char*' [-fpermissive] Commented Apr 26, 2019 at 6:29
  • yeah, sorry was in a rush - hang on - there, fixed Commented Apr 26, 2019 at 7:03
  • tried declaring a char with a size of 6000 in global and it reboots - really? that does seem odd Commented Apr 26, 2019 at 7:05
  • still no luck (event with malloc, which seems to work fine) but I think the problem could be elsewhere. For the sake of documentation I'll post the gist of the main file -> gist.github.com/leRenart/1b72db23371839fd5a7799ece63ad8ed Commented Apr 26, 2019 at 19:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.