c ++ cambiando el valor de un puntero que se pasa como un parámetro de función
Frecuentes
Visto 367 veces
2
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..
estructura.cpp
struct MyStruct {
Node* previous;
...
};
main.cpp
int main(...) {
MyStruct* m = new MyStruct;
m->previous = NULL;
...
while (traversing) {
library_function((AFUNPTR) myFunction, (void*) m->previous);
}
función.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
señalando a NULL
all the time.. I've tried to use Node** previous
and assign the "new" previous like *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?
Here is my attempt with a pointer to a pointer...
estructura.cpp
struct MyStruct {
Node** previous;
...
};
main.cpp
int main(...) {
MyStruct* m = new MyStruct;
*(m->previous) = NULL;
...
while (traversing) {
library_function((AFUNPTR) myFunction, (void*) m->previous);
}
función.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;
}
1 Respuestas
0
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*
.)
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*
a Node**
.
Here, you pass the pointer to node pointer (Node**
) as a second parameter. It is reinterpreted as void*
, but it is just semantic.
Respondido el 28 de enero de 14 a las 19:01
Thanks a lot! I guess this is something one is supposed to know by heart, but being a Java/Python/PHP developer for the past years this sh*t is magic - user3235200
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas c++ c pointers or haz tu propia pregunta.
Node** is the way to go. Show us what you tried to do with it. - Idov
A pointer to pointer is what you need. Since you said that you tried that and it didn't work for you, can you post some of the code that you did that made it break? - divinas