You are calling std::string::append
, that ultimately results in delete
getting called. If we go through the steps involved in std::string::append
, it might make more sense why delete
gets called.
Say you have:
std::string s("abc");
s.append("def");
When you create s
, memory has to be allocated to hold "abc"
. At the end of s.append("def");
, there has to be enough memory associated with s
to hold "abcdef"
. Steps to get there:
- Get the length of
s
=>3
. - Get the length of the input string
"def"
=>3
. - Add them to figure out the length of the new string. =>
6
. - Allocate memory to hold the new string.
- Copy
"abc"
to the newly allocated memory. - Append
"def"
to the newly allocated memory. - Associate the newly allocated memory with
s
. - Delete the old memory associated with
s
. (This is wheredelete
comes into picture).