4 messages in net.java.dev.jna.usersRe: [jna-users] Wrong Structure Value...
FromSent OnAttachments
RWAD...@UP.COMJun 20, 2008 5:41 am 
Michael Brewer-DavisJun 20, 2008 10:59 am 
RWAD...@UP.COMJun 23, 2008 5:27 am 
Timothy WallJun 23, 2008 11:52 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-users] Wrong Structure Values PassedActions...
From:Michael Brewer-Davis (mich@tech4learning.com)
Date:Jun 20, 2008 10:59:09 am
List:net.java.dev.jna.users

RWAD@UP.COM wrote:

Point[] points = new Point[2]; points[0] = new Point(1,2); points[1] = new Point(3,4); Point[] structures = (Point[])new Point().toArray(points);

Not an expert, but I think you misunderstand this toArray call. The javadoc:

public Structure[] toArray(Structure[] array) Returns a view of this structure's memory as an array of structures. Note that this Structure must have a public, no-arg constructor. If the structure is currently using a Memory backing, the memory will be resized to fit the entire array.

I parse that as (a) the first element of the returned array will be "this" (b) subsequent elements of the returned array will be from "this"'s memory

This fits your data: (a) first point is Java allocated 0, 0 (b) second point is newly (C) allocated gibberish

The purpose would be to treat Structure* values passed from C as Structure[] values when appropriate. The array param just reuses that array, reduces that allocation.

One possible solution? Point p = new Point(1, 2); Point[] points = p.toArray(2); p[1].setPoint(3, 4); // or some other assignment

Maybe something more elegant.

michael