Pages: [1] 2   Go Down
Author Topic: Strings larger than 256 bytes?  (Read 1062 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it possible to have strings larger than 256 bytes?

I would like to have something like longstring and not an array.

Thanks smiley
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13477
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

yes, you can implement a class that does uses long strings. Look at the code of the Arduino String class and with some minor tweaks it should be able to support longer strings.

Internally it still will be a char-array I guess, (or an array of char array's)

Problem with most string classes is (depends on implementation of course) that it fragments the memory because of continuous alloc/free actions.

Most of the applications that need long strings can be redesigned to work with smaller strings without the user noticing it.

Can you explain why your application needs long strings?

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Arduino String can be larger than 256 characters.  What are you doing that leads you to believe they can't?
Logged


Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Arduino String can be larger than 256 characters.  What are you doing that leads you to believe they can't?
Keep in mind, though, that that one string is going to eat up 1/8 of the memory available on a UNO.
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Arduino String can be larger than 256 characters.  What are you doing that leads you to believe they can't?
Keep in mind, though, that that one string is going to eat up 1/8 of the memory available on a UNO.

That's true, but so would any other custom string class.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you explain why your application needs long strings?

It's for my XELFA library that I mentioned earlier today in another post.
Basically what I need to do is to loop through a strings characters. As long as the string is 256 bytes long or less its ok,
but as soon as it exceeds 256 bytes the loop returns no character.
And thats when I'm using the brackets like: MyString

I'm experimenting a bit with it right now to see if I can find some simple code.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13477
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
asically what I need to do is to loop through a strings characters.

Please post this code as there is something strange - Is the index used in the for loop an uint8_t aka byte?

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A quick perusal of your library shows a few things...

You're allocating a char buffer of 256 bytes.

You have a bunch of static strings in your code.  All these strings consume SRAM when the Arduino boots up, that's just the way the compiler handles them.

You're making heavy use of the String class, which can very quickly fragment the limited amount of SRAM available on the Uno.

One quick test to try out, eliminate or drastically cut down the size of your inData buffer.  That alone is one quick method of freeing up SRAM.  Then see what you String size limitation is.  If it's larger than 256 bytes at that point, then you're just running out of SRAM.
Logged


Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Strings have the advantage that you quickly get a new copy.
How do you catch a bad_alloc exception?

Don't use String in situations where you don't know exactly what's happening.
( means: Use them only in situations when you could do as well without them )

Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Strings have the advantage that you quickly get a new copy.
How do you catch a bad_alloc exception?

Don't use String in situations where you don't know exactly what's happening.
( means: Use them only in situations when you could do as well without them )



To my knowledge, exceptions aren't available on the AVR implementation of C/C++ (likely due to the AVRs limited memory spaces).  malloc failures result in a null buffer.  Most of the String library operations silently abort on null buffers, but if you utilize the reserve method prior to said operations, it returns a zero or one based on the result of malloc.  This would, of course, require you to determine how much memory is needed for the desired operation.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok I've narrowed down my problem.

From my experiments I can gladly say that theres no problem getting characters from long string.
You can either use brackets like String[](will return as a char), the String.substring() function or use String(String[]).
Any of them work fine.

And for the problem that I'm having I suggest you to visit the dedicated blog for my library so you can fully understand its string handling functions, visit xelfa.blogspot.com.

Ok, so XELFA have 2 functions that are used by the problem.
One function, the CountStr(), works fine by entering any size String.
However the CountListItems, which basically calls CountStr and results the same value as CountStr plus 1 (CountStr+1)
does not seem to receive the first parameter, unless the string is short.

Try this:
Take the following super long string:
Code:
String MyValues = "00,0,0|100,26,0|200,28,0|300,29,0|400,30,0|500,32,0|600,31,0|700,32,0|800,34,0|900,37,0|1000,39,0|1100,41,0|1200,43,0|1300,46,0|1400,49,0|1500,52,0|1600,56,0|1700,60,0|1800,65,0|1900,71,0|2000,80,0|2100,88,0|2200,101,0|2300,114,0|2400,141,0|2500,180,0|2600,180,8|2700,180,18|2800,180,27|2900,180,36|3000,180,46|3100,180,57|3200,180,68|3300,180,77|3400,180,87|3500,180,97|3600,179,109|3700,155,109|3800,134,109|3900,117,109|4000,102,109|4100,88,109|4200,76,109|4300,66,109|4400,57,109|4500,50,109|4600,41,109|4700,35,109|4800,27,109|4900,21,109|5000,17,109|5100,10,109|5200,4,109|5300,0,115|5400,0,136|5500,0,170|5600,0,240|5700,-12,240|5800,-24,240|5900,-37,240|6000,-50,240|6100,-63,240|6200,-76,240|6300,-90,240|6400,-105,240|6500,-118,240|6600,-134,240|6700,-148,240|6800,-164,240|6900,-181,240|7000,-199,240|7100,-212,240|7200,-236,240|7300,-255,230|7400,-255,201|7500,-255,179|7600,-255,163|7700,-255,140|7800,-255,119|7900,-255,99|8000,-255,82|8100,-255,71|8200,-255,61|8300,-255,49"

And run this code:
Code:
Serial.println(String(XELFA.CountStr(MyValues,"|")));
Works like a charm, no matter how long the string is.

Now try to use the same string but with the following code:
Code:
Serial.println(String(XELFA.CountListItems(MyValues,"|")));
It wont work, unless the string is short.

The two functions look like this:

Code:
int XELFA::CountStr(String Str, String SubStr){
  //WORKING. DO NOT TOUCH!

  return FindStrPass(Str, SubStr, -1);
}


int XELFA::CountListItems(String List, String Spacer){
  int Count = CountStr(List, Spacer)+1;
  if (List=="") return 0;
  return Count;
}

Maybe I should use pointers instead of creating a bunch of new strings all the time?!

Btw I have an Arduino Mega.
« Last Edit: May 03, 2012, 04:40:25 pm by aid2012 » Logged

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you're sure that CountStr is fine, the problem is perhaps in

 (List == "")

which is not as trivial as it might look to you.

(List.length() == 0) might be a lot easier
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13477
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
String MyValues = "00,0,0|100,26,0 ... "
Serious?

This is where I would use an integer array, this is definitely not the way to program an Arduino imho.

Even if the data is send from a PC or sensor in this format, I would not store it in a string but convert it as fast as possible into an appropiate data array.

(Still I like the list functions, e.g. for Menu options)

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2975
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(Still I like the list functions, e.g. for Menu options)
... as a PROGMEM array of const char const * to PROGMEM ...
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13477
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
.. as a PROGMEM array of const char const * to PROGMEM ...
yep, exactly...
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Pages: [1] 2   Go Up
Jump to: