Quick Pointer Question

C

Crow

Alright....basically I have this:

Object **node;
..
..
..
node = &(*node)->getNextNode(); // <-- ERROR, where getNextNode
returns an Object *

is there any way to do that last statement without resorting to temp
variables i.e.

Object *node temp;
temp = getNextNode();
node = &temp;
 
A

Alan Johnson

Crow said:
Alright....basically I have this:

Object **node;
.
.
.
node = &(*node)->getNextNode(); // <-- ERROR, where getNextNode
returns an Object *

You are trying to store the address of a temporary object. Imagine what
happens in the following code:

int f() { return 5 ; }
// ...
int *v = &f() ; // Illegal

What am I trying to assign to v? The only thing that makes sense is
that I am trying to take the address of the temporary created to hold
the return value from f(). What happens when that statement ends and
the temporary is destroyed?
is there any way to do that last statement without resorting to temp
variables i.e.

Object *node temp;
temp = getNextNode();
node = &temp;

Same thing here, except you are copying the temporary created to hold
the return value into your own temporary variable first. What happens
when temp goes out of scope?

Alan
 
I

Ian Collins

Crow said:
Alright....basically I have this:

Object **node;
..
..
..
node = &(*node)->getNextNode(); // <-- ERROR, where getNextNode
returns an Object *

is there any way to do that last statement without resorting to temp
variables i.e.
Why bother? a temp often adds clarity.
 
R

Raju

node = &(*node)->getNextNode(); // <-- ERROR, where getNextNode returns an Object *
....
....Above temp is an Object-type variable *NOT* a pointer type
I doubt your function really returns a pointer to Object
 
C

Crow

Your right Raju, I missed that. It should have been:

Object *temp;
temp = getNextNode();
node = &temp;

Anyway, I get what your said Alan. That makes sense now. I had a
mental lapse dealing with pointers to pointers. Thanks for the tip.
 
H

Howard

Crow said:
Your right Raju, I missed that. It should have been:

Object *temp;
temp = getNextNode();

Did you mean:

temp = (*node)->getNextNode();

?
node = &temp;

Anyway, I get what your said Alan. That makes sense now. I had a
mental lapse dealing with pointers to pointers. Thanks for the tip.

You do realize that now node holds the address of the local variable temp,
right? Is that what you really want, to keep the address of another
(temporary) pointer?

I can't say I've seen much (if any) use of pointer-to-pointers within a
function. Usually, they're just passed as parameters, such as Object**
node, and then *node is operated on directly within the function. That's so
that you can modify the pointer passed to the function. And if that's what
you're really doing here, then when the function exits, node will be
pointing at a local variable that's going out of scope, and you'll be in
trouble.

(And what happened to the old pointer that node previously pointed to? Does
someone else own it? Was it leaked?)

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top