[英]c++ changing a pointers value that's passed as a function parameter
I working with a library where I need to send a pointer to an object to a function.. the problem is that I need to change what this pointer points to in the function itself, and I don't really know how I can overcome this problem since pointers are passed-as-value.. 我在一个库中工作,我需要向该函数的对象发送指针。问题是我需要更改该指针在函数本身中指向的内容,我真的不知道该如何克服这个问题。问题,因为指针是按值传递的。
struct.cpp struct.cpp
struct MyStruct {
Node* previous;
...
};
main.cpp main.cpp
int main(...) {
MyStruct* m = new MyStruct;
m->previous = NULL;
...
while (traversing) {
library_function((AFUNPTR) myFunction, (void*) m->previous);
}
function.cpp function.cpp
void myFunction(void* node_from_lib, void* point_to_previous) {
Node* current = (Node*) node_from_lib;
Node* previous = (Node*) point_to_previous;
if (previous != NULL) {
...
}
previous = current;
}
My problem is that I need to traverse through all the nodes, but this way previous will be sent into myFunction
pointing to NULL
all the time.. I've tried to use Node** previous
and assign the "new" previous like *previous = current;
我的问题是我需要遍历所有节点,但是这种方式会将前一个一直发送到指向
NULL
myFunction
。.我试图使用Node** previous
并分配“ new”上一个,例如*previous = current;
but I'm still getting a seg-fault. 但我仍然遇到段错误。
The library I'm using is Intel-PIN and I'm trying to instrument an instruction and chain them into a graph, although I've stripped away everything PIN from the example since I think this is a general c++ problem? 我正在使用的库是Intel-PIN,并且尽管我已将示例中的所有PIN剥离掉了,因为我认为这是一个通用的c ++问题,但我正在尝试插入一条指令并将它们链接成图形。
Here is my attempt with a pointer to a pointer... 这是我对指针的尝试...
struct.cpp struct.cpp
struct MyStruct {
Node** previous;
...
};
main.cpp main.cpp
int main(...) {
MyStruct* m = new MyStruct;
*(m->previous) = NULL;
...
while (traversing) {
library_function((AFUNPTR) myFunction, (void*) m->previous);
}
function.cpp function.cpp
void myFunction(void* node_from_lib, void** point_to_previous) {
Node* current = (Node*) node_from_lib;
Node** previous = (Node**) point_to_previous;
if ((*previous) != NULL) {
(*previous)->memberFunc();
...
}
*previous = current;
}
C: C:
int main(...) {
MyStruct* m = new MyStruct;
m->previous = NULL;
...
while (traversing) {
library_function((AFUNPTR) myFunction, &m->previous);
}
here, pass the m->previous
via pointer (the pointer is then of type Node**
, that can be reinterpreted to void*
and back without losing anything. The function expects void*
.) 在这里,通过指针传递
m->previous
(指针的类型为Node**
,可以将其重新解释为void*
然后返回而不会丢失任何内容。该函数期望void*
。)
function.cpp
void myFunction(void* node_from_lib, void* point_to_previous) {
Node* current = (Node*) node_from_lib;
Node* previous = * ((Node**)point_to_previous); // cast for reading - assume point_to_previous is pointer to pointer, and retrieve the pointer
if (previous != NULL) {
...
}
previous = current;
*((Node**)point_to_previous) = previous; // assume, point_to_previous is pointer to pointer, and change the pointer
}
Here, you can cast back the void*
to Node**
. 在这里,您可以将
void*
返回给Node**
。
Here, you pass the pointer to node pointer ( Node**
) as a second parameter. 在这里,您将指针作为第二个参数传递给节点指针(
Node**
)。 It is reinterpreted as void*
, but it is just semantic. 它被重新解释为
void*
,但这只是语义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.