2 messages in net.java.dev.jna.usersRe: JNA: Modulo by Zero in Structure
FromSent OnAttachments
Timothy WallFeb 13, 2008 11:44 am 
Timothy WallFeb 14, 2008 7:55 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: JNA: Modulo by Zero in StructureActions...
From:Timothy Wall (twal@dev.java.net)
Date:Feb 13, 2008 11:44:34 am
List:net.java.dev.jna.users

On Feb 13, 2008, at 11:59 AM, Nikolas Lotz wrote:

hi timothy,

i frequently fetch arrays of structures, which looks something like this:

--- snip -------------------------------------------------------

public class Foo extends Structure {

public int bar = 0;

public static Structure[] getArray(Pointer p, int size) { Foo obj = new Foo(); obj.useMemory(p); obj.read(); return obj.toArray(size); }

}

--- /snip ------------------------------------------------------

everything works fine, but when there are no items to fetch i get a 'division by zero'.

The argument to Structure.toArray(int) must be greater than zero.

this is actually a 'modulo by zero' in the class 'com.sun.jna.Structure' in line 770. am i doing something wrong or should there be a check for 'fieldAlignment' like in the snippet below ?

--- snip -------------------------------------------------------

if ((fieldAlignment != 0) && ((calculatedSize % fieldAlignment) != 0)) { [...] }

--- /snip ------------------------------------------------------

Field alignments will always be greater than or equal to "1"; I don't know how you would get a zero. Please put together a JUnit test which exhibits the error.