Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串和字符串[0]的内存地址是否不同? 从中,我知道字符串类的内存布局的精确实现不是由C++标准定义的。但是为什么&string和&string[0]会有不同的内存地址呢 string variable("String Test"); cout << (void *) &variable << endl; // 003EFE58 cout << (void *) &variable[0] << endl; // 003EFE5C cout << (void *) &variable[1] << endl; // 003EFE5D ... ..._C++_String - Fatal编程技术网

字符串和字符串[0]的内存地址是否不同? 从中,我知道字符串类的内存布局的精确实现不是由C++标准定义的。但是为什么&string和&string[0]会有不同的内存地址呢 string variable("String Test"); cout << (void *) &variable << endl; // 003EFE58 cout << (void *) &variable[0] << endl; // 003EFE5C cout << (void *) &variable[1] << endl; // 003EFE5D ... ...

字符串和字符串[0]的内存地址是否不同? 从中,我知道字符串类的内存布局的精确实现不是由C++标准定义的。但是为什么&string和&string[0]会有不同的内存地址呢 string variable("String Test"); cout << (void *) &variable << endl; // 003EFE58 cout << (void *) &variable[0] << endl; // 003EFE5C cout << (void *) &variable[1] << endl; // 003EFE5D ... ...,c++,string,C++,String,但它们确实共享相同的内容,请查看: cout << ((string)*(&variable))[0] << endl; // 'S' cout << *(&variable[0]) << endl; // 'S' 有人能解释一下这里发生了什么吗?因为一个是类实例的地址,另一个是第一个字符的地址,它通常包含在由类管理的动态分配内存中,或者在这种情况下包含在实例本身的某处。因为一个是类实例的地址,另一个是第一

但它们确实共享相同的内容,请查看:

cout << ((string)*(&variable))[0] << endl;  // 'S'
cout << *(&variable[0]) << endl;            // 'S'

有人能解释一下这里发生了什么吗?

因为一个是类实例的地址,另一个是第一个字符的地址,它通常包含在由类管理的动态分配内存中,或者在这种情况下包含在实例本身的某处。

因为一个是类实例的地址,另一个是第一个字符的地址,它通常包含在由类管理的动态分配内存中,或者在实例本身的某个地方,例如。

字符串是一个对象。它的开头有一些字段。这是特定于实现的。在您的例子中,只有一个4字节字段。它所包含的sting以偏移量开始。实际上,sting可以位于堆缓冲区中。它可能不在对象本身中。

字符串是一个对象。它的开头有一些字段。这是特定于实现的。在您的例子中,只有一个4字节字段。它所包含的sting以偏移量开始。实际上,sting可以位于堆缓冲区中。它可能不在对象本身中。

这会让许多程序员感到困惑,因此我将尽全力解释它

// here, you take the address (&) of the first item in the variable array...
//       you then convert that address to a string object
//       an implicit cast occure, which constructs a string instance and copies the contents of 
//       the referenced item in variable (which is a dereferenced char* char('S'))
//       this line is similar to:
//         char c = *(&variable[0]); // dereference
//         string S = string(c);     // copy
//         printf("%c\n", S);        // print
printf("%c\n", ((string)*(&variable))[0]);  // 'S'

// here, you do the same dereference of the first item in variable,
//       except you don't copy it into a string object
//       similar to
//         char c = *(&variable[0]);
//         printf("%c\n", c);
printf("%c\n", *(&variable[0]));            // 'S'

这恰好让很多程序员感到困惑,所以我将尽我所能解释它

// here, you take the address (&) of the first item in the variable array...
//       you then convert that address to a string object
//       an implicit cast occure, which constructs a string instance and copies the contents of 
//       the referenced item in variable (which is a dereferenced char* char('S'))
//       this line is similar to:
//         char c = *(&variable[0]); // dereference
//         string S = string(c);     // copy
//         printf("%c\n", S);        // print
printf("%c\n", ((string)*(&variable))[0]);  // 'S'

// here, you do the same dereference of the first item in variable,
//       except you don't copy it into a string object
//       similar to
//         char c = *(&variable[0]);
//         printf("%c\n", c);
printf("%c\n", *(&variable[0]));            // 'S'

字符串不是数组类型,而是类类型。如果您声明了一个字符数组,这将按预期工作。@H2CO3正确,同意,删除,因为printf@GrijeshChauhan不OP请求STD::String…@ GrijHeHuhanhan-Yes,不是太好的做法,而不是在C++代码中使用PrimTF……这可能是数组和指针互换使用的缺陷之一。指针上的运算符[]和数组上的运算符[]产生相同的结果,这让我很困惑。string不是数组类型,而是类类型。如果您声明了一个字符数组,这将按预期工作。@H2CO3正确,同意,删除,因为printf@GrijeshChauhan不OP请求STD::String…@ GrijHeHuhanhan-Yes,不是太好的做法,而不是在C++代码中使用PrimTF……这可能是数组和指针互换使用的缺陷之一。指针上的运算符[]和数组上的运算符[]产生了相同的结果,这让我感到困惑。这并不是因为它的开头有一些字段。数组通常是动态分配的,因此实际上只有一个指向其他完全不相关的内存块的指针。True。99%的案例都是这样。我应该不信任原始帖子中的列表吗?也许原因还是需要的,不一定。事实上,谁知道使用std::string intrinsic的现代优化编译器在使用字符串文本构造的字符串实例时会做什么呢。EG把它变成一个包含自动数组而不是动态分配指针的结构…那你为什么要在你的评论中反驳我的陈述呢?因为这不是典型而直接的推理。它的开头并不是有一些字段。数组通常是动态分配的,因此实际上只有一个指向其他完全不相关的内存块的指针。True。99%的案例都是这样。我应该不信任原始帖子中的列表吗?也许原因还是需要的,不一定。事实上,谁知道使用std::string intrinsic的现代优化编译器在使用字符串文本构造的字符串实例时会做什么呢。EG将其转换为包含自动数组而不是动态分配指针的结构…那么为什么您在注释中反驳我的陈述?因为这不是典型且简单的推理。我认为您误解了第一个,它是字符串*&变量[0]而不是字符串*&变量[0]。我认为您误解了第一个,它是字符串*&变量[0],而不是字符串*&变量[0]。