my aim create data structure should have following representation in memory:
int countofelements; // 4 bytes int time_1; // 4 bytes double price_1; // 8 bytes int time_2; // 4 bytes double price_2; // 8 bytes int time_3; // 4 bytes double price_3; // 8 bytes . . . . . int time_n; // 4 bytes, n = countofelements double price_n; // 8 bytes, n = countofelements
unfortunately, not free choose representation (field order , type) of structure in memory, because relates process on os. data structure intended placed on shared memory using winapi functions, , purpose of data structure data exchange. thus, i'll needed pointer data structure in java... so, effort realize data structure looks like:
import java.util.list; import java.util.arrays; import com.sun.jna.structure; public class tquote extends structure { public int time; public double price; @override protected list getfieldorder() { return arrays.aslist(new string[]{"time", "price"}); } } public class tsharedarray extends structure { public int countofelements; public tquote[] elements; public tsharedarray(int size) { super(); countofelements = size; elements = new tquote[size]; (int = 0; < size; i++) elements[i] = new tquote(); } @override protected list getfieldorder() { return arrays.aslist(new string[]{"countofelements", "elements"}); } }
though, attempts instantiate structure tsharedarray structure = new tsharedarray(10);
throws exception:
/* invalid structure field in class xxx$tsharedarray, field name 'elements' (class xxx$tquote): can't instantiate class xxx$tquote (java.lang.instantiationexception: xxx$tquote) java.lang.illegalargumentexception: invalid structure field in class xxx$tsharedarray, field name 'elements' (class xxx$tquote): can't instantiate class xxx$tquote (java.lang.instantiationexception: xxx$tquote) @ com.sun.jna.structure.validatefield(structure.java:962) @ com.sun.jna.structure.validatefield(structure.java:954) @ com.sun.jna.structure.validatefields(structure.java:972) @ com.sun.jna.structure.<init>(structure.java:186) @ com.sun.jna.structure.<init>(structure.java:180) @ com.sun.jna.structure.<init>(structure.java:167) @ com.sun.jna.structure.<init>(structure.java:159) @ xxx$tsharedarray.<init>(xxx.java:35) @ xxx.onstart(xxx.java:57) @ java.lang.thread.run(unknown source) */
java code in xxx.java line #35 super();
ideas, how fix it?
additionally, noticed 1 strange thing... let's assume, have 3 structures , each of these structures contains of 2 fields:
public class tintint extends structure { public int int1; public int int2; @override protected list getfieldorder() { return arrays.aslist(new string[]{"int1", "int2"}); } } public class tdbldbl extends structure { public double dbl1; public double dbl2; @override protected list getfieldorder() { return arrays.aslist(new string[]{"dbl1", "dbl2"}); } } public class tintdbl extends structure { public int int1; public double dbl1; @override protected list getfieldorder() { return arrays.aslist(new string[]{"int1", "dbl1"}); } }
the first 1 (tintint) have size expected of 2*4=8 bytes, second 1 (tdbldbl) have size expected of 2*8=16 bytes, third (tintdbl - it's same tquote) have size of 16 bytes instead of expected of 4+8=12. why?:
system.out.println("tintint.size()=" + new tintint().size()); // 8 bytes, ok system.out.println("tdbldbl.size()=" + new tdbldbl().size()); // 16 bytes, ok system.out.println("tintdbl.size()=" + new tintdbl().size()); // 16 bytes!!! why?
the amount of memory used increased in 8 byte blocks. more details see https://stackoverflow.com/a/321436/3915166
What is that for a blog post? Do you really expect anyone to read it?
ReplyDelete