Solved

Passing a pointer

Posted on 2007-11-22
9
162 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
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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
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.

920 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

16 Experts available now in Live!

Get 1:1 Help Now