Skip to content

Instantly share code, notes, and snippets.

@ioslh
Last active May 5, 2022 17:19
Show Gist options
  • Save ioslh/c00a490625cdbaff600800938a39c059 to your computer and use it in GitHub Desktop.
Save ioslh/c00a490625cdbaff600800938a39c059 to your computer and use it in GitHub Desktop.
#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