Solved

Passing a pointer

Posted on 2007-11-22
9
163 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Prototype for MessageDlg in Embacadero XE 2 2 39
Unable to start eclipse ? 17 142
C++ :Change value from  DisableCMD registry 4 60
gdb doesn't stop on breakpoint 2 70
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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.

825 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