passing pointer to a function

Mar 1, 2012 at 10:30pm
Hi,

I am completely confused now when I tested the following code.
The last print statement always gave me 0.
Can anyone explain me why?

#include <iostream>

class Stack {
public:
Stack() {next = 0;}
~Stack() {}
int data;
Stack* next;
};

void newStack(Stack* s) {
s = new Stack;
std::cout << "addr of s in newStack function : " << s << std::endl;
}

int main() {

Stack* s = 0;

newStack(s);

std::cout << "addr of s : " << s << std::endl;


return 0;
}
Mar 1, 2012 at 10:36pm
closed account (zb0S216C)
A few things:

1) Don't overload the destructor if it doesn't do anything productive. It's common practice to use the destructor for the deallocation of reserved memory.

2) I see a call to new but no call to delete.

3) Use code tags.

As for your problem, which isn't a problem, s evaluates to the address it points to, which is null (initialisation stage of s), which is effectively the same as 0; thus, std::cout yields 0.

Wazzak
Last edited on Mar 1, 2012 at 10:38pm
Mar 1, 2012 at 10:39pm
Parameter Stack *s declared in your function newStack is a local object that will deleted after execution of the function.

You should redefine your function the following way

1
2
3
4
5
6
Stack * newStack() 
{
   s = new Stack;
   std::cout << "addr of s in newStack function : " << s << std::endl;
   return ( s );
}


and in main use it as

Stack *s = newStack();
Last edited on Mar 1, 2012 at 10:41pm
Mar 1, 2012 at 10:39pm
Stack* s = 0;

Because here you are creating a stack pointer s which points to the value of 0, not to any specific address. When you pass your stack pointer from main to newStack, newStack then assigns the passed pointer to a random place[an address] in memory large enough to hold a Stack object. [this is what happens when you use the new operator]






Last edited on Mar 1, 2012 at 10:46pm
Mar 1, 2012 at 10:41pm
Thanks for the quick reply.

What I am confused here is that I am pssing a pointer to a function where a new memory is assigned. So after calling this function, the variable s should have a pointer to that address, right?

Thanks,
dwjang
Mar 1, 2012 at 10:45pm
closed account (zb0S216C)
Yes. new yields a pointer to the allocated memory. s is then assigned to the address where the allocated memory resides. This is called transferring memory ownership, which means giving allocated memory to another pointer.

Wazzak
Mar 1, 2012 at 10:49pm
So then, why after calling newStack(Stack* s) function, s is still 0?
Mar 1, 2012 at 11:00pm
closed account (zb0S216C)
I'm not entirely sure, but you can fix that by making s (the parameter) a reference to a pointer (*&) or a pointer-to-a-pointer.

Wazzak
Mar 1, 2012 at 11:06pm
I know how to fix this and how to implement more elgant way, but what I don't understand from this simple excise is the concept of passing by reference that I have understood so far.
Passing a pointer to a function should basically be able to change the value of that pointer pointing to. So if I assign a new memory to that passed pointer, I should be able to have a correct pointing address.
Am I missing any fundamental basic concept here?

Thanks,
Mar 1, 2012 at 11:09pm
You are correctly saying that "Passing a pointer to a function should basically be able to change the value of that pointer pointing to"

But the pointer itself declared as a parameter is a local object of the function. And it is deleted then the function finishes its work.
Last edited on Mar 1, 2012 at 11:10pm
Mar 1, 2012 at 11:38pm
As you said, "the pointer itself declared as a parameter is a local object of the function". If this is true, then how can you achieve "pass by reference to the fucntion"?
Mar 2, 2012 at 12:24am
closed account (zb0S216C)
dwjang wrote:
If this is true, then how can you achieve "pass by reference to the fucntion"?
void newStack( Stack *&In_Pointer ); ?

Wazzak
Topic archived. No new replies allowed.