#include <iostream>
#include <string>
// release mode: a string that has 15 characters or less will not allocate memory on the heap, but a stack buffer.
// debug mode:
void* operator new(size_t size)
{
std::cout << "Allocating " << size << " bytes.\n";
return malloc(size);
}
int main(void)
{
// debug mode:Allocating 8 bytes. release mode:0 bytes.
std::string name1 = "IamGroot";
std::cout << "------------------------" << std::endl;
// debug mode:Allocating 8 bytes. release mode:0 bytes.
std::string name2 = "IamGrootamGroot";
std::cout << "------------------------" << std::endl;
// debug mode:Allocating 8 bytes.Allocating 32 bytes. release mode:32 bytes.
std::string name3 = "IamGrootIamGroot";
std::cin.get();
}
【Cherno的C++视频】Small string optimization in C++
最新推荐文章于 2024-09-07 23:15:53 发布