2

Is there an easy way to get the native sizeof(int) from the Java VM running on a particular platform? The value I want is not Integer.SIZE, in particular - the size of a Java int, but rather what you'd get from sizeof(int) in C on the platform.

I need this because I'm using a particular library that reads and writes binary files, and trying to parse those files, whose interpretation depends in a particular way on the size of the machine int. I'd like it to be portable.

I get the impression that including JNA will give me that capability - but I'd rather not have to include a native library dependency (again, portability), and I don't want to play nasty games like the only solution I could come up with offhand - allocating many direct int buffers and looking at management/memory metrics before and after. That's a hack and not reliable...

BadZen
  • 4,083
  • 2
  • 25
  • 48
  • 3
    Fresh idea: If you want a C `sizeof(int)`, then you have to call C `sizeof(int)`. JVM is oblivious of what a particular C compiler would believe is the `int`. This is not "machine int" anyhow. What you probably want is machine bitness, if so, it is available through `System.getProperty("os.arch")`. – Aleksey Shipilev Dec 19 '14 at 15:37
  • Aleksey - That would not be portable, as each new target would need to have a case added to the code that parses the property. But, you got me on the right track here, so thanks! – BadZen Dec 19 '14 at 16:00

1 Answers1

1

A comment suggested a system property so I looked at the list of those - it turns out there is one that gives this value:

System.getProperty("sun.arch.data.model")
Michaël
  • 3,679
  • 7
  • 39
  • 64
BadZen
  • 4,083
  • 2
  • 25
  • 48
  • 1
    Sounds like that could be it, if that's what the OP was after. But you are the OP so I guess that must be the case :-) – Erwin Bolwidt Dec 20 '14 at 13:34