空节点结构的JNA初始化 [英] JNA initialization of empty node structures

查看:77
本文介绍了空节点结构的JNA初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have node structure (it contain value on next same structure).

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

我想传递一个空的(null)节点.通过引用来填充它.

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

所以,如果我使用

Node.ByReference list = null;

当C ++尝试读取列表时,我得到无效的内存访问错误,例如任何空的 Pointer . 因此,我正在尝试初始化列表.但是在那种情况下,我必须初始化下一个节点,下一个节点和...

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...

推荐答案

我通过将 Node 包装在 PointerByReference 中来找到解决方案:

I find out solution by wrapping Node in 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的构造函数我用相同的方式得到. 此示例是复杂程序的简化版本,具有更多抽象,但希望不会丢失任何内容,并且对某些人有所帮助.

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.

一些想法:

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

这篇关于空节点结构的JNA初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆