cdeist
cdeist

Reputation: 229

Why can't I access value in a vector using "print" in gdb debugger?

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

Upvotes: 4

Views: 4207

Answers (2)

Bill Lynch
Bill Lynch

Reputation: 81976

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.

Upvotes: 3

Mahesh
Mahesh

Reputation: 34625

Let me answer part of your question.

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])?

Terminology you used ( new, reference ) seems confusing to me.

copyNums[0] contains a copy of the value at num[0]. &nums[0] is different from &copyNums[0]. And nums[0] contains a value of int type. So, copyNums[0] neither contains the address of the location nums[0] nor it contains a reference to a new int.

Upvotes: 0

Related Questions