The problem is C++ related, in const char *a = input.substr(0, idx).c_str() you are taking the value of a temporary. The value of the c_str() is undefined on the next line.
Thank you for your answer, one further question is if I write those lines in a script and then run the script with root, I can get the correct answer. What causes the difference?
Now that you mention it, the result of c_str() should actually remain valid until you destroy or modify the string (my “temporary” explanation was wrong, I think). Which would explain why it works in the script but not in the prompt. So apparently there is something more ongoing, cling related. @vvassilev can you explain the result?
The value printing actually captures the value, and indeed: at the time it’s printed the value is gone. I’d be happy to discuss whether / how that can be improved! @jalopezg@vvassilev I’ll contact you next week to see whether we can come up with a solution. Thanks for bringing this up, @weibin !
But until then, the workaround is to assign the temporary substr() result to a std::string.
Thanks for bringing up this! Indeed, input.substr(0, idx) creates a std::string, copying characters from the original buffer. However, the constructed object is an r-value, i.e. the underlying buffer that you get via .c_str() is not valid after destruction. This memory might get overwritten at any time.
I don’t know if we can workaround this in the interpreter, though. Perhaps, we can discuss it, @Axel, @vvassilev.
I think you were right? (EDIT: as Javi says, ) substr returns a new std::string, which goes out of scope right after the statement is executed, leaving a pointing to invalid memory.
I would not expect a different behavior from this, actually. cling prints the contents of a, and that’s correctly garbage
@weibin when you run this code as a script it just might happen that the memory pointed by a is not overwritten with different contents (it could be, it just happens not to be) – the use-after-delete is there but by chance it does not have visible effects.