为什么不能使用“print”访问向量中的值在gdb调试器? [英] Why can't I access value in a vector using "print" in gdb debugger?

查看:198
本文介绍了为什么不能使用“print”访问向量中的值在gdb调试器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习使用gdb调试器,但是当该向量包含从另一个向量复制的值时,无法访问向量的值。最终,下面的程序做了我想要它做的事情(将nums的值复制到一个新的向量copyOfNums),但是当我尝试检查copyOfNums [0]的值在copyNums ,则无法访问内存中的值(请参阅下面的gdb会话)。



下面的copy nums已经执行了for循环的for语句,因此copyNums [0]应该包含nums [0]指向的值的地址,引用一个新的int(与nums [0]指向的l值相同)?无论我希望能够访问的值,但正如你可以看到从以下gdb输出,它是不可访问的。

 断点1,main.cpp的copyNums(nums = @ 0x7fff5fbffad0):10 

(gdb)print nums [0]
$ 1 =(int&)@ 0x100103910:2
(gdb)print copyOfNums [0]
$ 2 =(int&)@ 0x0:不能访问内存地址0x0
(gdb)

#include< iostream> ;
#include< fstream>
#include< sstream>
#include< string>
#include< vector>

using namespace std;

vector< int> copyNums(vector< int# nums){
int sizeNums = nums.size();
vector< int> copyOfNums;

=> for(int i = 0; i copyOfNums.push_back(nums [i]);
}

return copyOfNums;
}

vector< int> getSmallNums(){

vector< int> nums;
nums.push_back(2);
nums.push_back(1);
nums.push_back(8);

return nums;
}

int main(int argc,char * argv []){
vector< int& nums = getSmallNums();
vector< int> copyOfNums = copyNums(nums);


return 0;
}

这里是返回sort后的gbd输出。看起来副本包含对内存中新位置的引用,其l值为2(如预期)。为什么我无法访问函数copyNums中的copyOfNums [0]?

 (gdb)print nums [0] 
$ 3 =(int&)@ 0x1001000e0:2
(gdb)print copyOfNums [0]
$ 4 =(int&)@ 0x100103920:2



编辑:这是调试会话的更完整的图片

 (gdb)info locals 
i = 1
sizeNums = 3
copyOfNums = {
< std :: _ Vector_base< int,std :: allocator< int> >> = {
_M_impl = {
< std :: allocator< int>> = {
< __ gnu_cxx :: new_allocator< int>> = {< No data fields>},< No data fields>},
std :: _ Vector_base< int,std :: allocator< int& > :: _ Vector_impl:
_M_start = 0x0,
_M_finish = 0x0,
_M_end_of_storage = 0x0
}
},< No data fields>}
(gdb)print copyOfNums [0]
$ 1 =(const int&)@ 0x0:无法访问地址0x0处的内存


解决方案

我不知道编译器能够优化多少,但在现实中,这个代码:

 矢量< int> copyNums(vector< int# nums){
int sizeNums = nums.size();
vector< int> copyOfNums;

for(int i = 0; i copyOfNums.push_back(nums [i]);
}

return copyOfNums;
}

与此代码具有相同的结果:

 矢量< int> copyNums(vector< int# nums){
return nums;
}



如果编译器正在进行这样的优化,优化, copyofNums 可以完全优化。


I'm learning to use the gdb debugger but having trouble accessing the values of a vector when that vector contains values copied from another vector. Ultimately the program below does what I want it to do (copies the values of "nums" into a new vector "copyOfNums"), but when I try to examine the value at copyOfNums[0] after executing the first for loop in "copyNums", the value in memory can't be accessed (see gdb session below).

Below copy nums has executed 1 loop of the for statement, so should copyNums[0] contain the address of the value pointed to by nums[0] or should it contain a reference to a new int (that is the same as the l-value pointed to by nums[0])? Regardless I would expect to be able to access the value, but as you can see from the following gdb output, it is inaccessible.

Breakpoint 1, copyNums (nums=@0x7fff5fbffad0) at main.cpp:10

(gdb) print nums[0]
$1 = (int &) @0x100103910: 2
(gdb) print copyOfNums[0]
$2 = (int &) @0x0: Cannot access memory at address 0x0
(gdb) 

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

vector<int> copyNums(vector<int> nums){
  int sizeNums = nums.size();
  vector<int> copyOfNums;

=>for (int i = 0; i < sizeNums; i++){
    copyOfNums.push_back(nums[i]);
  }

  return copyOfNums;
}

vector<int> getSmallNums(){

  vector<int> nums;
  nums.push_back(2);
  nums.push_back(1);
  nums.push_back(8);

  return nums;
}

int main (int argc, char *argv[]){
    vector<int> nums = getSmallNums();
    vector<int> copyOfNums = copyNums(nums);


    return 0;
}

Here is the gbd output after sort has returned. Looks like the copy contains a reference to a new location in memory, the l-value of which is 2 (as expected). Why was I unable to access copyOfNums[0] within the function copyNums?

(gdb) print nums[0]
$3 = (int &) @0x1001000e0: 2
(gdb) print copyOfNums[0]
$4 = (int &) @0x100103920: 2

EDIT: here is a more complete picture of the debugging session

(gdb) info locals
i = 1
sizeNums = 3
copyOfNums = {
  <std::_Vector_base<int,std::allocator<int> >> = {
    _M_impl = {
      <std::allocator<int>> = {
        <__gnu_cxx::new_allocator<int>> = {<No data fields>}, <No data fields>},
      members of std::_Vector_base<int,std::allocator<int> >::_Vector_impl:
      _M_start = 0x0,
      _M_finish = 0x0,
      _M_end_of_storage = 0x0
    }
  }, <No data fields>}
(gdb) print copyOfNums[0]
$1 = (const int &) @0x0: Cannot access memory at address 0x0

解决方案

I'm not sure how much a compiler will be able to optimize this away, but in reality, this code:

vector<int> copyNums(vector<int> nums){
  int sizeNums = nums.size();
  vector<int> copyOfNums;

  for (int i = 0; i < sizeNums; i++){
    copyOfNums.push_back(nums[i]);
  }

  return copyOfNums;
}

Has the same result as this code:

vector<int> copyNums(vector<int> nums){
  return nums;
}

If a compiler is doing an optimization like that, or a number of other possible optimizations, copyofNums could be completely optimized away.

这篇关于为什么不能使用“print”访问向量中的值在gdb调试器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆