I am testing a function vect_dbg(vector<int>) that returns the contents of a STL vector as a C string, so I can call it with gdb output vect_dbg(test) or put it in a watch.
#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, output vect_dbg(test) breaks the debugging.
The program being debugged was signaled while in a function called
from GDB. GDB has restored the context to what it was before the call.
To change this behavior use “set unwindonsignal off” Evaluation of the
expression containing the function (vect_dbg(std::vector > const&)) will be abandoned. Program received
signal SIGSEGV, Segmentation fault. 0x004014b1 in vect_dbg
(V=@0x22ff34) at main.cpp:16
I discovered that the function works if I make test global, but i couldn’t figure out a solution for the locally defined vector. How can i solve this? Many thanks.
EDIT: Mainly solved, the answer was pretty obvious, the vector wasn’t initialized at breakpoint. Now i am wondering if it’s possible to detect that in vect_dbg.
An alternative i found is to declare the vector<int> static.
Put breakpoint one line after (on
forloop). The variabletestwas not yet initialized when you trying to use it.