Home > Others related to the project > String maximum length on Seeeduino Stalker v2

String maximum length on Seeeduino Stalker v2


Since I had some problems with String variables into another application I wanted to test which is the maximum possible length of a String variable for a simple application where no other important variables are declared. I had to mention that the ATmega328P microprocessor of the Seeeduino Stalker v2 device has 32 kbytes of Flash memory.

The code of the first test application is as below:

void setup()
{
Serial.begin(9600);
}

void loop()
{
String message = “0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789”
“0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789”
“0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789”
“0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789”
“0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789”
“0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789”
“0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789”
“0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789”
“012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345”;

Serial.println(message);
Serial.println(message.length(), DEC);
}

In this case I was able to create a String variable with maximum 896 characters in length. If I want to store more characters into the variable it just get an empty string.

A second test have been done using the next code, inspired from the Rob Faludi test (Arduino String Memory):

int length = 1719;
char* message;

void setup()
{
Serial.begin(9600);
}

void loop()
{
message = (char*) malloc(length);

if(message == NULL)
{
Serial.print(“memory allocation FAILED at string length: “);
Serial.println(length, DEC);
}
else
{
Serial.print(“memory allocated successfully at string length: “);
Serial.println(length, DEC);

free(message);
}
}

The maximum size of the char array was 1719. After this value the application is not able to allocate memory. It’s easily observable that the second application permit us to store more characters into a single variable (version which I recommend for this reason and because it will be easier to verify if the string was created correctly or not). Also, do not forgot that these are the simplest applications possible and usually we have more variables declared into an Arduino application: the maximum possible length for a char array /string will decrease even more…

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: