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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tcl_LinkVar

Status
Not open for further replies.

nater

Programmer
Jun 14, 2002
21
CA
Also, I'm attempting to link a C++ string to a TCL variable, and can't seem to do it. With the following code, I get strange behavior, such as the string being set to the *name* of the TCL variable. I assume I'm being an idiot and confusing the pointers or something, but I can't get it to work, and I'm starting to wonder if there might be a bug with LinkVar..
Now feel free to tell me why I'm an idiot. =)

...
char *statusString;
statusString = Tcl_Alloc(6);

Tcl_LinkVar(interp, "status", statusString, TCL_LINK_STRING);

statusString = "WHY??";
...
 
I believe that this line is your problem:

Code:
statusString = "WHY??";

You just overwrote your pointer, so statusString is now pointing to some statically allocated memory instead of the memory allocated by Tcl_Alloc(). And as the Tcl_LinkVar() documentation says when linking to string values:

"The C variable is of type char *. If its value is not null then it must be a pointer to a string allocated with Tcl_Alloc."

So, you'll want to do something like this instead:

Code:
strcpy(statusString,"WHY??");
- Ken Jones, President
Avia Training and Consulting
866-TCL-HELP (866-825-4357) US Toll free
415-643-8692 Voice
415-643-8697 Fax
 
Thanks.. I actually realized that stupidity before I saw your post.. but it uncovered a larger problem. When I remove the attempt to assign a value to the string there, the program compiles fine, but then LinkVar tries to read an invalid memory location. Apparently in order to get around this you have to create a pointer to the original string handle, like so:

char statusString[6] = "WHY??";
char *statusStringPtr = statusString;

Tcl_LinkVar(interp, "status", (char *)&statusStringPtr, TCL_LINK_STRING);

It was probably designed like this to avoid the original problem I was having. This way if you want to modify the string, you can deallocate the original (statusString[6]), and point statusStringPtr to a new string. Of course, as you pointed out, you can also just use strcpy.
In any event, the linking does not seem to work without this double pointer deal..

 
Ack.. my bad. That last one.. worked, but I didn't really understand why. It should have been more like this. The "sizeof(char)" part is unnecessary, as you know it's 1 byte, but it makes it a bit easier to follow.. (At least, if you're me.) Basically what the linkvar function wants is a pointer to the string pointer.

Just in case anybody's actually reading this. =)

char *statusString = Tcl_Alloc(100 * sizeof(char));
char **statusStringPtr = (char **)Tcl_Alloc(sizeof(char *));
*statusStringPtr = statusString;

strcpy (statusString, "Start string");

Tcl_LinkVar(interp, "end", endFlg, TCL_LINK_INT);
Tcl_LinkVar(interp, "status", (char *)statusStringPtr, TCL_LINK_STRING);

 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top