B4R Question Adding strings

kohle

Active Member
Licensed User
Longtime User
Hi Erel,

you say that adding strings is different than in b4a
because of "lack of memory" .....

But see in Ardruino. IDE I make it like this

float t = dht.readTemperature();
sendLine = String("Temp: ")+ t + "C";

Why it cant be like in b4a :

sendLine = "Temp: " & t & "C"

there should be a way your compiler/translater convert to the C code.
I dont see a different of memory usage.

Why, like this ?

Dim c As String = JoinStrings(Array As String ......

Maybe allow both options and the developer decide , depending on the memory he has for use.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
I dont see a different of memory usage.
You need to first understand how strings are built and how the micro controller organizes the memory to fully understand the reasons why appending strings is problematic and should be avoided.

There are many discussions in the Arduino forum about the pitfalls of using the String object (which B4R doesn't use it all). There are also many discussions related to the issues with dynamic memory allocations and heap fragmentation which will happen when you use the String object.

In almost all of the cases it is a mistake to create a large string and then send it. You should instead call AStream.Write multiple times. In the rare case where you do need to append strings then you should use JoinStrings which is more efficient than the code you posted above.

B4R is a RAD tool. It allows developers to quickly and (relatively) easily build real-world, powerful and reliable solutions with zero memory leaks.
After a lot of research I decided against adding support for the concatenation operator as it is too "dangerous" to use in a small micro controller with tiny memory and no proper memory management.
 
Last edited:
Upvote 0
Top