best way to construct dynamic strings/char arrays?

Working libraries, libraries being ported and related hardware
Post Reply
Nutsy
Posts: 241
Joined: Sun Jul 24, 2016 4:26 pm

best way to construct dynamic strings/char arrays?

Post by Nutsy » Sun Jul 30, 2017 1:50 pm

In maple whats the best way to construct dynamic strings and char arrays?
As you know im upgrading my speedo graphics to EVE. And i need to build some dynamic strings to show things like worded dates " Wed 15th july 2017" for instance. I need to be able to convert floats and doubles to strings and while testing in arduino ive discovered its really rather difficult to do as much of the string based libraries are missing.

Do I have the same limitations within libmaple?

What is the best course for me?

User avatar
RogerClark
Posts: 7443
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: best way to construct dynamic strings/char arrays?

Post by RogerClark » Sun Jul 30, 2017 9:39 pm

AFIK there is only one way to allocate dynamic chunks of memory, and that's to use malloc()

However, do you really need to create it dynamically?

I have seen implementations of the String functions which allocate a fixed amount of memory on the stack, in the String constructor.

I.e you can create multiple Strings but each has a maximum size e.g. Of perhaps 128 characters, depending on the value defined in the String constructors local array size

User avatar
mrburnette
Posts: 1877
Joined: Mon Apr 27, 2015 12:50 pm
Location: Greater Atlanta
Contact:

Re: best way to construct dynamic strings/char arrays?

Post by mrburnette » Mon Jul 31, 2017 2:26 am

Nutsy wrote:
Sun Jul 30, 2017 1:50 pm
<...>
i need to build some dynamic strings to show things like worded dates " Wed 15th july 2017" for instance. I need to be able to convert floats and doubles to strings and while testing in arduino ive discovered its really rather difficult to do as much of the string based libraries are missing.
<...>
Nutsy,

Were you on an official supported Arduino "core", the string.reserve(size) function is available to "pre-allocate" string memory max. I have actually never needed to use string on STM32; rather you can often abstract your output needs by using the streaming.h macro:

Code: Select all

#include <Streaming.h>  // http://arduiniana.org/libraries/streaming/  Documents\Arduino\libraries\Streaming (legacy)
Example:

Code: Select all

    Serial.print("Temp/Pres:\t");
    Serial << _FLOAT((temperature * 9.0 / 5.0) + 32.0 , 1) << "F   \t";
    Serial << _FLOAT((pressure / 100.0) , 2) << " hPa\t";
    Serial << _FLOAT((pressure / mmhg_conversion) , 2) << " inHg" << endl;
I have argued in Arduino.cc that using strings was acceptable (for newbies) and the forum should accept user solutions that utilizes any official command or function. However, I have argued in this forum that users should be advanced; therefore, using Arduino style strings is ill-advised even if they existed, which I believe they do not, but I may be wrong.

Ray

User avatar
RogerClark
Posts: 7443
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: best way to construct dynamic strings/char arrays?

Post by RogerClark » Mon Jul 31, 2017 3:05 am

An additional note on converting doubles and floats to strings.

I think this can be somewhat problematic as most cores (libmaple included) (and I think AVR) do not include full sprintf / printf functionality so if you use %f or %g in sprintf, I don't think you get anything at all.

Perhaps AVR's String handling as additional functions to do the conversion.

And..
Like Rays says, using "String" is not ideal for embedded programming.

I normally allocate a buffer (either globally or locally) and use sprintf , rather than using String
(This applies to all my embedded development not just to Arduino)

Nutsy
Posts: 241
Joined: Sun Jul 24, 2016 4:26 pm

Re: best way to construct dynamic strings/char arrays?

Post by Nutsy » Mon Jul 31, 2017 12:12 pm

Yeah i realise that memory limits is the real reason why strings arent widely supported. And yes %f doesnt work with sprintf, just get a ? as a result .

I was also told I should use snprintf rather than sprintf, not sure of the reasons why.

So building into allocated char arrays is the done thing? :) Thats fine.

Still need to work out how to take numbers into the chars :p

stevestrong
Posts: 1813
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: best way to construct dynamic strings/char arrays?

Post by stevestrong » Mon Jul 31, 2017 12:50 pm

itoa, utoa, ltoa ?
ftoa:
https://forum.arduino.cc/index.php?topic=63721.0
or
http://forum.arduino.cc/index.php?topic ... #msg276209

Don't know whether GCC has something like this:

Code: Select all

#include<stdlib.h>
dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER);
AVR lib source code:
https://github.com/vancegroup-mirrors/a ... strf.c#L49
https://github.com/vancegroup-mirrors/a ... _prf.c#L37

Nutsy
Posts: 241
Joined: Sun Jul 24, 2016 4:26 pm

Re: best way to construct dynamic strings/char arrays?

Post by Nutsy » Mon Jul 31, 2017 2:40 pm

did find this method which is a bit long winded...




char tmpSign = (foo < 0) ? "-" : "";
float tmpVal = (foo < 0) ? -foo : foo;
int tmpInt1 = tmpVal; // Get the integer (678).
float tmpFrac = tmpVal - tmpInt1; // Get fraction (0.0123).
int tmpInt2 = trunc(tmpFrac * 100); // Turn into integer (123).

snprintf (bah, sizeof(bah), "foo: %s%d.%02d", tmpSign, tmpInt1, tmpInt2);


dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER); works if I just want to display the float on its own, but I dont know how to place it as part of a line of text... IE "This line of text means X=(insert float value here)"

stevestrong
Posts: 1813
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: best way to construct dynamic strings/char arrays?

Post by stevestrong » Mon Jul 31, 2017 5:25 pm

The one method would be the one suggested by Roger, using sprintf, I used to use it as very often.

The other one is the method suggested by Ray, using "Streaming.h". Also am using it often lately because of its simplicity.

Why don't you like any of them?


Post Reply