bassam tarek
bassam tarek

Reputation: 3

Why does print the pointer directly give a diffrent result when print it by a function in my code?

whats wrong here? why the 2 "cout" didn't give the same results?

#include<iostream>
#include<string>

using namespace std;

template <class t>

class stack{
    private:
    
        struct node{
            t val;
            node *next;
        };
    
    public:
    
        node *top;
        
        stack(){
            top = NULL;
        }
    
        void push(t v){
            node item;
            item.val = v;
            item.next = top;
            top = &item;
        }
        
        void printNext(){
            cout << top->next <<endl;
        }
};

int main(){
    stack <int> s;
    s.push(10);
    
    cout << s.top->next << "\n";
    s.printNext();
    
    return 0;
}

when i directly trying to print the next pointer by

cout << s.top->next << "\n";

the result i got was "0"

but when i trying print the same pointer by the printNext() function

cout << s.printNext(); << "\n";

the result i got was "0x7f4a046aad5c"

so why it is not the same?

Upvotes: 0

Views: 62

Answers (2)

Alexey S. Larionov
Alexey S. Larionov

Reputation: 7937

Refer to the possible picture of you program's stack (not your stack stack, but stack that contains all data of your program from start to finish) : enter image description here

The first state is after you execute stack <int> s, it contains your local variable s and the content of stack object (i.e. the pointer top, which now points to NULL).

The state 2 is when you enter function s.push(10); (not after it yet, but inside it). When you call it, there's some data added on stack that allows to return back from the function (not important, marked with crossed cell under top), next you have arguments of your function (the number 10 you pass), then your function creates a node item which consists of two variables for value and pointer on next. As a result of your push function, top now points on the location of next, while next points on 0 (i.e. pointer next is equal to 0).

The state 3 is right after you return from push function, all the local state of the function is again undefined (usually it's left untouched, but it's UNDEFINED, no guarantees about anything ever). Notice that top still points on undefined memory location.

The state 4 is when you call cout << s.top->next << "\n";, it's also a call to a function, so again it adds some technical data on stack, and also all the arguments (i.e. the copies of values of s.top->next and of "\n"). The copy contains the same address that is stored in top pointer, and it prints some undefined value, but you see 0, because as I already said the value is marked undefined, but not changed, thus it's still 0 as in state 2.

Now finally why the second print isn't zero: enter image description here

There you call a function, which adds some technical data on stack. Inside this function you call (!!) another function cout<<, so some more data has to be added, also you need to copy the argument again, i.e. the copy of values of top->next and new-line symbol too. However it just happened that this technical data now occupies the data that was previously occupied by item.next value, so it's overwritten, and no longer 0 (thus you see some garbage non-zero value).

Upvotes: 1

Skyy Civil
Skyy Civil

Reputation: 63

A pointer can print the function or variable ADDRESS. The second number that you got was the address of your function. I hope I answered your question. If you want to know more about this go here

have a fantastic day!

Upvotes: 0

Related Questions