Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot call function with reference parameter in gdb

Tags:

c++

gdb

For this function:

void foo_ref(const int& i)
{
  cout << i << endl;
}

It's failed when I call it in gdb:

(gdb) call foo_ref(5)
Attempt to take address of value not located in memory.

Of course, in this simple example there's no need to use reference as parameter. If I use a normal "int", no problem then.
Actually the real example is a template function, like this:

template<class T>
void t_foo_ref(const T& i)
{
  cout << i << endl;
}

When "T" is "int", I have the problem mentioned above.

Is it a bug in gdb? Or is it possible I could call such function in gdb?

like image 270
vicshen Avatar asked May 05 '12 09:05

vicshen


Video Answer


1 Answers

It is possible, though not in an intuitive fashion (I would still classify this as a bug).

You need an actual memory region (a variable, or something heap-allocated).

(gdb) p (int *) malloc(sizeof(int))
$8 = (int *) 0x804b018
(gdb) p * (int *) 0x804b018 = 17
$9 = 17
(gdb) p t_foo_ref<int>((const int&) * (const int *) 0x804b018 )
17
$10 = void
(gdb)
like image 121
n. 1.8e9-where's-my-share m. Avatar answered Sep 21 '22 10:09

n. 1.8e9-where's-my-share m.