

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reserve memory in Arduino for string manipulation
It may happen that a string may change length dynamically during the execution of a program.
If you want to ensure that there is always enough memory available for your string, you can reserve some memory using the reserve() function.
Syntax
String1.reserve(n_bytes);
Where String1 is the string for which you are reserving memory, and n_bytes (unsigned int) is the number of bytes to be reserved in the memory.
Example
String s1 = "Hello"; void setup() { // put your setup code here, to run once: Serial.begin(9600); Serial.println(); s1.reserve(20); s1 = s1+" World!"; Serial.println(s1); s1 = s1+" I'm now trying to exceed the reserved memory"; Serial.println(s1); } void loop() { // put your main code here, to run repeatedly: }
Output
The Serial Monitor output is shown below −
As you can see, even though we exceeded the reserved memory, there were no repercussions, because there was enough memory available. This function only helps set aside some memory for the string, so that we don’t face a shortage at runtime, especially in the execution of heavy codes.
- Related Questions & Answers
- Understanding memory types in Arduino Uno
- String manipulation instructions in 8086 microprocessor
- State the difference between capital reserve and revenue reserve.
- String comparisons in Arduino
- What are Python modules for date manipulation?
- What is Python module for date manipulation?
- String to byteArray in Arduino
- unordered_multimap reserve() function in C++ STL
- Difference between provision and reserve.
- Matrix manipulation in Python
- File system manipulation
- For and While loops in Arduino
- vector::resize() vs vector::reserve() in C++
- Replace characters in a string in Arduino
- Data Manipulation Commands in DBMS
Advertisements