Solved

Passing a pointer

Posted on 2007-11-22
9
161 Views
Last Modified: 2010-04-01
Hi,

I'm a little confused on what happens when you pass a pointer to some object as a parameter to a function. Something like this:

int main()
{
    MyObj* p = new MyObj;
    Test1(p);
    Test2(&p);
}

void Test1(MyObj* p)
{
    p->SomeFunction();
}

void Test2(MyObj** pp)
{
    (*pp)->SomeFunction();
}


Is there any difference? I think Test1() is getting a copy of the pointer declared in main(), while Test2() is actually getting the same pointer declared in main().

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 4
  • 3
  • 2
9 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
Comment Utility
>> I think Test1() is getting a copy of the pointer declared in main()

Correct.


>> Test2() is actually getting the same pointer declared in main().

To be more exact, it gets a pointer to the pointer declared in main(). This means that you can modify the pointer inside the Test2() function, and that modification will be visible in main() :

    void Test2(MyObj** pp)
    {
        delete *pp;
        *pp = new MyObj;
        (*pp)->SomeFunction();
    }

and calling it :

    MyObj* p = new MyObj;
    Test2(&p);
    // p now points to the newly allocated MyObj
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
the different version have different uses:
in Test1 you can use the pointer to access some data
in Test2 you can use and change the pointer to point to a different data
0
 
LVL 10

Expert Comment

by:peetm
Comment Utility
When a pointer [or anything is - that's not using 'references'], the expression used in the call is evaluated, and the result of that evaluation is passed - so *values*/*copies* of things are passed to called functions.

In your code a copy of value of p is passed to Test1, and a copy of the address of p is passed to Test2.  The 'type' of the first is 'pointer to a MyObj' and a 'pointer to a pointer to a MyObj' respectively.  And, as you don't declared these formal parameters using 'const' you've turned a constant into a variable at each call, i.e., the result of the expression evaluation is a number - a constant.  But that is 'received' in Test1/2 as a variable.  Still, non-matted really, as each variable is initialised with the value obtained in each evaluation.

So, in Text1 p is a copy of the address of a MyObj as a scalar variable, and in Test2 it is a copy of the address of a pointer to a MyObj as a scalar variable.

When Test1/2 return/complete, these local variables [the formal parameters] are destroyed.

Hope that helps.

0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
to extend my last comment:
void Test2(MyObj** pp)
{
    *pp = new MyObj();
}

this allows Test2 to modify the pointer of the calling function.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Sorry, not a realistic example, will generate a memory leak, will be better:

to extend my last comment:
void Test2(MyObj** pp)
{
    delete (*pp);
    *pp = new MyObj();
}
0
 
LVL 10

Expert Comment

by:peetm
Comment Utility
Sans the typos and tidied a little.
=========================

When a pointer [or anything is - that's not using 'references'] is passed, the expression used in the call is evaluated, and the result of that evaluation is what's passed - so *values*/*copies* of things are passed to called functions with using references.

So, in your code, a copy of *value* of p is passed to Test1, and a copy of the [value] *address* of p is passed to Test2.  The 'types' are 'pointer to a MyObj' and a 'pointer to a pointer to a MyObj' respectively.  As you don't declared these formal parameters using 'const' you've turned a constant into a variable at each call, i.e., the result of the expression evaluation is a number - a constant.  But that is 'received' in Test1/2 as a variable.  Still, no matter really, as each local variable is initialised with the value obtained by each of the evaluations taking place ahead of each call.

So, in Test1, p is a copy of the address of a MyObj as a scalar variable, and in Test2 it is a copy of the address of a pointer to a MyObj as a scalar variable.

When Test1/2 return/complete, these local variables [the formal parameters] are destroyed.

Hope that helps.
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
nice example, jaime, but a bit late ;)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
Comment Utility
Oops, not noticed, I think my proxy is not refreshing correctly. I am arriving late to many questions.
0
 
LVL 10

Expert Comment

by:peetm
Comment Utility
I seem to arrive late at every question usually - what could be the problem?  OR - should I ask this somewhere for some points do you think?
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now