-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
56 lines (41 loc) · 1.2 KB
/
Main.cpp
File metadata and controls
56 lines (41 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <string>
#define LOG(msg) std::cout << msg << std::endl
static uint32_t s_alloc_count = 0;
void* operator new(size_t size)
{
s_alloc_count++;
LOG("allocating " << size << " bytes");
return malloc(size);
}
void print_str(const std::string& str)
{
LOG(str);
}
int main()
{
std::string name = "Cherno Test";
print_str(name);
print_str("Cherno Test");
std::string first_name = name.substr(0, 6);
print_str(first_name);
// <- gives const char*
// <- offset
std::string_view first_name2(name.c_str() + 0, 6);
LOG(first_name2);
const char* name2 = "Cherno Test";
LOG(name2);
std::string_view first_name3(name2 + 0, 6);
LOG(first_name3);
LOG("allocations: " << s_alloc_count);
std::cin.get();
}
/*
* std::string always allocates on the heap (actually only most of the time) -> overhead
*
* std::string_view doesn't allocate memory on the heap -> less overhead
* it allows a substring of an already existing string to be accessed
* can easily be used by value rather than by reference
*
* functions can just take a std::string_view instead of a std::string -> no heap allocation
*/