我想知道char序列(字符串(在RAM中是如何排列的,所以用c++进行下面的测试。编译器环境是64位系统,在windows10上运行cygwin。
#include <iostream>
using namespace std;
int main() {
char* c1 = "01";
cout << "string to print:" << endl;
cout << c1 << endl;
cout << "the address in stack of the pointer:" << endl;
cout << &c1 << endl;
cout << "the address in heap for the first letter in the string:" << endl;
cout << (void*)c1 << endl;
cout << "the address in heap for the second letter in the string:" << endl;
cout << (void*)(++c1) << endl;
}
输出为:
string to print:
01
the address in stack of the pointer:
0xffffcc28
the address in heap for the first letter in the string:
0x100403001
the address in heap for the second letter in the string:
0x100403002
我知道字符"0"one_answers"1"的地址偏移了1字节。这是不是意味着这两个字符只是在一个ram单元中一个接一个地排列?如下图所示?
/*
* 64bit ram
* |-------------------------64bits--------------------------------|
* stack | |
*
* heap | 8bits | 8bits | 8bits | 8bits | 8bits | 8bits | '1' | '0' |
*/
我知道字符'0'和'1'的地址只偏移了1bit。
否。它们偏移一个字节。char的大小是一个字节,数组元素在虚拟内存中是相邻的。字符串是一个数组。