The forum is full of posts why the String should be avoided at all costs and instead all should love the char array.
If that be the case then why is it that almost all code examples for things like web server etc use the String right through ... is there no other " better " option ??
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += "<head>";
s += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
s += "<meta http-equiv=\"refresh\" content=\"60\" />";
s += "<script src=\"https://code.jquery.com/jquery-2.1.3.min.js\"></script>";
s += "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">";
s += "<style>body{font-size: 24px;} .voffset {margin-top: 30px;}</style>";
s += "</head>";
Ardubit:
The forum is full of posts why the String should be avoided at all costs and instead all should love the char array.
If that be the case then why is it that almost all code examples for things like web server etc use the String right through ... is there no other " better " option ??
the examples you see just make it easier for the entry-level beginner to understand without adding the confusing complexity and syntax of C strings compared to ease of String manipulation (after all, there is a reason to write such a class!).
The String over-hype is mostly just parrots echoing what is good common sense in embedded programming.
why is it that almost all code examples for things like web server etc use the String
Because anybody can make an example. Very few people can make a good example.
is there no other " better " option ??
Yes, but it takes more time to learn the other option(s). Knowing how to use the standard C string functions is much harder than looking at the String class header. IMO, spending time at the beginning to learn about C strings is much more enjoyable than spending time wondering why a sketch using String crashes at random times or only runs for a limited time.
For your code snippet, you don't say what the string is used for, but you can compare these two options:
With the String class:
#define client Serial
void setup()
{
Serial.begin( 9600 );
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
s += "<head>";
s += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">";
s += "<meta http-equiv=\"refresh\" content=\"60\" />";
s += "<script src=\"https://code.jquery.com/jquery-2.1.3.min.js\"></script>";
s += "<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">";
s += "<style>body{font-size: 24px;} .voffset {margin-top: 30px;}</style>";
s += "</head>";
client.print( s );
}
void loop() {}
The String class program uses 4194 bytes of program space and 620 bytes of RAM, plus ~400 bytes of the heap (aka "dynamic memory", which uses additional RAM). Depending on how these Strings are used in the larger program, it probably has long-term stability concerns.
The C string program uses 2442 bytes of program space and 182 bytes of RAM (for the core). Rock solid. No RAM is needed to hold on to that HTML text.
The C string program uses 2442 bytes of program space and 182 bytes of RAM (for the core). Rock solid. No RAM is needed to hold on to that HTML text.
Seems like an easy choice...
You hit the nail on the head ! And going by the responses so far, looks like I touched a raw nerve for many.. In fact after reading through your example ( thanks for taking the time to do it ) I really wonder why the String class was used at all in this case.
Of course doing it other language may ease up things but then THAT language has to be learnt ...
6v6gt:
Without getting into the discussion about whether Strings are good or not:
Many of the classes in the ESP8266 Arduino core use the String class so you are more or less stuck with it.
I've noted that too, especially in the various web server classes. Given that, could something like the following be a "safer" use of the String class?
My thinking is: If only one String object is used at a time and it's deleted immediately after use, then the chance of memory fragmentation / leak is reduced.