# How to reduce String Memory Demand

**URL:** <https://forum.arduino.cc/t/how-to-reduce-string-memory-demand/682373>\
**Category:** Programming\
**Created:** [November 11, 2020, 10:14pm UTC](https://forum.arduino.cc/t/how-to-reduce-string-memory-demand/682373 "2020-11-11T22:14:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ardocman](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@ardocman](https://forum.arduino.cc/u/ardocman)\
**Post date:** [November 11, 2020, 10:14pm UTC](https://forum.arduino.cc/t/how-to-reduce-string-memory-demand/682373/1 "2020-11-11T22:14:11Z")

</div>

My example sketch uses strings which can hog lots of memory. I'm looking for ways to reduce that memory demand. In the sketch below, memory isn't a problem, but with other sketches, it is.  
I tried char with multiple characters, but that didn't work.

Any ideas? My example sketch appears below.

```cpp
//Add, subtract and multiply calcutator for two input numbers. Input and output via the Serial Monitor. See note after the sketch. 
//Multiply doesn't work with large input numbers.                        DE 1/29/20
//When using Quit, the IDE will ask for save or not.

int numberA;
int numberB;
int sum;
int diff;
int product;
int both;
String msgA="Enter 1st number, then hit Enter.";
String msgB="Enter 2nd number, then hit Enter.";
String msgA1="Your 1st number is: ";
String msgB1="Your 2nd number is: ";
void setup() {
Serial.begin(9600);
Serial.println("A simple add/multiply/total calcutator. No large numbers please.");
Serial.println();
} //end setup

void loop() {
//get 1st number
Serial.println(msgA);
while (Serial.available()==0){//waiting for 1st number
}
numberA=Serial.parseInt();//get 1st number
Serial.print(msgA1);
Serial.println(numberA);
Serial.println();
//get 2nd number
Serial.print(msgB);
while (Serial.available()==0){//waiting for 2nd number
}
numberB=Serial.parseInt();//get 2nd numbe
Serial.println();
Serial.print(msgB1);
Serial.println(numberB);
Serial.println();
//calculate answers
sum = numberA+numberB;
delay(100);
diff = numberA-numberB;
delay(100);
product = numberA*numberB;
delay(100);
both = sum+product;
//print answers
Serial.print("The SUM is ");
Serial.println(sum);
Serial.print("The DIFFERENCE is ");
Serial.println(diff);
Serial.print("The PRODUCT is ");
Serial.println(product);
Serial.print("Added TOGETHER (sum + product) they are ");
Serial.println(both);
}

//NOTE !! Serial-monitor must be set to NO LINE ENDING to avoid extra zeros in the println(number) display. Also use auto scroll.

```

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [November 11, 2020, 10:25pm UTC](https://forum.arduino.cc/t/how-to-reduce-string-memory-demand/682373/2 "2020-11-11T22:25:51Z")

</div>

Use c-strings ... null-terminated char arrays:

> **[C - Strings](https://www.tutorialspoint.com/cprogramming/c_strings.htm)**
>
> C Strings - Strings are actually one-dimensional array of characters terminated by a null character ''. Thus a null-terminated string contains the characters that comprise the string followed by a null.

> **[Strings in C - GeeksforGeeks](https://www.geeksforgeeks.org/strings-in-c-2/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

[http://www.cplusplus.com/reference/cstring/](http://www.cplusplus.com/reference/cstring/)

---

<div class="post-metadata">

**Author:** ![anon73444976](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon73444976](https://forum.arduino.cc/u/anon73444976)\
**Post date:** [November 11, 2020, 10:35pm UTC](https://forum.arduino.cc/t/how-to-reduce-string-memory-demand/682373/3 "2020-11-11T22:35:04Z")

</div>

> ardocman:  
> My example sketch uses **S** trings which can hog lots of memory.

```arduino
Serial.println("A simple add/multiply/total calcutator. No large numbers please.");

```

The F() macro would help here.

---

<div class="post-metadata">

**Author:** ![IoT\_hobbyist](https://avatars.discourse-cdn.com/v4/letter/i/e5b9ba/32.png) [@IoT\_hobbyist](https://forum.arduino.cc/u/IoT_hobbyist)\
**Post date:** [November 11, 2020, 11:55pm UTC](https://forum.arduino.cc/t/how-to-reduce-string-memory-demand/682373/4 "2020-11-11T23:55:07Z")

</div>

If string is constant, use [PROGMEM](https://arduinogetstarted.com/reference/arduino-progmem) or F() macro to save RAM memory.  
If string is not constant, and if you want to use [String](https://arduinogetstarted.com/reference/arduino-string-object)object, use [String.reserve()](https://arduinogetstarted.com/reference/arduino-string-reserve) to avoid the memory leak and fragment issue.

---

<div class="post-metadata">

**Author:** ![anon57380292](https://avatars.discourse-cdn.com/v4/letter/a/d6d6ee/32.png) [@anon57380292](https://forum.arduino.cc/u/anon57380292)\
**Post date:** [November 12, 2020, 3:06am UTC](https://forum.arduino.cc/t/how-to-reduce-string-memory-demand/682373/5 "2020-11-12T03:06:13Z")

</div>

[The evil of Arduino Strings](https://hackingmajenkoblog.wordpress.com/2016/02/04/the-evils-of-arduino-strings/)...

---

<div class="post-metadata">

**Author:** ![ardocman](https://avatars.discourse-cdn.com/v4/letter/a/f475e1/32.png) [@ardocman](https://forum.arduino.cc/u/ardocman)\
**Post date:** [November 12, 2020, 8:11pm UTC](https://forum.arduino.cc/t/how-to-reduce-string-memory-demand/682373/6 "2020-11-12T20:11:40Z")

</div>

> TheMemberFormerlyKnownAsAWOL:
> 
> ```arduino
> Serial.println("A simple add/multiply/total calcutator. No large numbers please.");
> 
> ```
> 
> _The F() macro would help here._
> 
> Thanks! Your simple F() macro suggestion worked very well. My dynamic memory went from 1,318 ------\> 596 bytes.

---

<div class="post-metadata">

**Author:** ![drmpf](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drmpf/32/1126438_2.png) [@drmpf](https://forum.arduino.cc/u/drmpf)\
**Post date:** [November 14, 2020, 1:40am UTC](https://forum.arduino.cc/t/how-to-reduce-string-memory-demand/682373/7 "2020-11-14T01:40:42Z")

</div>

While using F() macro is the correct answer here, if you go further take a look at my SafeString library available via the  
Arduino library manager. ([detailed tutorial here](https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html))  
SafeStrings are debuggable and safe and much more robust then c-strings and do not have the memory fragmentation and the odd errors of Arduino Strings.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 12:50am UTC](https://forum.arduino.cc/t/how-to-reduce-string-memory-demand/682373/8 "2021-05-06T00:50:48Z")

</div>

Closed on May 6, 2021, 12:50am UTC


