Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing a pointer

Posted on 2007-11-22
9
Medium Priority
?
172 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 2000 total points
ID: 20335260
>> 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
ID: 20335290
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
ID: 20335336
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Expert Comment

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

this allows Test2 to modify the pointer of the calling function.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20335354
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
ID: 20335358
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
ID: 20335373
nice example, jaime, but a bit late ;)
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20335418
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
ID: 20335477
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

783 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