Closed
Description
Description:
Hi guys,
I noticed that the function String::clear() does not behave as expected. According to the name, it should clear the content of a string object, i.e. after this operation an empty String is returned. This works for all string methods except String::c_str(). Here, the content is retained.
Internally, the clear method only sets the length of the string to 0. For c_str(), however, the first character in the internal memory buf would also have to be set to 0 ('\0').
Sketch:
#include <Arduino.h>
void setup() {
String s = "abc";
Serial.print("print(s): ->");Serial.println(s);
Serial.print("print(s.c_str()): ->");Serial.println(s.c_str());
s.clear();
Serial.println("After s.clear():");
Serial.print("print(s): ->"); Serial.println(s);
Serial.print("print(s.c_str()): ->"); Serial.println(s.c_str());
}
void loop() {
}
Output:
print(s): ->abc
print(s.c_str()): ->abc
After s.clear():
print(s): ->
print(s.c_str()): ->abc
Metadata
Metadata
Assignees
Labels
No labels