The Use of += or +

Hey up,

Here was an odd thing,
during some experimenting I find this.
Obviously not real code.
Just to show the use of + and +=

Serial.print(F("HelloGoodbye"));

//Change that to 
Serial.print(F("Hello") + F("Goodbye"));

//Change that to 
Serial.print(F("Hello"));
Serial.print(F("Goodbye"));


//Change that to 
String x = F("Hello") + F("Goodbye");
Serial.print(x);


//Change that to 
String x = F("Hello");
x += F("Goodbye");
Serial.print(x);

All produce slightly different sketch outputs.

amazing how something that looks shorter can produce longer output.
No chance of that with assembler, WYSIWYG.

Hv.

I am not a computer science major.

+= is used for arithmetic

in the case of + when used with strings, polymorphism comes into play and + becomes concatenation of strings.

Note that the use of String class can lead to fragmented memory.

the += "appears" to work on strings too.
But that maybe the compiler being friendly.

Actually it gives the smaller output oddly.
unlike bla + bla + Bla.

Yep, STRINGS are BAD.

But hey, so is jumping in front of a Bus, but it's just so easy.!
I use both, Strings and chars. they don't come lazier than me.

I stopped messing with Busses, perhaps I will do the same with Strings one day.

String is an object/struct used to handle array of char. A C(++) object/struct may be extended with operators such as "+", "-", "+=" and so on in order to attach a function to handle the operators. So "+=" is not necessary an arithmetic operator, but it may work for struct and classes as well.

Would be nice to know, what methods produces which output :slight_smile:

//This produced a larger output 526575b
//  retdate += (String) s.substring(6, 8) + T_space + s.substring(0, 4) + T_space + s.substring(8, 10) + T_colon + s.substring(10, 12) + F(":00");

//This produced a SMALLER output 526543b
  retdate += s.substring(6, 8);   retdate += T_space ;
  retdate += s.substring(0, 4);   retdate += T_space;
  retdate += s.substring(8, 10);  retdate += T_colon;
  retdate += s.substring(10, 12); retdate += T_colon;
  retdate += T_n0;  retdate += T_n0;

That was the Only change.

I find using Bla + Bla saves on Line space but makes bigger output.!

If you by "bigger output" mean a larger compiled program size or larger memory usage, then it is obvious that multiple smaller assignments would require less than one larger assignement since the space / memory used by smaller assignemt can be reused by subsequent assignments. Anyhow, you should avoid to use String in arduino, so why bother at all? :wink:

Not bothered to look at the Memory usage difference.

Just the sketch size. Dynamic Memory remained unchanged.

I will no doubt have a play later and compare on a bare project.

Re Strings, I like living on the edge, without the fear of flat toes or death.
Strings suit me cos I am LAZY.!

Hv.

What they call a working example.!
About as small as they get.

#define portspeed  115200

void ICACHE_FLASH_ATTR setup(void) {
  Serial.begin(portspeed);
  while (!Serial) {    yield();  }
}
void loop(void){

// Sketch Size = 249851
//Serial.print(F("HelloGoodbye"));

//Change that to 
// Sketch Size = 250779
//Serial.print((String)F("Hello") + F("Goodbye"));

//Change that to 
// Sketch Size = 248867 <<< SMALLEST
//Serial.print(F("Hello"));
//Serial.print(F("Goodbye"));

//Change that to 
// Sketch Size = 250779
//String x = (String)F("Hello") + F("Goodbye");
//Serial.print(x);

//Change that to 
// Sketch Size = 250459
String x = F("Hello");
x += F("Goodbye");
Serial.print(x);

}

Hiddenvision:
Just the sketch size. Dynamic Memory remained unchanged.

Because the Strings go in the heap. They're not counted in that number you get when you compile. Only the pointers to them.

Hiddenvision:
Strings suit me cos I am LAZY.!

I know you think that. But I promise you this, if you code with Arduino very much you are going to run into lots of libraries that won't take Strings and you'll spend far more effort trying to jam a String in there than you would learning that everything you do with String has an equivalent with char arrays you just have to learn a different function name.

Plus the whole thing with random crashes.

Hiddenvision:
Re Strings, I like living on the edge, without the fear of flat toes or death.
Strings suit me cos I am LAZY.!

Delta_G:
I know you think that.

I think Hiddenvision was being sarcastic.

You don't need capital-S Strings in order to concatenate text.
That's what strcat() is for.

Hey Odometer,

Sarcasam, who me...?

But seriously I don't know what is the best method, that's why I was posing the scenario.

There is Strcat() Concat(), dammcat() and coolcat() to mention but a few.!
Not to mention the various ways to define the actual 'text' in the first place.

I am tinkering, so will find my answers in time if only thru process of elimination.

Also thinking, rather than everyone bitching about how strings are "BAD",
why has nobody re-written the Strings Class to improve it.!

In real terms it should not matter how things are written,
code should compile down to the same output if the intention is the same.
Sorry, I'm too used to assembler, all this nasty C stuff does it best though.

Hiddenvision:
Also thinking, rather than everyone bitching about how strings are "BAD",
why has nobody re-written the Strings Class to improve it.!

Because it isn't bad for bad code. It works great on systems with more memory and/or garbage collection to clean up the leftover bits when you concatenate Strings. The code is good. It's just the Arduino that doesn't quite have what it takes to really use it.

Hiddenvision:
In real terms it should not matter how things are written,
code should compile down to the same output if the intention is the same.

That's just crazy-talk. How is a compiler supposed to know your intention? All it sees is the code you write. If you tell it to do something the hard way how is it supposed to know that you didn't do that on purpose?

ieee488:
I am not a computer science major.

+= is used for arithmetic

in the case of + when used with strings, polymorphism comes into play and + becomes concatenation of strings.

Or for Arduino String objects where that particular function is implemented for string concatenation.

Hmm. If you overload "operator+" to handle string concatenation, does that get used for "+=" as well, or do you have to provide a separate overload of "operator+="?

Is the OP going to give us hints as to what these "different outputs" actually are?

Serial.print(F("Hello") + F("Goodbye"));

String x = F("Hello") + F("Goodbye");

Those don't even compile, do they? You can't add two pointers together, and there's nothing to tell the compiler that it should convert them to Strings just so that the add will make sense...

String x = F("Hello");

x += F("Goodbye");

And that one's a bit ridiculous: "I will save RAM by putting my string literals in flash, and then I will waste RAM by converting them to Strings so that I can concatenate them!"

Serial.print(F("HelloGoodbye"));

Serial.print(F("Hello"));
Serial.print(F("Goodbye"));

Those should be the same, though...

westfw:
Hmm. If you overload "operator+" to handle string concatenation, does that get used for "+=" as well, or do you have to provide a separate overload of "operator+="?

"+" and "+=" are two different operators so each must be implemented on their own.

westfw:
Those should be the same, though...

Nope, you have two calls to F() in the latter and that will result in larger code than a single call. Reason being that F() is a macro and a macro is basically code that must be copied by the compiler and more code = bigger program. :slight_smile:

i notice you have used (F("blah"))(F("blah"))
what is the f for?

devcoder:
i notice you have used (F("blah"))

(F("blah"))

what is the f for?

It's amazing what Google finds Arduino Playground - Memory