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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With