Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Seg fault with Tcl_RegExpRange

Status
Not open for further replies.

GrayGh0st

Programmer
Apr 6, 2004
2
US
Ok. I'm really not sure what I'm doing wrong here (hence the post). It may be blaringly obvious, but I can't see it. I always get a seg fault when I hit the function call Tcl_RegExpRange in this snippet of code:

string reg_1("(\\d+)(?:-(\\d+))?");
string fullReg(reg_1 + "(?::" + reg_1 + ")?");

Tcl_Interp *error = Tcl_CreateInterp();
Tcl_Obj
* ptrExpObj = Tcl_NewStringObj(fullReg.c_str(), fullReg.size());

Tcl_RegExp tclRegExp = Tcl_GetRegExpFromObj(error, ptrExpObj, TCL_REG_ADVANCED);

if(tclRegExp != 0)
{
const char *fullArg, *search;
fullArg = search = arg.c_str();

while(Tcl_RegExpExec(error, tclRegExp, search, fullArg) > 0)
{
const char **start = 0, **end = 0;

for(int j = 1; j <= 4; ++j)
{
int num;
Tcl_RegExpRange(tclRegExp, j, start, end);
memcpy(&num, *start, *end - *start);
tcout << "Num: " << num << std::endl;
}
search = *end;
}
}

I don't *think* I'm doing anything wrong here... But clearly I am if I'm seg faulting. Anywho, anything wrong with my syntax? Any help is appreciated! Thanks!

My apologies if this code loses it's formatting

Justin Miller
 
Just a thought:
Did you verified that your solution is not a C solution but a C++ solution (I think there are some incompatibilities between them)?

ulis
 
Not positive about that. BUT, I do think that the solution is stupidly simple. I haven't tried it yet, but a friend mentioned this:

instead of a char** start, it should simply be a char*.
And then, when I pass it to Tcl_RegExpRange, I should pass the address of the pointer: &start.

char** things scare me ;) I don't have a ton of experience with them. But his answer seems to make sense, too much sense in fact :)

Thanks for your help though! Much appreciated.

Justin
 
You are passing a pointer to a pointer to type char
with your char ** declaration. The man page for Tcl_
RegExpRange says:

"Tcl_RegExpRange may be invoked after Tcl_RegExpExec returns; it provides detailed information about what ranges of the string matched what parts of the pattern. Tcl_RegExpRange returns a pair of pointers in *startPtr and *endPtr that identify a range of characters in the source string for the most recent call to Tcl_RegExpExec. Index indicates which of several ranges is desired: if index is 0, information is returned about the overall range of characters that matched the entire pattern; otherwise, information is returned about the range of characters that matched the index’th parenthesized subexpression within the pattern. If there is no range corresponding to index then NULL is stored in *firstPtr and *lastPtr."

Everything else looks okay to me. The tcl_object interface
is not for the squeamish is it? Have you looked at the
definition for Tcl_Obj? Yikes.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top