Why we should not use the String object of arduino??

When using sd lib, I ran into problem of con char* and string problem in sd.println(), the compiler told me it's not right to write a string....

I have a function which assemble some strings and return back a string. let say String form-string(void);
then I tried
    myFile = SD.open("test.txt", FILE_WRITE);
  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
   myFile.println(form_string());
    // close the file:
   myFile.close();

The error is that it says it need "const char *" not a "string"...
I was so so confused....

I need to go deeper in the string and char array....google it....
turns out that lot of people talk about not to using arduino String objects, saying it's bad, and wast more time and space....well, today we can have powerful and big MCU then before with same cost, I don't really think this is a problem...Until I read this article....

The is the one every one should read:
https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/

The problem is at the memory level of MCU,memory allocation.In native C, there is no string like arduino, string is a char array with last element as NULL "\0". So the char array is always declair with it's size and take a space in flash when complied.
when you use arduino String, you don't need to declair the size of it, and when you use it, it ask memory management to allocate a place in RAM for it, when you change the content of the string, it will ask for a bigger memory, and leave the original place. after several operation(or if you do a lot of string operation), the heap memory start to get holes, and possible run out of memory, or become unstable.

So, a good lib will not allow passing string to it, and you have to pass a pointer of an char array, if the char array does not change in the function, it will as for const char *array.

Then I have question on & and * before a variable, I read & is the address, and * is the pointer....what's the difference? it seems & is for variable...and * is the start address of an array.
so if we have:
                                           char str[4]={'a','b','c','\0'};
which is the same as           char str[]= "abc";
the second one take 3+1 as it's length.
str is the name of the array, and it also represent the address of first element,
i.e. str == &str[0]
and the *? 
It's a pointer point to an address, and pointer can be a variable.
let say i is an int, *ptr is a pointer point to i's address...
int *ptr;
int i=100;
ptr = &i;
so, ptr is the address of i, if we want to take contain of i, we need to use *ptr

So, the best way is to declair a char array, and pass the  point variable(*str) to the function.
then manipulate the array.
I will need to get int, float to char array with C string library function(string.h and ctype.h)
few I noted :
strlen(s) is the length of string but not include "\0"
strcpy(t,s) copy s string to t
strcat (t,s) append s to t
strncpy (t,s,n) only copy the first n char from s to t

to get int into char array,
itoa(i,array,base);// i is the int, array is the buffer, base  can be 10 or 16 or 2.


For float to char array,
char *   dtostrf (double __val, signed char __width, unsigned char __prec, char *__s)
seems handy, but in my test, it lost precision for 4 and below...

留言

這個網誌中的熱門文章

Heltec ESP32+OLED+Lora, hardware testing

micro SD card for ESP32, on lolin32 with OLED and heltec 32 lora oled

Install Network Time Protocol(NTP) on BeagleBone with Angstrom linux and set local time zone