Re: need a tiny help with my SWIG'd program
- From: George Petasis <petasis@xxxxxxxxxxxxxxxxx>
- Date: Thu, 28 Feb 2008 21:15:15 +0200
O/H George Peter Staplin έγραψε:
Georgios Petasis wrote:O/H Mel ÎγÏαψε:the C function i am calling requires a:It is not so easy :-)
'char ** myvar"
what would I be passing as TCL var to this function ?
void junk(char **' myvar)
in tcl I would call something like:
junk <what should I put here>
many thanks for your help
char ** means an array of null terminated strings. There in nothing in tcl that can be converted to this type. So, you have to add to your header file some C code to generate such structures.
Hi,
That's not entirely accurate and overly general.
Here's an example:
#include <stdio.h>
int main () {
char c = 'a';
char *cp = &c;
char **cpp = &cp;
printf ("%c\n", **cpp);
return 0;
}
This explains more about the differences between arrays and pointers:
http://c-faq.com/aryptr/index.html
Arrays decay to pointers in some cases.
It's important to know the distinction because:
int main() {
char *a = "Hello World";
char b[] = "Hello World";
a[0] = 'a'; /*faults usually with read-only string literals/constants*/
b[0] = 'b'; /*works*/
printf ("%s %s\n", a, b);
return 0;
}
Yes, you are correct. Not enough information was given in the initial post, and I assumed the obvious usage, whithout making this clear :-)
More information is needed to be able to help more :-)
For example, in a similar situation I have added the following helper code:
#ifdef SWIG
%rename(String) array_string;
#endif
#if defined(ARRAY_STRING_CLASS)
class array_string {
char **array;
int _size;
public:
array_string(int size) {
_size=size;
array = (char **) Tcl_Alloc(sizeof(char*)*_size);
if (array) for(int i=0; i<_size;++i) array[i]=NULL;
Tcl_Alloc() will panic if it's unable to allocate memory. It never returns if the request can't be met, so the "if (array)" isn't necessary.
True :-) But its a habbit of mine to use checks when I allocate memory, even if I use tcl functions :-) Better safe than sorry :-)
}
void setitem(int i, char* value) {
if (!array || i < 0 || i >= _size) return;
if(array[i]) Tcl_Free(array[i]);
array[i]=Tcl_Alloc(sizeof(char)*(strlen(value)+2));
strcpy(array[i], value);
}
char *getitem(int i) {
if (!array || i<0 || i>=_size) return NULL;return array[i];
}
char** cast(void) {return array;}
~array_string() {
if (array) {
for(int i=0; i<_size; ++i) {
if (array[i]) Tcl_Free(array[i]);
}
Tcl_Free((char *)array);
}
}
};
#endif /* ARRAY_STRING_CLASS */
Them, from tcl you can do (assuming that you have enabled shadow classes):
I don't mean to be overly critical, but I believe your code is violating the C++ standard and possibly will break.
http://developer.mozilla.org/en/docs/C%2B%2B_Portability_Guide#Don.27t_use_reserved_words_as_identifiers
And I quote from that:
"According to the C++ Standard, 17.4.3.1.2 Global Names [lib.global.names], paragraph 1:
Certain sets of names and function signatures are always reserved to the implementation:
* Each name that contains a double underscore (__) or begins with an underscore followed by an uppercase letter (2.11) is reserved to the implementation for any use.
* Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace."
So I guess you could wrap it in a namespace, or rewrite it to not use an underscore prefix for _size. C has a similar restriction with underscore prefixes. Sometimes as an alternative programmers use a _ suffix for instance variables.
To say the truth, I didn't know about these limitations, although I know that usually c compilers rename functions by putting an _ infront of the function names. Is this true for all variables in all namespaces?
There is a reference in the titles about "global" names. Maybe this applies only to global symbols? In such a case, I am lucky, as the variable is inside a class, and not in the global namespace :-)
And hopefully, compilers are more forgiving than the stantards: at least visual C++ (all versions) and gcc (all versions) will not complain on this code (or issue a warning if ran with default options). I know this, because the above code was a copy-paste from an application I have written :-), which works under linux/windows...
Its embarassing to know that my code violates the stantard :-( I wish the visual c++ compiler issued more warnings to have fix this earlier.
So, we are not supposed to start names with _?
George
.
I hope this constructive criticism helps.
You know more about swig than I do. :-)
George
- Follow-Ups:
- Re: need a tiny help with my SWIG'd program
- From: George Peter Staplin
- Re: need a tiny help with my SWIG'd program
- References:
- need a tiny help with my SWIG'd program
- From: Mel
- Re: need a tiny help with my SWIG'd program
- From: Georgios Petasis
- Re: need a tiny help with my SWIG'd program
- From: George Peter Staplin
- need a tiny help with my SWIG'd program
- Prev by Date: Strange font/language issue on Solaris 10 and wish8.3
- Next by Date: Re: need a tiny help with my SWIG'd program
- Previous by thread: Re: need a tiny help with my SWIG'd program
- Next by thread: Re: need a tiny help with my SWIG'd program
- Index(es):
Relevant Pages
|