当std :: string对象传递给函数时,字符串数据会发生什么? [英] What happens to the string data when std::string objects are passed to functions?
问题描述
我注意到一些我不明白的函数的字符串参数发生的事。
我写了这个小测试程序:
#include< string>
#include< iostream>
using namespace std;
void foo(string str){
cout<< str<< endl;
}
int main(int argc,char ** argv){
string hello =hello;
foo(hello);
}
我这样编译:
$ g ++ -o string_test -g -O0 string_test.cpp
在Mac OSX 10.6上,在g ++ 4.2.1下, str
里面 foo()
它做为 hello
outside foo()
:
12 foo(hello);
(gdb)p hello
$ 1 = {
static npos = 18446744073709551615,
_M_dataplus = {
< std :: allocator< char> = {
< __ gnu_cxx :: new_allocator< char>> = {< No data fields>},< No data fields>},
std :: basic_string< char,std :: char_traits< char>,std :: allocator< char> > :: _ Alloc_hider:
_M_p = 0x100100098hello
}
}
(gdb)s
foo(str = @ 0x7fff5fbfd350)at string_test.cpp: 7
7 cout<< str<< endl;
(gdb)p str
$ 2 =(string&)@ 0x7fff5fbfd350:{
static npos = 18446744073709551615,
_M_dataplus = {
< std :: allocator< ; char>> = {
< __ gnu_cxx :: new_allocator< char>> = {< No data fields>},< No data fields>},
std :: basic_string< char,std :: char_traits< char>,std :: allocator< char> > :: _ Alloc_hider:
_M_p = 0x100100098hello
}
}
但是,在Ubuntu下的g ++ 4.3.3下,它不会:
12 foo
(gdb)p hello
$ 1 = {static npos = 18446744073709551615,_M_dataplus = {< std :: allocator< char> = {< __ gnu_cxx :: new_allocator< char>> = {< No data fields>},< No data fields>},_M_p = 0x603028hello}}
(gdb)s
foo(str = {static npos = 18446744073709551615,_M_dataplus = {< std :: allocator< char>> = {< __ gnu_cxx :: new_allocator< char>> = {< No data fields>},< No data fields>},_M_p = 0x7fff5999e530 }})at string_test.cpp:7
7 cout<< str<(gdb)p str
$ 2 = {static npos = 18446744073709551615,_M_dataplus = {< std :: allocator< char>> = {< __ gnu_cxx :: new_allocator< char>> = {< No data fields>},< No data fields>},_M_p = 0x7fff5999e530 }}
(gdb)p str-> _M_dataplus-> _M_p
$ 3 = 0x7fff5999e530(0`
那么,当传递给这个函数时,字符串的值会发生什么?为什么两个编译器之间的区别?
c>在我的编译器foo()
是内联的,所以只有一个hello
。也许这也是为你发生了什么。
调试器中的程序看起来像不是语言标准的一部分。只有可见的结果,如实际打印你好,是。
I've noticed something I don't understand happening to the string arguments to functions.
I've written this little test program:
#include <string> #include <iostream> using namespace std; void foo(string str) { cout << str << endl; } int main(int argc, char** argv) { string hello = "hello"; foo(hello); }
I compile it like this:
$ g++ -o string_test -g -O0 string_test.cpp
Under g++ 4.2.1 on Mac OSX 10.6,
str
insidefoo()
looks the same as it does ashello
outsidefoo()
:12 foo(hello); (gdb) p hello $1 = { static npos = 18446744073709551615, _M_dataplus = { <std::allocator<char>> = { <__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, members of std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Alloc_hider: _M_p = 0x100100098 "hello" } } (gdb) s foo (str=@0x7fff5fbfd350) at string_test.cpp:7 7 cout << str << endl; (gdb) p str $2 = (string &) @0x7fff5fbfd350: { static npos = 18446744073709551615, _M_dataplus = { <std::allocator<char>> = { <__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, members of std::basic_string<char,std::char_traits<char>,std::allocator<char> >::_Alloc_hider: _M_p = 0x100100098 "hello" } }
Under g++ 4.3.3 on Ubuntu, however, it doesn't:
12 foo(hello); (gdb) p hello $1 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x603028 "hello"}} (gdb) s foo (str={static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x7fff5999e530 "(0`"}}) at string_test.cpp:7 7 cout << str << endl; (gdb) p str $2 = {static npos = 18446744073709551615, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, _M_p = 0x7fff5999e530 "(0`"}} (gdb) p str->_M_dataplus->_M_p $3 = 0x7fff5999e530 "(0`"
So, what's happening to the value of the string when it is passed to this function? And why the difference between the two compilers?
解决方案On my compiler
foo()
is inlined, so there is only onehello
. Perhaps that is what is happening for you too.What a program looks like in a debugger is not part of the language standard. Only the visible result, like actually printing "Hello", is.
这篇关于当std :: string对象传递给函数时,字符串数据会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文