473,405 Members | 2,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Typecasting portability in C

2
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

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. //This structure could contain anything
  4. struct CouldBeAnything
  5.    {
  6.    long ThisTimeItsALong;
  7.    char AndAChar;
  8.    };
  9.  
  10. struct MinimalStructure
  11.    {
  12.    int Signature;
  13.    };
  14.  
  15.  
  16. struct LargerStructure
  17.    {
  18.    int Signature;
  19.    struct CouldBeAnything SomeStructure;
  20.    };
  21.  
  22. int main ()
  23.    {
  24.    struct LargerStructure SomeLargeStructure;
  25.  
  26.    SomeLargeStructure.Signature = 112;
  27.    SomeLargeStructure.SomeStructure.ThisTimeItsALong = 1;
  28.    SomeLargeStructure.SomeStructure.AndAChar = 'a';
  29.  
  30.    struct MinimalStructure* Minimal = ( struct MinimalStructure* ) &SomeLargeStructure;
  31.  
  32.    printf( "Signature = %d\n", Minimal->Signature );
  33.    return 0;
  34.    }
Jan 28 '07 #1
0 1135

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Kapil Khosla | last post by:
Hi, I have been trying to understand this concept for quite sometime now somehow I am missing some vital point. I am new to Object Oriented Programming so maybe thats the reason. I want to...
2
by: Arun Prasath | last post by:
Hi all, I have the following question regd pointer typecasting. Is the following type of pointer typecasting valid? #define ALLOC(type,num) ((type *)malloc(sizeof(type)*num)) /*begin...
63
by: andynaik | last post by:
Hi, Whenever we type in this code int main() { printf("%f",10); } we get an error. We can remove that by using #pragma directive t direct that to the 8087. Even after that the output is...
11
by: Vinod Patel | last post by:
I have a piece of code : - void *data; ...... /* data initialized */ ...... struct known_struct *var = (struct known_struct*) data; /*typecasting*/ How is this different from simple...
93
by: roman ziak | last post by:
I just read couple articles on this group and it keeps amazing me how the portability is used as strong argument for language cleanliness. In my opinion, porting the program (so you just take the...
3
by: jdm | last post by:
In the sample code for the SortedList class, I see the use of a string typecasting macro consisting of a single letter "S". i.e.: Sortedlist->Add(S"Keyval one", S"Item one"); Now I have...
12
by: bwaichu | last post by:
What is the best way to handle this warning: warning: cast from pointer to integer of different size I am casting in and out of a function that requires a pointer type. I am casting an...
12
by: chadsspameateremail | last post by:
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...
26
by: Nishu | last post by:
Hi All, Is it valid in C to typecast a pointer? eg. code snippet... considering int as 16 bit and long as 32 bit. int *variable, value; *((long*)variable)++ = value; *((long*)variable)++...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.