Arduino resets when "too many" stream operators (<<) are used inline

I'm using Streaming.h which adds the very handy << operator to the Serial class.

Serial << "foo" << _DEC(a) << "bar" << _FLOAT(b) << endl;

vs.

Serial.print("foo");
Serial.print(a, DEC);
Serial.print("bar");
Serial.print(b, FLOAT);
Serial.println();

Well I guess I got carried away. If I used "too many" of the << chained together the code would compile but the Arduino would reset. Simply splitting the long chain into two or more separate lines seems to solve the problem.

This might fail:

 Serial << "a" << "b" ....... << "y" << "z";

whereas this might pass:

 Serial << "a" << "b" ..... "m";
Serial << "n" .... << "y" << "z";

Sorry for the vagueness. It seems to be dependent both on the size of the variables as well as the number of chained << operators.

Which begs the question - why is it failing?

Well, my guess is that the 'duino is running out of stack memory.

My question is what limits stack size and how does one know that limit and program defensively?

Or is this not a stack issue at all?

Any insight gratefully received!

Thanks.

my guess is that the 'duino is running out of stack memory.

I'd say that's right.

what limits stack size

Avaliable RAM and how much the heap is used. String uses the heap a lot a I think so that won't help and on the mega328 you only have 2k to start with.

how does one know that limit

There's a function around somewhere that will give you the free memory. Can't remember what/where, maybe search for "freemem"

program defensively

Don't use String, much as I like the << operator and have used it I think String is too heavy for a little Arduino.


Rob

Thanks Rob. Both for the advice and the best damn "geek quote" I've seen in a while (2B OR NOT 2B....) I'm still laughing!