Re: need a tiny help with my SWIG'd program
- From: George Peter Staplin <georgepsSPAMMENOT@xxxxxxxxxxxx>
- Date: Thu, 28 Feb 2008 19:50:54 +0000 (UTC)
George Petasis wrote:
O/H George Peter Staplin ÎγÏ?αÏ?ε:
}
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 _?
Don't be too embarassed. :-) I made the mistake of using a _ prefix
too, before I learned better. I read some code that used it, and
thought it made sense. Some people even promote this technique on blogs
to the unknowing public.
It's not just variables. It's names in general. The system is free to
use _foo for macro names, functions, and global variables for the
implementation, and often does.
For instance a brief search of the MinGW Win32 API shows: _iob, _IOREAD,
_IOWRITE, etc. in stdio.h
Unix-like systems, such as GNU/Linux also have system libraries that
have this restriction for C and C++.
The way that C compilation units work, this is really the only way for
the implementation to do what it needs to do.
George
.
- 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
- Re: need a tiny help with my SWIG'd program
- From: George Petasis
- need a tiny help with my SWIG'd program
- Prev by Date: Re: need a tiny help with my SWIG'd program
- Next by Date: Re: New question on strings
- Previous by thread: Re: need a tiny help with my SWIG'd program
- Next by thread: tls question
- Index(es):
Relevant Pages
|
Loading