G
Guest
What I am trying to do is implement polymorphism in C. Why? This is to
build a library which will be a C library and callable from C.
However, I want to have polymorphic functions which are callable from
outside the library. Specifically I want to have functions like Show()
which will take a pointer to an object and do something different
based on the type object passed to it. Of course this would be trival
in C++ but I'm restricted to use C.
OK so my current idea for something like the polymophic Show()
routine: Since all the objects are created inside the library I can
attach to each object a signature. The Show() routine would accept a
void* but then typecast it and get the signature out of the object.
Since each object is a structure (and created and defined inside the
library) I would append this signature to the front of each and every
struct. The signature would likely be something like an integer. So
each and every object would look like:
struct SomeObjectType {
int signature;
.....//bunch other other stuff
};
The Show() routine would typecast the void* to the following
structure:
struct {
int signature;
}
So my Show() and similar polymorphic routines would get the object and
ultimatly open it and look at the first field which would always be
the signature object. Based on what it found there (the value of
signature) it would do something different. Makes sense? I hope so.
So I have implemented some prototype code on this and it does work but
what I was wondering is how portable is this? The structure's first
item (the signature) will always be the same this I can guarantee but
again what about portability. Really I'm converting from a pointer to
one type, to a void*, then to a pointer to another type (for signature
extraction). Certainly the first object in the final structure will be
the same as the first object in the original structure and the same
size as well. But does that make it portable?
Is this legal C as defined by the standard? Is this going to work
across platforms? I hope you see my dilemma the code appears to work
on my system. The code below happily prints the expected output of
112. but I don't know if it's guaranteed to work everywhere and how
portable the library functions will be because of it?
Is this defined in any C standard anywhere what will happen and what
about general portability concerns?
In the example below I defined a structure CouldBeAnything and filled
it with a long and a char but really not only that structure but
anything after that initial signature integer will be different from
object to object. The only thing I can guarantee is that the first
item will be that signature integer on each object. Will this
conversion work? Is it portable? It works on my machine but does that
mean it will work in general? Thank you.
---
//On the code below when run on my machine it happily prints out 112
//No warnings are given by the gcc complier with warning flag -Wall
#include <stdio.h>
//This structure could contain anything
struct CouldBeAnything
{
long ThisTimeItsALong;
char AndAChar;
};
struct MinimalStructure
{
int Signature;
};
struct LargerStructure
{
int Signature;
struct CouldBeAnything SomeStructure;
};
int main ()
{
struct LargerStructure SomeLargeStructure;
SomeLargeStructure.Signature = 112;
SomeLargeStructure.SomeStructure.ThisTimeItsALong = 1;
SomeLargeStructure.SomeStructure.AndAChar = 'a';
struct MinimalStructure* Minimal = ( struct MinimalStructure* )
&SomeLargeStructure;
printf( "Signature = %d\n", Minimal->Signature );
return 0;
}
build a library which will be a C library and callable from C.
However, I want to have polymorphic functions which are callable from
outside the library. Specifically I want to have functions like Show()
which will take a pointer to an object and do something different
based on the type object passed to it. Of course this would be trival
in C++ but I'm restricted to use C.
OK so my current idea for something like the polymophic Show()
routine: Since all the objects are created inside the library I can
attach to each object a signature. The Show() routine would accept a
void* but then typecast it and get the signature out of the object.
Since each object is a structure (and created and defined inside the
library) I would append this signature to the front of each and every
struct. The signature would likely be something like an integer. So
each and every object would look like:
struct SomeObjectType {
int signature;
.....//bunch other other stuff
};
The Show() routine would typecast the void* to the following
structure:
struct {
int signature;
}
So my Show() and similar polymorphic routines would get the object and
ultimatly open it and look at the first field which would always be
the signature object. Based on what it found there (the value of
signature) it would do something different. Makes sense? I hope so.
So I have implemented some prototype code on this and it does work but
what I was wondering is how portable is this? The structure's first
item (the signature) will always be the same this I can guarantee but
again what about portability. Really I'm converting from a pointer to
one type, to a void*, then to a pointer to another type (for signature
extraction). Certainly the first object in the final structure will be
the same as the first object in the original structure and the same
size as well. But does that make it portable?
Is this legal C as defined by the standard? Is this going to work
across platforms? I hope you see my dilemma the code appears to work
on my system. The code below happily prints the expected output of
112. but I don't know if it's guaranteed to work everywhere and how
portable the library functions will be because of it?
Is this defined in any C standard anywhere what will happen and what
about general portability concerns?
In the example below I defined a structure CouldBeAnything and filled
it with a long and a char but really not only that structure but
anything after that initial signature integer will be different from
object to object. The only thing I can guarantee is that the first
item will be that signature integer on each object. Will this
conversion work? Is it portable? It works on my machine but does that
mean it will work in general? Thank you.
---
//On the code below when run on my machine it happily prints out 112
//No warnings are given by the gcc complier with warning flag -Wall
#include <stdio.h>
//This structure could contain anything
struct CouldBeAnything
{
long ThisTimeItsALong;
char AndAChar;
};
struct MinimalStructure
{
int Signature;
};
struct LargerStructure
{
int Signature;
struct CouldBeAnything SomeStructure;
};
int main ()
{
struct LargerStructure SomeLargeStructure;
SomeLargeStructure.Signature = 112;
SomeLargeStructure.SomeStructure.ThisTimeItsALong = 1;
SomeLargeStructure.SomeStructure.AndAChar = 'a';
struct MinimalStructure* Minimal = ( struct MinimalStructure* )
&SomeLargeStructure;
printf( "Signature = %d\n", Minimal->Signature );
return 0;
}