Re: Overloaded vendor library routine: is this C++? Or very clever
C?
Jack Daly <jd1033@yahoo.com> writes:
On Sun, 14 May 2006 18:36:35 GMT, Keith Thompson <kst-u@mib.org>
wrote:
Jack Daly <jd1033@yahoo.com> writes:
wibble = (long)bar(); // shouldn't be possible, but...
printf("wibble is %d\n", wibble); // works!!
}
Is this possible in C? Or does the vendor library have to be C++? If
it is C++, how does this work? I'm compiling with gcc and I can't see
how this module could interface with a C++ class with overloaded
access routines.
You're assuming that the code might be C++ because you think it's
doing "overloading". You've misunderstood the meaning of the term.
Ok, to be more specific: I'm postulating that the vendor has supplied
a C++ library which has (at least) two copy assignment operators:
long operator=(const bar& rhs) {...}
bar* operator=(const bar& rhs) {...}
Somehow, this C code has linked with the C++ library and the
assignment 'wibble = (long)bar()' is using the first operator.
There's no need to assume C++ to explain the behavior you're seeing.
C code is perfectly capable of subverting the type system.
Here's a simple C program in which a function returning a pointer can
return either an actual pointer, or an integer converted to a pointer;
the caller knows what to expect based on the argument it passed to it.
#include <stdio.h>
#include <stdlib.h>
void *func(int n)
{
if (n == 0) {
return malloc(100);
}
else {
return (void*)n;
}
}
int main(void)
{
printf("func(0) returns %p\n", func(0));
printf("func(1) returns %d\n", (int)func(1));
return 0;
}
This is ugly, non-portable code that can easily fail if int and void*
are of different sizes -- or even if they're the same size. But it
may be similar to what your library is doing.
Have you tried asking the vendor?
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.