Calling gdb function on std :: vector from local scope results in error
I am testing a function vect_dbg(vector<int>)
that returns the contents of an STL vector as a C string, so I can call it with gdb output vect_dbg(test)
or put it in a clock.
#include<vector>
using namespace std;
int main()
{
vector<int> test; //breakpoint; output vect_dbg(test) ends debugging.
for (int i=0;i<=10;i++) test.push_back(i);
return 0;
}
#include<sstream>
const char* vect_dbg(const vector<int> &V)
{
stringstream ss;
for (int i=0;i<V.size();i++)
ss<<"V["<<i<<"]:"<<V[i]<<' ;'; //line 16
static char sEnu[256];
ss.getline(sEnu,256);
return sEnu;
}
Unfortunately, it output vect_dbg(test)
interrupts debugging.
The program being debugged was signaled when in a function called from GDB. GDB has restored the context to what it was before the call. To change this behavior, use "set unindonsignal off". Evaluating an expression containing a function (vect_dbg (std :: vector> const &)) will be canceled. The received program signal SIGSEGV, segmentation fault. 0x004014b1 in vect_dbg (V = @ 0x22ff34) on main.cpp: 16
I found this function works if I do test
global, but I couldn't find a solution for a locally defined vector. How can I solve this? Many thanks.
EDIT: Mostly solved, the answer was pretty obvious, the vector was not initialized at the breakpoint. Now I'm wondering if this can be detected in vect_dbg
. The alternative I have found is static declaration vector<int>
.
source to share