0

I have this structure in c

 typedef struct { uint32_t id; uint32_t dateTime; uint32_t deviceID; union { char userID[BS2_USER_ID_SIZE]; uint32_t ioDeviceID; }; union { uint16_t code; struct { uint8_t mainCode; uint8_t subCode; }; }; uint8_t param; uint8_t image; } BS2Event; 

And this java class to map the struct:

 public class BS2Event extends Structure{ public int id; public int dateTime; public int deviceID; public int param; public int image; } 

How can I map unions in the mapping java class?

7
  • One option is to make a normal class, with separate fields for each union field, and some kind of indication of what's stored. However, this won't use the same memory for both. Another option is to store one field, an Object, and have it point to an object of the appropriate type. Commented Jun 14, 2017 at 17:08
  • Java doesn't actually support what a union does. You cannot overlay one type like a Foo or double over the same memory location as another, like a Bar or long. This is too low-level for Java. You can convert representations like with docs.oracle.com/javase/8/docs/api/java/lang/…, but that doesn't overlay memory. What would you use such a feature for anyway? Commented Jun 14, 2017 at 17:12
  • 1
    @LewBloch - clearly to store either code or mainCode + subCode in the same location - same as c ;) Commented Jun 14, 2017 at 18:50
  • 1
    This question seems fairly clear and valid, at least to someone with significant experience in both C and Java. Anyone care to explain the downvotes or closing votes? Commented Jun 14, 2017 at 19:38
  • Very funny, @KevinDTimm, but I was looking for a serious answer. Commented Jun 14, 2017 at 23:07

2 Answers 2

4

Java does not allow multiple fields to occupy the same memory. There is no direct analog of a C union in Java. So you can't:

  • Save memory by having multiple fields occupy the same space, when only one is appropriate at any given time.
  • Access one stored value in different representations just by accessing another field in the union (as your second union appears to be intended). Even in C++, this is technically undefined behavior, though some compilers support it.

That said, you can translate code that includes a C union to Java code.

Multiple fields, not saved in same memory

One way is to have multiple fields in a class. Memory will be allocated for each field, even though at most one will be used at any one time.

public class BS2Event { ... // Only one of these can be set char[] userID = new char[BS2_USER_ID_SIZE]; int ioDeviceID; ... } 

Single field of base class or interface type

Another way is to represent the union with a single Object or interface pointer that references the one current representation. Memory is allocated within the class only for a single reference, but additional memory is required for the referenced object.

public class BS2Event { // Ordinary struct fields ... private IEntityID m_userOrDeviceID; ... } 

Now, m_userID can point to either a UserID or a DeviceID.

public interface IEntityID { ... } public class UserID implements IEntityID { private char[] userID = new char[BS2_USER_ID_SIZE]; ... } private class DeviceID implements IEntityID { private int ioDeviceID; ... } 

One representation, accessed in multiple ways

If you have one representation that you want to access in multiple ways, you can do that with methods rather than fields.

public class BS2Event { // Ordinary struct fields ... private Code m_code; ... } private class Code { private int code; public int getCode() { return code; } public short getMainCode() { ... } public short getSubCode() { ... } } 
Sign up to request clarification or add additional context in comments.

1 Comment

Java does not allow multiple fields to occupy the same memory. A bit late here, but I'll just note that if you're worried that much about memory usage, you wouldn't be using Java in the first place...
0

You can map a struct directly using: http://javolution.org/apidocs/javolution/io/Struct.html It works very well (supports packed-structs, different byte-ordering). Click the link to see an example of how to use it. This library also supports unions. I have used it in production for many years without problems.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.