Cannot call function with reference parameter in gdb

For this function:

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

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 is no need to use the link as a parameter. If I use the usual "int", then there is no problem.
Actually a real example is a template function, for example:

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

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

Is this a bug in gdb? Or maybe I could call such a function in gdb?

+4
source share
2 answers

Perhaps, although not intuitively (I would still classify this as an error).

You need a real memory area (variable or some kind of heap).

 (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) 
+11
source

5 is a literal, and when you pass it to a function, the compiler tries to store it in the address allocated to the parameter of function i. But since I am a link, there is no place where 5 can be stored in memory, thus your mistake.

0
source

Source: https://habr.com/ru/post/1410832/


All Articles