0

I am working on an arduino mega 2560 sketch. I have to parse http query strings.

My program is concatenating strings for each http request. And i have an http request every second during all day.

I have read that this kind of work may cause ram memory fragmentation. I am very upset about this because my program will randomly crash if there are no free ram memory.

Do you know if there is a way to initialise a string with a given memory length ?

I do not want to work with char arrays because i need indexof, substring functions and comparison with ==

Thanks

2
  • And how many of those concatenated requests do you expect to fit into the memory??? – AnT May 11 '19 at 14:57
  • 1
    What is wrong with using the char* functions provided by C? They can do everything a String can, without the problem of memory fragmentation, and if you need a function that does not exist, it's very easy to find an implementation or create it yourself. – Michel Keijzers May 11 '19 at 14:58
1

There is a way to initialize an Arduino String with a given memory length: see reserve() method. It will generally greatly reduce or entirely eliminate memory fragmentation, if you can afford reserving a sufficient amount of memory in advance.

Just keep in mind that this is a "soft" boundary: once your string gets longer than the reserved amount, String will quietly resume reallocating memory with all those typical consequences.

It is not clear what you plan to do if the pre-reserved amount of memory proves to be insufficient. Fall back to "memory fragmentation"? Anything more elaborate than that will be pretty much as complex as performing manual management of a char[] array.

All the functionalities you requested are present for char[] arrays as well, might just require a few more lines of code. And it is not clear why one might "need" comparisons to be == specifically.

This might also be an XY problem. Why are you trying to concatenate all these requests in memory? What are you planning to do with them once you are done "concatenating"?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.