Memory needed to modify string (newbie)

Hi. New to c and c++. I wonder if somebody could help me with a basic question (which I realise may not be well-framed due to my lack of knowledge) please?

If I have a variable defined as

String varname;

and I ask the sketch to add more to the end of it, do I need enough free contiguous heap to store the complete revised version of the string for this to work successfully, or some other amount - (ignoring a few bytes pointer overhead). Is fragmented heap automatically defragmented under these circumstances, or is it only adjacent free spaces that are ever joined?

The variable in question has the potential to be large - several thousand or even more bytes - and needs to be sent to a browser. I didn't write the code and it's quite complex, but I could consider splitting the variable into sections if it reduced the heap overhead. I don't think it's practical to dynamically construct the whole output.

This is running on an esp8266 and afaik the standard exposed API only allows getfreeheap.

I'm wondering if the best I can do is something like

error if existing variable size plus size of lump to be added comes to more than (rule of thumb) say 30% of free heap

Is it perhaps possible to reserve a fixed size area based on a variable input at run time?

Thanks very much for any input, really appreciated. I'm sorry if these are very simplistic questions, I've read around a bit but not found a simple answer...

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

Using cstrings you should allocate space for the longest string you will need (plus an extra byte for the terminating NULL).

...R

Thanks very much Robin, very much apprecy. That fits in with my rather superficial understanding of why this is so, did some reading before I posted but got a bit bogged. Not always easy to pick out current stuff. With cstring, does all the manipulation happen inside the allocated space do you happen to know?

I'm very much in favour of keep it simple. The problem here is varied use - while some times it needs a big buffer, other times the buffer only needs to be small but the memory is really wanted elsewhere.... But robust is the absolute essential. I will try to put that in and see what happens, thanks again!

newbod:
With cstring, does all the manipulation happen inside the allocated space do you happen to know?

I'm not sure of the precise answer to that question (I think it would need clarification) but I do know that the manipulation will only happen within memory that you have allocated.

...R

Thanks very much again. I'll allocate the cstring to match a high usage, then see how much heap is left and if any of the other aspects struggle in basic testing.

It's all the unknowns that make this tricky, both the coding (my own ignorance) and the usage the device is being put to, but you've given me a big help - hopefully light at the end of the tunnel etc...

Onward!

When I use Strings, I use a string buffer or two for all the String work.

After declaring the String, use sString.reserve( SizeOfBuffer ) to create the string buffer. During the development of a project, I print out the String length, not the buffer size, to test if the buffer requires more or less reserve. I add in, at least one more byte for the end of string character and a few bytes as a fudge factor. I add to String buffer by using sString.concat and clear the buffer with a sString = "";. Works great last a long time.

Idahowalker:
Works great last a long time.

That suggests that it does not last forever :slight_smile:

Or ... the dog has not bitten you yet.

...R

Robin2:
That suggests that it does not last forever :slight_smile:

Or ... the dog has not bitten you yet.

...R

wise use of Strings do last forever, i had couple of gsm based controllers , with Strings in it , 4 years old now on site and never spot an issue

KASSIMSAMJI:
i had couple of gsm based controllers ,

Using what microprocessor?

…R

Even if you have built a system using Strings that runs flawlessly, there are known issues with them that can cause even experienced programmers problems.

Since there is no need whatsoever to use them, it seems unkind (at best) to encourage novices to do so.

Robin2:
Using what microprocessor?

...R

some run on arduino nano

others run on mega 2560

I always used to get testing done by people who didn't know what whatever it was was supposed to do.... Surprising how often that found things.... At the moment I don't know what I'm doing, so I'm really quite enjoying learning.

The String object does have the reserve() method, which allows you to explicitly set the size of the internal buffer. If you set this to accommodate the largest string you'll ever need, it will never have to allocate more memory.

Regards,
Ray L.

Thanks very much to all who posted. I need to experiment a bit to see if the maximum size of fixed buffer I can allocate can be big enough but still leaves enough space for everything else. (Think total ram is 80KB, which sounds a lot but it has to do a lot!) . The buffer currently has a max size of 4KB, so if changes to the contents involved having both the buffer and a similar size temporary copy on the heap are expensive so it's good to hear they can be avoided.

Thanks again

newbod:
Thanks very much to all who posted. I need to experiment a bit to see if the maximum size of fixed buffer I can allocate can be big enough but still leaves enough space for everything else. (Think total ram is 80KB, which sounds a lot but it has to do a lot!) . The buffer currently has a max size of 4KB, so if changes to the contents involved having both the buffer and a similar size temporary copy on the heap are expensive so it's good to hear they can be avoided.

Thanks again

just ensure your expected String size must not exceed the buffer size ( .reserve() )

use string.concat() over string += blahblah

Off to Google concat Vs += I go.... :slight_smile:

Ah... So the + causes a copy to be created but concat doesn't..

Be interesting to see how it all comes out...

newbod:
Ah... So the + causes a copy to be created but concat doesn't..

Be interesting to see how it all comes out...

No. + and concat both do exactly the same thing. + calls concat.
Regards,
Ray L.