问题
I have node structure (it contain value on next same structure).
struct Node {
Node *nextElem;
Element thisValue;
};
I want to pass empty (null) node.ByReference in function that fills it.
// C++
Element element = ...; //fills in another function;
Node *list = NULL;
AddElementToList(element, &list);
// which declered as
void AddElementToList (Element element, Node * list) {...}
// Java
Element.ByValue element = ...; //fills great in another function in the same way ByReference (and reconstructed as ByValue),
//but initialize with trash in Pointers and without recurtion;
Node.ByReference list = null;
MyDll.INSTANCE.AddElementToList(element, list);
So if I use
Node.ByReference list = null;
I get Invalid memory access Error when C++ side try to read list, like for any null Pointers. So I'm trying initialize list. But in that case I have to init next node and next and...
回答1:
I find out solution by wrapping Node in PointerByReference:
// method declaration:
void AddElementToList(Element element, PointerByReference wrapedNodeInPointerByRef);
Usage:
Element.ByValue element = ...;
PointerByReference list = new PointerByReference();
MyDll.INSTANCE.AddElementToList(element, list); // yes, Element.ByValue puts in Element
// but to get **Node** from filled PointerByReference you should reparse it like:
Node node = new Node(list.getValue());
For that create constructor:
public Node (Pointer value) {
super(value);
read();
}
Constructors for Node.ByValue and Node.ByReference I have get in same way. This example is simplified version from complicated program with more abstractions, but hope nothing was lost and will helpful for somebody.
Some thinkings:
- If PointerByReference can has empty instanse, whether Structure.ByReference's can't?
- Not clear why Element.ByValue works like Element, but when declaration with Element.ByValue it couses to Invalid memory access.
来源:https://stackoverflow.com/questions/46707280/jna-initialization-of-empty-node-structures