Re: need a tiny help with my SWIG'd program



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
.



Relevant Pages

  • Re: String reversing problem
    ... >> To Mr. Christian Bau: ... > Nothing wrong with my english. ... Declare char here, far of the semantically parent of it it ... And lots of compilers will ignore it (no ...
    (comp.lang.c)
  • Re: It works without stdio.h !!
    ... most C compilers will assume that a function that has no prototype is an ... extern int, and since you only specify functions that live in the C library, an unresolved ... never declare a variable of type char unless you well and truly believe you need to be ... strcpy, strcat, sprintf etc. with it. ...
    (microsoft.public.vc.mfc)
  • Re: gcc4 and lvalues
    ... > I am trying to compile a C program (that used to work fine with gcc 2.x.y, ... On the assignment to s, callocreturns a void* which is implicitly ... converted to char*. ... other compilers weren't catching these errors. ...
    (comp.std.c)
  • Private bytes of Client WMI Appln increases
    ... ptr is released during each iteration. ... disabled, the leak appers. ... *NameSpace, char *User, char *Passwd) ...
    (microsoft.public.win32.programmer.wmi)
  • Re: what does __foo means.
    ... >> Can anyone tell me does double underscore before a function mean? ... The _ is used to provide for a new namespace, ... identifiers with two leading underscores are reserved for ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)

Loading