

![]() | Start a set with this search |
![]() | Include this search in one of my sets |
![]() | Exclude this search from one of my sets |
![]() | Permalink to these results Paste this link in email or IM: |
| Atom feed for tracking future search results Paste this URL into your reader: |
8 messages in net.java.dev.jna.usersRe: [jna-users] call to sysinfo() cau...| From | Sent On | Attachments |
|---|---|---|
| Joel Uckelman | Apr 7, 2009 8:37 am | |
| Timothy Wall | Apr 7, 2009 8:47 am | |
| Joel Uckelman | Apr 7, 2009 9:05 am | |
| Kevin Burton | Apr 7, 2009 9:56 am | |
| Timothy Wall | Apr 7, 2009 9:58 am | |
| Joel Uckelman | Apr 7, 2009 12:19 pm | |
| Joel Uckelman | Apr 7, 2009 12:22 pm | |
| Kevin Burton | Apr 7, 2009 1:33 pm |

![]() | Permalink for this message Paste this link in email or IM: |
![]() | Permalink for this thread Paste this link in email or IM: |
| Atom feed for this thread Paste this URL into your reader: |
| Subject: | Re: [jna-users] call to sysinfo() causes crash | Actions... |
|---|---|---|
| From: | Timothy Wall (twal...@dev.java.net) | |
| Date: | Apr 7, 2009 9:58:49 am | |
| List: | net.java.dev.jna.users | |
On Apr 7, 2009, at 12:06 PM, Joel Uckelman wrote:
Thus spake Timothy Wall:
On Apr 7, 2009, at 11:38 AM, Joel Uckelman wrote:
Hi,
I'm trying to call the sysinfo() function from the Linux C library:
int sysinfo(struct sysinfo *info);
struct sysinfo { long uptime; /* Seconds since boot */ unsigned long loads[3]; /* 1, 5, and 15 minute load averages */ unsigned long totalram; /* Total usable main memory size */ unsigned long freeram; /* Available memory size */ unsigned long sharedram; /* Amount of shared memory */ unsigned long bufferram; /* Memory used by buffers */ unsigned long totalswap; /* Total swap space size */ unsigned long freeswap; /* swap space still available */ unsigned short procs; /* Number of current processes */ unsigned long totalhigh; /* Total high memory size */ unsigned long freehigh; /* Available high memory size */ unsigned int mem_unit; /* Memory unit size in bytes */ char _f[20-2*sizeof(long)-sizeof(int)]; /* Padding for libc5 */ };
char _f[] != char*, at least not in the context of a struct. You're only allocating a pointer, when you need a character array.
Aha. That was not clear to me from the documentation. From the C side, there's no difference between char[] and char*, and the JNA docs say to use String for char*.
Au contraire, on the C side there is indeed a difference between char[] and char*. Try replacing one with the other and watch the size of your structure change. The expressions are often interchangeable, but that doesn't mean there is no difference.
I changed the last element of the structure on the Java side to be this, to match the definition given in the C header:
public byte[] _f = new byte[20-2*NativeLong.SIZE-Native.getNativeSize(int.class)];
After that, it worked perfectly for me on 32-bit machines, but fails with this exception on 64-bit machines:
Exception in thread "main" java.lang.IllegalArgumentException: Arrays of length zero not allowed in structure
Two 8-byte longs and one 4-byte int are 20 bytes together, so that makes the byte array _f have zero length on a 64-bit machine. In C, that would just make _f point to the end of the struct.
Well, since in C you have two distinct structure definitions, you could do the same in Java. Alternatively, you can simply ensure the array is not zero length, since padding beyond the end of the structure will be ignored, unless you have an array of structures. Or you could make the final field an array as well and include the necessary padding in that.







