Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String::clear() does not work as expected #4893

Closed
anp59 opened this issue Mar 4, 2021 · 1 comment
Closed

String::clear() does not work as expected #4893

anp59 opened this issue Mar 4, 2021 · 1 comment

Comments

@anp59
Copy link

anp59 commented Mar 4, 2021

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
@me-no-dev
Copy link
Member

done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants