Last active
May 5, 2022 17:19
-
-
Save ioslh/c00a490625cdbaff600800938a39c059 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
char get_first_char(string *str) { | |
return (*str)[1] + 2; | |
} | |
void string_memory(){ | |
long bottom = -1; | |
string word = "dev"; | |
cout << get_first_char(&word) << endl; | |
cout << "> Current string content: " << word << endl; | |
cout << " &bottom: " << &bottom << endl; | |
cout << " &word: " << &word << endl; | |
char &c1 = word[0]; | |
cout << "&word[0]: " << (void *)&c1 << endl; | |
auto size = (char *)&bottom - (char *)&word; | |
cout << " sizeof word: " << size << endl; | |
long *pword = (long *)&word; | |
cout << " 0. " << std::hex << *pword << endl; | |
cout << " 1. " << std::hex << *(pword + 1) << endl; | |
cout << " 2. " << std::hex << *(pword + 2) << endl; | |
// 长度超过 24 怎么办 | |
cout << string(40, '-') << endl; | |
word = string(33, 'C'); | |
cout << " &word: " << &word << endl; | |
char &c2 = word[0]; | |
cout << "&word[0]: " << (void *)&c2 << endl; | |
cout << " 0. " << std::hex << *pword << endl; | |
cout << " 1. " << std::hex << *(pword + 1) << endl; | |
cout << " 2. " << std::hex << *(pword + 2) << endl; | |
} | |
int main() { | |
string_memory(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment