Strings loosing its content

Hi,

actually I am programming an LCD Display with my arduino Nano.
I am using many Strings and the problem is, that the more String arrays I use, the more allocated String arrays loose their content.

My dynamic storage is at 70% and the program memory at 30%.

Can anyone explain the reason of that problem?

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

If you need more advice please post your program. And please use the code button </> when doing so.

...R

Thanks for your answer.

First I was thinking about using an char array with the length of 20 ( on account of the 4x20 character LCD-Display).

The problem is that my output on the display has a variable number of characters.
So I am getting an error if my input is not 20 characters long.

So actually I am using PROGMEM to store my variables that contain strings.
My program code has more than 400 rows.
So I think that it would be easier if you don´t have to understand my entire code :slight_smile:

Does someone know, how PROGMEM works?

I created an simply program code to see if i can safe some program storage.

#include <avr/pgmspace.h>

struct person
{
String first_name;
int age;
};

void setup() {
// put your setup code here, to run once:

}

void loop() {
// put your main code here, to run repeatedly:
String hallo PROGMEM = "aaaaaaaaaaaaaaaaaaaaaaaaasbjaölsflksadjflsajfsalkjfsalj";
String hallo2 PROGMEM ="aaaaaaaaaaaaaaaaaaaaaaaaasbjaölsflksadjflsajfsalkjfsaljasdf";
String hallo3 PROGMEM = "aaaaaaaaaaaaaaaaaaaaaaaaasbjaölsflksadjflsajfsalkjfsalj";
String hallo24 PROGMEM = "aaaaaaaaaaaaaaaaaaaaaaaaasbjaölsflksadjflsajfsalkjfsaljasdf";
String hallo5 PROGMEM = "aaaaaaaaaaaaaaaaaaaaaaaaasbjaölsfertlksadjflsajfsalkjfsalj";
String hallo26 PROGMEM ="aaaaaaaaaaaaaaaaaaaaaaaaasbjaölsflfgksadjflsajfsalkjfsaljasdf";
String hallo7 PROGMEM= "aaaaaaaaaaaaaaaaaaaaaaaaasbjaölsflksadjflssdajfsalkjfsalj";
String hallo28 PROGMEM ="aaaaaaaaaaaaaaaaaaaaaaaaasbjaölsflksadjflsajfsalkjfsadsdljasdf";
String hallo9 PROGMEM = "aaaaaaaaaaaaaaaaaaaaaaaaasbjaölsflksadjflsajfsalkjfsalj";
String hallo1232 PROGMEM = "aaaaaaaaaaaaaaaaaaaaaaaaasbjaölsflksadjflssajfsalkjfsaljasdf";
String hall365o PROGMEM = "aaaaaaaaaaaaaaaaaaaaeaaaaasbjaölsflksadjflsajfsalkjfsalj";
String hallo6572 PROGMEM = "aaaaaaaaaaaaaaaaaaaaaaadaasbjaöalsflksadjflsajfsalkjfsaljasdf";
String hallo678 PROGMEM = "aaaaaaaaaaaaaaaaaaaaaaaaasbjaölsflksadjflsajfsalkjfsalj";
String hallo7892 PROGMEM = "aaaaaaaaaaaaaaaaavaaaaaaaasbjaölsflksadjflsajfsalkjfsaljasdf";
}

Strangely it doesn´t matter if i use PROGMEM or not.
The used dynamic storage is still 30%, either I use PROGMEM or not.

Does someone know the problem?

anneme:
First I was thinking about using an char array with the length of 20 ( on account of the 4x20 character LCD-Display).

The problem is that my output on the display has a variable number of characters.
So I am getting an error if my input is not 20 characters long.

First, if you want to store 20 characters the array must be 21 characters long to allow room for the terminating '\0'

Second, if you can't store fewer characters then there is something wrong with your code which you have not posted even though I asked you to.

My program code has more than 400 rows.
So I think that it would be easier if you don´t have to understand my entire code :slight_smile:

I would clearly prefer to look at a short program rather than a long one. But it is essential to see a complete program that illustrates the problem.

...R

I wrote an code that shows the problem.
I want to create manually some variable of type person and want to add them to an array.
This is my idea to make it use less storage.

I found out, that I will not get an error, if I´m initializing "const person all_persons[2] PROGMEM;" in the setup() function.

the array all_persons is used by other functions therefore I want to declare it global (or return it alternatively via reference).

#include <avr/pgmspace.h>

struct person
{
  String first_name;
  int age;  
};

const person all_persons[2] PROGMEM;

void setup() {

  Serial.begin(9600);

  person a PROGMEM;
  a.first_name = "Tim";
         a.age = 11;

  person b PROGMEM;
  b.first_name = "Tobi";
         b.age = 24;

  person all_persons[0] =a;
  person all_persons[1]=b;
}


void loop() 
{
  Serial.println(
}

I told you in Reply #1 not to use the String class.

I don't think you can build a struct containing a String because the length is not defined at compile time.

...R

Thak you for your answers!!
So the best way to solve this problem is to generate the struct like this?

struct person
{
const char *first_name;
int age;

};

person all_persons[5][5];

Actually I could save one percent of dynamic ram.
Sorry for my inexperience of this topic...

ok, perfekt.
i could save four percent by using this method.
Are there some more stepts I can do (like using PROGMEM) to safe some more dynamic memory?

Thank you so much for your help!!!!

anneme:
So the best way to solve this problem is to generate the struct like this?

struct person
{
const char *first_name;
int age;

};

I would do it like this

struct person
{
 char first_name[20]; // or whatever is the longest length you will need
 byte age;  // I doubt you will have anyone older than 255

};

...R

I have tried it with an char array with a given size.

The problem is just that if I am going to say for example:


person person_one;

person_one.first_name = "Walter";


I am getting that error: incompatible types in assignment of 'const char [8] to 'char[20]'

What am I doing wrong?

anneme:
I am getting that error: incompatible types in assignment of 'const char [8] to 'char[20]'

What am I doing wrong?

You can't assign a value to an existing char array in the same way that you can to (say) a byte.

You need to use the strcpy() function. There is also strncpy()

...R

Thanks Robin2,

your are right!!!
I think I am on the right way by using strcpy().

I got one more problem.

If I am using this struct

struct person
{
char first_name[20]; // or whatever is the longest length you will need
byte age; // I doubt you will have anyone older than 255

};

person person_one;

strcpy(person_one.first_name,"peter");
Serial.println(

and try to output the first_name on the serial monitor, the first sign it shows is the question mark.

Does anyone know hot to solve this problem?

Not without seeing the code, no. A snippet does not count.