I have the following code :
std::string Utils::get() {
std::string result;
result.append(1, 'x');
result.append(1, 'x');
result.append(1, 'x');
return result;
}
I expect ‘xxx’ to be returned.
However, when I run under debug mode, I get the warning
“Stack around the variable ‘result’ was corrupted”
Am I using append function the wrong way?
Your use of
appendis correct. Something else (possibly just before calling the function, etc.) is corrupting the stack. It’s only when you use the stack some more (a function call in this case) will it detect it.The reason you won’t see it in Release is because the function call is (likely) inlined. Ergo, the stack isn’t manipulated like it is now, and the result is different. Post some more context and we’ll help you track it down.