JNA initialization of empty node structures

て烟熏妆下的殇ゞ 提交于 2019-12-11 07:42:23

问题


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:

  1. If PointerByReference can has empty instanse, whether Structure.ByReference's can't?
  2. 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!