I came across a scenario where string concatenation is failing in C++. But I don't see a reason for it to fail.
Code sample is as below:
int main()
{
std::string a;
std::string b = "bbbbbbb";
a.resize(10);
for (int i = 0; i <= 5; i++) {
a[i] = 'a';
}
a = a+b;
printf("\n%s\n", a.c_str());
}
It is outputting aaaaaa
.
I was expecting it to output aaaaaabbbbb
. If I change a.resize(10);
to a.resize(5);
I am getting the expected output.
Would be helpful if someone could help me in understanding the behaviour?