Max size for String constuctors.

Hi

I have using an Arduino Uno + Ethernet Shield. The arduino will need to send some SOAP messages to a webservice. I have everything working except for one problem with the size of String that can be constructed and concated....

The code that fails is below.

String SH1,SH2;

// SOAP Envelope
SH1 = String("<?xml version=\"1.0\" encoding=\"UTF-8\"?> <S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">  <S:Header/>  <S:Body> <ns2:postSensorData xmlns:ns2=\"http://scc/\">  <SData>  ");
SH2 = String("</SData></ns2:postSensorData> </S:Body> </S:Envelope>");
// Data for between SData tags
SH3 = String("abcefeghijklmnopqrstuvwxyzabcefeghijklmnopqrstuvwxyzabcefeghijklmnopqrstuvwxyzabcefeghijklmnopqrstuvwxyz");

// Create Full SOAP message
SH1 += SH3;
SH1 += SH2;

// Send to serial to check
Serial.println(SH1);

I get very inconsistent results for the concat operation (+=) - it seems to be related to the sizes of the SH3 string. If its empty or small, its seems to work....

Any help really appreciated.

If its empty or small, its seems to work....

Well, there is a clue to your problem. Make it small and use the FreeMemory function (yes, it's google time). See how much free memory you have. Then, make it big, and see how much you have. 0 is not a good number.

String is not really intended for what you are doing. Using fixed sized char arrays that you index properly will provide better results, because String fragments memory badly. Every time it adds a character, it needs to allocate space for the new String.

IMO using String is generally a bad idea because of the dynamic memory allocation it uses, which leads to varying and unpredictable RAM usage on a system that has only 2K of RAM to begin with. Also, when you take a string literal and then build a String from it, you use more than double the RAM that you need just for the string literal. It looks like most of your string data is constant and could be sent to the serial port directly using print(). Even better, with a little more effort you can store your string literals in PROGMEM and print them direct from there, avoiding storing them in RAM completely.

Thanks. I will move the strings into progmem and see how I go.

Indeed most of the header and envelope is static.