passing pointer to a function

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;
}
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
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
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
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
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
So then, why after calling newStack(Stack* s) function, s is still 0?
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
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,
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
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"?
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.