Tool for sharing C data-structures with Java

R

Rick Giuly

I'm thinking about making a program in java that will allow you to use
C data-structures (probably read from a network) easily:

My question: has anyone already done this?

here's the approach that I'm thinking of:

-------
// c code
struct Person
{
int size;
int height;
char firstinitial;
}
--------
// java code
//description in java:
String[] PersonDescription =
{ "int", "size",
"int", "height",
"char", "firstinitial"
}
-----------
then you could have get/set methods that allowed this sort of thing in
java

data = (something from a c program)
data.setType(PersonDescription)'

x = data.getInt("size") // this would figure out the
// offset and give you the correct value




(Please go easy on my syntax, I use lots of languages.)

-Rick
 
M

Michael Borgwardt

Rick said:
I'm thinking about making a program in java that will allow you to use
C data-structures (probably read from a network) easily:

C structs are not portable, because data type lenghts, endianness and especially
alignments differ between platforms and compilers.
 
C

Chris Uppal

Michael said:
C structs are not portable, because data type lenghts, endianness and
especially alignments differ between platforms and compilers.

That just means that you'd need some way of parameterising the code with
settings reflecting the known behaviour of some specific compiler(s).
Perfectly doable, and even sensible.

I agree that you don't want to get into a position where you are dependent on
the unspecified behaviour of a C compiler in the first place. But if you /are/
in that position then centralising and encapsulating the required knowledge in
(the configuration of) some tool or library seems like a Good Idea to me.

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,059
Members
48,769
Latest member
Clifft

Latest Threads

Top