Have a basic snippet of initialization a std::string variable, and track the memory usage of program using overloaded new operator function as below:
static uint32_t s_AllocCount = 0;
// Overload of new operator.
void* operator new(size_t size) {
s_AllocCount++;
std::cout << "Allocating " << size << " bytes\n";
return malloc(size);
}
Example-1: When initialized below in main:
int main()
{
std::string name = "Hello World 1234";
std::cout<<"No. of allocations="<<s_AllocCount<<std::endl;
return 0;
}
Output
Allocating 17 bytes
No. of allocations=1
Example-2: When initialized below in main:
int main()
{
std::string name = "Hello World";
std::cout<<"No. of allocations="<<s_AllocCount<<std::endl;
return 0;
}
Output
No. of allocations=0
Query
Why the overloaded new operator is not called in the 2nd example where the length of the string is less than 16 bytes/characters. Where does the initialization of string memory happen in 2nd example.