string overwriting

When strings are overwritten, such as input from the serial line, does the old string stay in memory, or is it deleted? I've googled some, but no one seems to know the answer.

Can you show us a test sketch.
Normal array of characters, or the String class ?

String class, although I might be able to switch.

Here's a sketch that illustrates the question.
What I want to know is, will every value that input has ever contained stay in the SRAM, or will previous contents be deleted as soon as more traffic comes in?

String input;

void setup(){
  Serial.begin(9600);
}

void loop(){
if(Serial.available()>0){
  input = Serial.read();
}

It depends if you delete the object, and create a new one, or if you modify the exising object.

If the class is properly implemented, the space occupied by the deleted data should eventually be re-used. But that depends on the quality of the compiler and the class library. In Java, that is an inherent part of the language, but in C/C++, it depends upon the implementation.

It has been a general recommendation for a long time, that it is better to avoid the String class. There were some shortcomings in the implementation at one stage ( a few years ago ), I am uncertain if it has been completely resolved.

Even if there are no bugs in it, using the String class is a fairly profilgate use of available ram.

Alright, I'll switch over to char arrays. I don't really need any string functions anyhow.
Thanks

farTooManyWires:
Here's a sketch that illustrates the question.
What I want to know is, will every value that input has ever contained stay in the SRAM, or will previous contents be deleted as soon as more traffic comes in?

In the scenario you have, yes.

input = Serial.read();

Serial.read() grabs a single character, then you assign it to a string.

You will have to append it:

input = input + ( char ) Serial.read();

//Or

input += ( char ) Serial.read();

To minimize the allocations done, you can also pad out input to an expected size.

input.reserve( 15 );

farTooManyWires:
Alright, I’ll switch over to char arrays. I don’t really need any string functions anyhow.
Thanks

With char array you put the read character in at the index you intend.
To add characters one after the other, just increment the index each time.
Be sure to not write more characters than you have array and be sure to leave room for a 0 at the end.

The array is more direct, more open, and does not copy itself on change then delete the original as String does.

I had to edit my post above, a slight error:

I added a cast to char on Serial.read(), as it returns an int. What would happen is the received char ‘a’ would be added to the String as the integer “97”. The cast makes the String library add it as a single character.

This and the many allocations are reasons to use a DIY method. Where as
*__ <strong>*arr[i] = Serial.read();*</strong> __*
would work without a cast.