Solved

Passing a pointer

Posted on 2007-11-22
9
167 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 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
Technology Partners: 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

630 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