-1

It seems that member function clear() of string does remove its content, but the removed contents still can be accessed by operator[] . Here's the example that makes me confused.

#include <iostream>
using namespace std;
int main()
{
    string input = "Weird";
    cout << "Your Input: " << input << "\n";
    input.clear();
    cout << "Your Input: " << input << "\n";
    cout << "Your Input: " << input[0] << input[1] << input[2] << input[3] << input[4] << '\n';
    return 0;
}

The results are:

Your Input: Weird
Your Input:
Your Input: eird

Why this is happenning? If example above is normal, what should I do to completely remove its content? (accessing by input[1] should be '\000')

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
YH Cho
  • 29
  • 6
  • Undefined behavior sometimes looks like it's working, even when it isn't. – Mark Ransom Jan 28 '23 at 17:05
  • If you are adamant about the string *actually* being empty after a call to `.clear()` call `.shrink_to_fit()`. – Casey Jan 28 '23 at 17:13
  • @Casey No, that does not work any better. The string *is* actually empty. What OP does is invalid. Calling `.shrink_to_fit()` doesn’t change that. – Konrad Rudolph Jan 28 '23 at 17:15
  • *what should I do to completely remove its content?* -- Why should it matter to you what the junk is at the logical of the string happens to be? If it's 0, the letter `a`, or some other crazy character, why should it be a concern? You shouldn't attempt to index into that area anyway. – PaulMcKenzie Jan 28 '23 at 17:16
  • @PaulMcKenzie that is because when I get string through cin in a loop, (like cin >> input) some leftover element can still be accessed even if I clear() it. – YH Cho Jan 28 '23 at 17:24
  • @PaulMcKenzie One more thing to add, I encountered this kind of problem while solving problems on an algorithm training site. – YH Cho Jan 28 '23 at 17:29
  • 2
    @YHCho But *what* is the problem here? `clear()` does the correct thing, as long as you write valid code. If this scenario poses a problem for you, you have a bug in your code (such as accessing values out of bound). – Konrad Rudolph Jan 28 '23 at 17:30
  • @KonradRudolph Yeah you have a point. At least I have to be careful not to wirte a code like a loop containing both `clear()` and accessing through `operator[]` – YH Cho Jan 28 '23 at 17:34
  • 1
    This isn't much different from `int arr[5];` and then accessing `arr[6];`. The C++ compiler trusts you-the-programmer to not operate out-of-bounds and generates the most efficient code that it can assuming that you will not. Don't betray that trust. – user4581301 Jan 28 '23 at 17:37

1 Answers1

1

Accessing elements of a string after calling the method clear invokes undefined behavior.

It seems in your case the class std::string uses its internal buffer defined within the class itself for short strings.

After the call of clear the class just set the first character of the buffer with the terminating zero character '\0;.

To check that string was cleared just output its length as for example

std::cout << input.length() << '\n';
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 1
    It does not matter if `std::string` is using an internal buffer or not. It's not uncommon for deallocated buffers to hold their former contents for a brief period of time. – Mark Ransom Jan 28 '23 at 17:45
  • @m The C++ Standard does not define what will be with the dynamically allocated buffer in this case.. – Vlad from Moscow Jan 28 '23 at 17:53
  • 1
    The standard doesn't require a previously allocated buffer to be freed when `clear()` is called. It's entirely valid for the string to set the size to 0 and make sure that the `[]` operator does return `'\0'` when accessing the first element while leaving the capacity unchanged. – fabian Jan 28 '23 at 17:58
  • @fabian it's not even required for the string indexing to return `'\0'`, that's only required for `.c_str()`. – Mark Ransom Jan 28 '23 at 18:08
  • @MarkRansom true, but I would find it very curious if c_str()[0] reported '\0' and [0] didn't. Do you know of any such string implementation? – Captain Giraffe Jan 28 '23 at 20:15
  • @CaptainGiraffe I don't know of any such implementation, and I wouldn't expect to find one - by far the easiest way to make `c_str` do the right thing is to already have the `'\0'` at the end of the buffer. But I hate to tempt those nasal demons; any access at or beyond the length of the string is undefined behavior no matter what. – Mark Ransom Jan 29 '23 at 04:30