C++ GDB报告了c+;中参数的错误地址+;对象';s构造函数

C++ GDB报告了c+;中参数的错误地址+;对象';s构造函数,c++,constructor,gdb,parameter-passing,stdstring,C++,Constructor,Gdb,Parameter Passing,Stdstring,我遇到了一个奇怪的行为,GDB将字符串作为参数传递给构造函数。 代码运行良好,但当我在调试器中单步执行时,GDB似乎认为我的参数位于与它不同的地址。有人知道这里发生了什么吗 下面是我能创建的最简单的程序,它演示了这个问题: --(jwcacces@neptune)--------------------------------------------(/home/jwcacces)-- --$ nl gdb_weird.cpp 1 #include <iostream>

我遇到了一个奇怪的行为,GDB将字符串作为参数传递给构造函数。 代码运行良好,但当我在调试器中单步执行时,GDB似乎认为我的参数位于与它不同的地址。有人知道这里发生了什么吗

下面是我能创建的最简单的程序,它演示了这个问题:

--(jwcacces@neptune)--------------------------------------------(/home/jwcacces)--
--$ nl gdb_weird.cpp
     1  #include <iostream>
     2  #include <string>
     3
     4  class C
     5  {
     6  public:
     7     C(std::string str)
     8     {
     9        std::string* str_ptr = &str;
    10        std::cout << "Address of str: " << &str << std::endl;
    11        std::cout << "Address in str_ptr: " << str_ptr << std::endl;
    12        std::cout << "Value of str: " << str << std::endl;
    13     };
    14  };
    15
    16  int main(int, char*[])
    17  {
    18     std::string s("Hello, World!");
    19     C c(s);
    20     return 0;
    21  }
现在,调试:

--(jwcacces@neptune)--------------------------------------------(/home/jwcacces)--
--$ gdb a.out
GNU gdb (Ubuntu/Linaro 7.3-0ubuntu2) 7.3-2011.08
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://bugs.launchpad.net/gdb-linaro/>...
Reading symbols from /home/jwcacces/a.out...done.

(gdb) br main
Breakpoint 1 at 0x80488ce: file gdb_weird.cpp, line 18.

(gdb) run
Starting program: /home/jwcacces/a.out

Breakpoint 1, main () at gdb_weird.cpp:18
18         std::string s("Hello, World!");

(gdb) next
19         C c(s);

(gdb) step
C::C (this=0xffffd74f, str=...) at gdb_weird.cpp:9
9             std::string* str_ptr = &str;
那么,GDB认为
str
的地址是什么

(gdb) output &str
(std::string *) 0xffffd734
(gdb) next
10            std::cout << "Address of str: " << &str << std::endl;

(gdb) next
Address of str: 0xffffd748
11            std::cout << "Address in str_ptr: " << str_ptr << std::endl;

(gdb) next
Address in str_ptr: 0xffffd748
12            std::cout << "Value of str: " << str << std::endl;
程序认为str的地址是什么

(gdb) output &str
(std::string *) 0xffffd734
(gdb) next
10            std::cout << "Address of str: " << &str << std::endl;

(gdb) next
Address of str: 0xffffd748
11            std::cout << "Address in str_ptr: " << str_ptr << std::endl;

(gdb) next
Address in str_ptr: 0xffffd748
12            std::cout << "Value of str: " << str << std::endl;
并且程序本身在使用参数时没有问题:

(gdb) next
Value of str: Hello, World!
13         };

(gdb) continue
Continuing.
[Inferior 1 (process 19463) exited normally]

(gdb) quit
我已经尝试将构造函数参数的类型更改为int、struct和指针,但我无法重现这种奇怪的情况。
此外,我还尝试将调试格式设置为-ggdb

问题:

  • 这是怎么回事
  • 为什么gdb说
    std::string
    npos
    成员被优化了(可能是从库中优化的),这与此有关系吗
  • 在GDB认为
    str
    所在的“对象”中,
    成员指向
    0xffffd748
    ,即
    str
    实际所在的地址,这只是巧合吗
  • 在其他什么情况下会发生这种行为
----哇,突破--
如果我将调试格式设置为-gstabs+,GDB将获得
str
right的地址。

这是否意味着GDB调试格式不能正常工作?< / P> < P>从任何帖子()看来,C++调试时,GSTASS或-GSTASS+是正确的标志。我希望这能有所帮助。

让我印象深刻的是,您的“正确”指针0xffffd748也显示为您第一次打印语句中“无效”字符串的
\M\p
成员。因此,gdb可能被混淆为指向-的指针和指向-的指针?顺便说一句,我假设简单地说“这就是为什么应该将字符串作为const refs传递”并不是一个合适的答案:p字符串作为常规参数(非限定的传递值)传递良好,只是gdb有问题。是的,_M_p指针对我来说真的很奇怪。这一定是因为std::string是一个函数参数-如果你在C::C()内将str赋给另一个std::string变量,然后在gdb中检查第二个变量,那么看起来很好,尽管函数参数对gdb来说似乎是垃圾。这是gdb bugzilla中的一个已知错误,但自2011年报告该错误以来,该错误报告似乎已经停滞不前:尽管可能是gcc错误-使用clang++-gdwarf-4编译相同的代码,gdb将非常高兴地对其进行调试。
(gdb) next
Value of str: Hello, World!
13         };

(gdb) continue
Continuing.
[Inferior 1 (process 19463) exited normally]

(gdb) quit