Pages: 1 ... 3 4 [5] 6   Go Down
Author Topic: The HATRED for String objects - "To String, or not to String"  (Read 12636 times)
0 Members and 1 Guest are viewing this topic.
Global Moderator
Online Online
Brattain Member
*****
Karma: 506
Posts: 19117
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Re-entrant would also be where an ISR calls a function.

Say you are in the middle of a Serial.print. A timer interrupt fires. Inside the timer you do a Serial.print (a bad idea, I know). Then the Serial.print function is "re entered". And say inside Serial.print it keeps a variable (eg. for working out how to convert a float). That variable is now changed by the re-entry. So the original one (the one that got interrupted) is now corrupted.

This is another reason why you should keep interrupt service routines short. And why those people that try to work around Serial.print "not working" inside an ISR by adding a line to turn interrupts on, are really shooting themselves in the foot.
Logged


Switzerland
Offline Offline
Newbie
*
Karma: 0
Posts: 1
Arduino and Android goes well together
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Regarding the String class, there is actually a method 'reserve()' that was added with 1.0.

Strange enough totally undocumented on http://arduino.cc/en/Reference/StringObject, but available: http://code.google.com/p/arduino/issues/detail?id=449

I assume with this many dynamic mem allocation probs should be solved if one stays within the reserved memory. And we still can use the richer api. Tested it and it solved my mem leak problem immediately.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4816
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it love of C++ String, dislike of C string or just not used to C string that drives people to accept the behind your back actions and overhead RAM bytes of C++ String?
Look at all String does to "make it easier" to manipulate text in a number-like manner. Ooooh! Power windows! Hey, turn the car on just so I can open the window! No, that's not inefficient, that's con-veeen-yence!
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9701
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it love of C++ String, dislike of C string or just not used to C string that drives people to accept the behind your back actions and overhead RAM bytes of C++ String?
Look at all String does to "make it easier" to manipulate text in a number-like manner. Ooooh! Power windows! Hey, turn the car on just so I can open the window! No, that's not inefficient, that's con-veeen-yence!

How is your world of horses, buggies, and telegrams/bag phones? C strings are difficult for non programmers like me and appear to somewhat primitive operations compared to the ease of Strings. The "old timers" just don't get it.  smiley-wink
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4816
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When you're on a horse, you don't expect a steering wheel. An MCU is not a PC.

As far as not knowing C strings, once you understand what a char array with NULL terminator the rest with few exceptions is very straightforward. Yes there is some memorizing of shortened words but strcat() means string concatenation isn't hieroglyphs. Strtok() takes a bit of study but after writing your own array parsing it's no surprise at all. In fact I'd go so far as to say that writing your own version of any of those commands and then refining/rewriting to compact code will teach not only C string but better array handling and coding in general.

The old timers get that an MCU is not a PC. Many of us have written lots of code on both. We know to avoid the traps that the inexperienced walk right into and would rather save others from wasting their time.

Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Faraday Member
**
Karma: 62
Posts: 3080
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I find C++ Strings  annoying and unreliable on any platform.   In Java I use strings  but in C++ I use C-style char arrays.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 309
Posts: 26527
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How is your world of horses, buggies, and telegrams/bag phones?
I saw a feature on TV recently about small-scale logging, using draught horses to extract the timber.
The horses could go where the tractors could not, and their impact on the environment much less, and they could work woodlands too small for ordinary commercial operations.

A reasonable analogy, I think.

The right tool/technique for the right problem.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Johannesburg, South Africa
Offline Offline
Full Member
***
Karma: 0
Posts: 180
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm fairly new to Arduino's, and C coding for that matter as well but have done PHP / MySQL / ASP / HTML coding before.

Can someone please explain to me, what to use if we shouldn't use strings? I see, in many forum posts, where people have problems they're simply being told "don't use strings", and more often than not no sample of an alternative piece of code is given for their problem so the OP in said forum thread doesn't learn anything. And, consequently other newbies don't learn anything useful either and still use strings, since most of the tutorials and forum sample code has them.


Logged

New Jersey
Offline Offline
Faraday Member
**
Karma: 70
Posts: 3743
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

C++ Strings are objects that require memory allocation routines (malloc, free) to be called when they are used. For a long time, there was a bug in the implementation of free that the arduino uses that meant that you could run out of memory, so Strings were a dangerous proposition.

More recent versions of the IDE do not have this defect, but manipulating Strings still requires that you free and allocate memory. Given that arduinos have so little RAM, there is still the possibility that you will run out of space or fragment your free memory in such a way that there isn't a big enough contiguous piece to satisfy a request. At this point, your sketch will likely fail in unexpected ways, which will be hard to debug and when you ask for help on the forums you will be told "Don't use Strings".

There's nothing inherently wrong with C++ Strings if you have the memory for them and suitable error handling for dealing with low memory conditions, but for arduino, where you don't have these things, they are not sensible, particularly if your sketch is expected to run in perpetuity. As an alternative, arrays of char let you see exactly what memory you are using without any 'sleight of hand' stuff happening behind the scenes.
Logged

Global Moderator
Online Online
Brattain Member
*****
Karma: 506
Posts: 19117
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I see, in many forum posts, where people have problems they're simply being told "don't use strings" ...

There is a terminology problem here. The posts usually say don't use "String" (which is a specific class) not "don't use strings".

Depending on your requirements you can use an array of characters using the normal C functions like strcpy and strcat. A lot of the Arduinos only have 2048 bytes of RAM, so you don't have a lot of memory for arrays, dynamically allocated strings, or anything else.

At least with manually allocated arrays you know how much memory they are taking. And for constants, you can use strings directly from program memory, eg.

Code:
Serial.println (F ("hi there"));

Quote
... where people have problems  ...

Some problems are caused by the String class, when it had bugs. Some are caused even afterwards because of memory fragmentation. Some are unrelated. However when people post a lengthy sketch, which uses String extensively (as opposed to "strings") one of the first suggestions is to rework without using the String class.

Quote
... and more often than not no sample of an alternative piece of code is given for their problem ...

I have examples below which show receiving serial data. They stores strings into static buffers but do not use "String".

http://www.gammon.com.au/forum/?id=11425
Logged


0
Offline Offline
Tesla Member
***
Karma: 145
Posts: 9701
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can someone please explain to me, what to use if we shouldn't use strings? I see, in many forum posts, where people have problems they're simply being told "don't use strings", and more often than not no sample of an alternative piece of code is given for their problem so the OP in said forum thread doesn't learn anything. And, consequently other newbies don't learn anything useful either and still use strings, since most of the tutorials and forum sample code has them.

Don't expect any of the "string" advocates to fix your code using the "string" functions, most likely due to the fact that the real code issue rarely ever has to do with using Strings. If persons can't wrap their head around the real issue, or are just stumped, they can always say "praise Jesus, drink more milk, and don't use Stings". Maybe the cure..., maybe not.
Logged

Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   smiley-cool

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't expect any of the "string" advocates to fix your code using the "string" functions

Give a man a fish...
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12630
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't expect any of the "string" advocates to fix your code using the "string" functions

... because there have been dozens, probably hundreds, of examples posted showing how to do this yourself and it is not hard.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Global Moderator
Online Online
Brattain Member
*****
Karma: 506
Posts: 19117
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

If persons can't wrap their head around the real issue, or are just stumped, they can always say "praise Jesus, drink more milk, and don't use Stings". Maybe the cure..., maybe not.

I can wrap my head around issues, thanks. When I see something obviously wrong, I mention it. If that doesn't fix it, we can move onto subtle things.

Example: If your care won't start, and it's out of petrol, that's probably the problem. Putting petrol in doesn't guarantee it will start, maybe the spark plugs are bad, but let's fix the simple stuff first.
Logged


Johannesburg, South Africa
Offline Offline
Full Member
***
Karma: 0
Posts: 180
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see, in many forum posts, where people have problems they're simply being told "don't use strings" ...

My bad..... I didn't see the Capital S in "Strings", and misread it as "strings"
Logged

Pages: 1 ... 3 4 [5] 6   Go Up
Jump to: