2017-10-12 16 views
1

노드 구조가 있습니다 (다음 동일한 구조에 값이 들어 있습니다).빈 노드 구조의 JNA 초기화

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

빈 (null) node.ByReference를 채우는 함수에 전달하려고합니다. C++ 측면에 null에 대한 포인터들처럼, 목록을 읽으려고 할 때

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

를 사용하는 경우. 그래서 초기화하려고합니다 목록. 그러나이 경우에 나는

+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의 생성자 나는 같은 방법으로 얻는다. 이 예제는 추상화 된 복잡한 프로그램에서 단순화 된 버전이지만 아무 것도 손실되지 않고 누군가에게 도움이되기를 바랍니다.

일부 thinkings :

  1. 하면 PointerByReference 수 있습니다 빈 instanse, Structure.ByReference의 수 없습니다 여부?
  2. Element.ByValue가 Element와 같은 이유는 분명하지 않지만 Element.ByValue로 선언하면 잘못된 메모리 액세스가 발생합니다.