空节点结构的 JNA 初始化

JNA initialization of empty node structures

我有节点结构(它包含下一个相同结构的值)。

struct Node {
    Node *nextElem;
    Element thisValue;
};  

我想在填充它的函数中传递空 (null) node.ByReference。

// 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);

所以如果我使用

Node.ByReference list = null;

当 C++ 端尝试读取 list 时,我得到 无效内存访问 错误,就像任何 null Pointer秒。 所以我正在尝试初始化 list。但在那种情况下,我必须初始化下一个节点和下一个节点......

我通过将 Node 包装在 PointerByReference:

中找到解决方案
// method declaration:
void AddElementToList(Element element, PointerByReference wrapedNodeInPointerByRef);

用法:

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()); 

为此创建构造函数:

public Node (Pointer value) {
 super(value);
 read();
} 

Node.ByValue 和 Node.ByReference 的构造函数我也有同样的方法。 这个例子是复杂程序的简化版本,具有更多的抽象,但希望没有丢失任何东西并且对某些人有所帮助。

一些思考:

  1. 如果PointerByReference可以有空实例,是否Structure.ByReference不可以?
  2. 不清楚为什么 Element.ByValue 像元素一样工作,但是当用 Element.ByValue 声明时它会导致无效的内存访问。