String Literals
Why is the size 7? Because there is an additional null terminator 00
: "Cherno\0"
The two
.
represent the position of 00
This string is stored in the const section of the binary file.
Opening it with a hex editor, you can see "Cherno" in the binary file. These characters are embedded in the binary file. When we reference it, it actually points to a constant data block that is not allowed to be edited.
Running the program in Release mode to modify the string will not change it. However, in Debug mode, an exception will be thrown.
Wide Char
const char* name = u8"Cherno";
const wchar_t* name2 = L"Cherno"; // L indicates that the following string literal is composed of wide characters
// 2 bytes
// Introduced in C++11
const char16_t* name3 = u"Cherno"; // 16 bits = 2 bytes
const char32_t* name4 = U"Cherno"; // 32 bits = 4 bytes
Difference between wchar_t and char16_t?
Although we often say that wchar is 2 bytes per character, it is actually determined by the compiler. (Windows: 2 bytes, Linux: 4 bytes).
If you want it to always be 2 bytes, you can use char16_t
.
R Method
Adding R
before a string means ignoring escape characters, where R
stands for raw.
String literals are always stored in read-only memory.