Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- static uint32_t s_AllocCount = 0;
- void* operator new(size_t size)
- {
- s_AllocCount++;
- std::cout << "Allocating " << size << " bytes\n";
- return malloc(size);
- }
- void PrintName(std::string_view name) // No need for const reference
- {
- std::cout << name << std::endl;
- }
- int main()
- {
- const char* name = "Yan Chernikov"; // Changed from string to const char*
- #if 0
- std::string firstName = name.substr(0, 3);
- std::string lastName = name.substr(4, 9);
- #else
- std::string_view firstName(name, 3); // Removed c_str()
- std::string_view lastName(name + 4, 9); // Removed c_str()
- #endif
- PrintName("Cherno"); // Also no allocation since PrintName is std::string_view
- // Changing back to std::string will give an allocation
- PrintName(firstName);
- PrintName(lastName);
- std::cout << s_AllocCount << " allocations" << std::endl;
- std::cin.get();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement