2017-10-12 69 views
1

我有節點結構(它包含下一個相同結構的值)。JNA初始化空節點結構

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

我想在函數中傳遞empty(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++方面嘗試讀取列表,像任何空指針秒。 所以我想初始化列表。但在這種情況下,我必須初始化下一個節點和明年和...

+0

你能展示C++函數的聲明是什麼,以及你將如何在C++中調用它? – cubrr

+0

啊,但你似乎需要一個指向一個節點的空指針的指針。您當前正在傳遞空指針。 – cubrr

+0

你確定AddElementToList不是'(Element element,Node ** list)'嗎? – cubrr

回答

1

我在包裝節點 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能有空instanse,是否Structure.ByReference的不能?
  2. 不清楚爲什麼Element.ByValue像Element一樣工作,但是當使用Element.ByValue聲明時,它會導致無效的內存訪問。