Is pointer parameter local variable to a function?

Discussion in 'C++' started by ali, Mar 7, 2007.

  1. ali

    ali Guest

    Hi,

    When I pass a pointer as an argument of a method, is it safe if I
    change the data pointed by the argument and return it upon
    completion ?

    For example:

    Object* someFunction(Object* ob)
    {
    //do some manipulation and return
    return ob;
    }

    Are there things I need to be aware/cautious when doing this? I tried
    with int pointer and got the results as expected (no error or garbage
    output). I had thought it would give me garbage result because to my
    undestanding, ob would be a local variable to someFunction, and should
    have been destroyed upon return?

    I know that if within the body of the function above, I created a
    pointer and assigned memory with new operator, I should not return the
    pointer because it will be destroyed upon exit and I might be getting
    garbage.

    I know i'm missing some simple concept. Will appreciate your help.

    Thanks,

    Ali
     
    ali, Mar 7, 2007
    #1
    1. Advertising

  2. ali wrote:
    > When I pass a pointer as an argument of a method, is it safe if I
    > change the data pointed by the argument and return it upon
    > completion ?


    Sure. You're receiving a value, you're using the value [to change
    the data], you're returning the [same, supposedly] value. What's
    the problem?

    > For example:
    >
    > Object* someFunction(Object* ob)
    > {
    > //do some manipulation and return
    > return ob;
    > }
    >
    > Are there things I need to be aware/cautious when doing this?


    The pointer passed in could be NULL. Make sure you check before you
    try to manipulate the data pointed to by the pointer.

    > I tried
    > with int pointer and got the results as expected (no error or garbage
    > output). I had thought it would give me garbage result because to my
    > undestanding, ob would be a local variable to someFunction, and should
    > have been destroyed upon return?


    The variable will be destroyed, but the value you're returning isn't.
    What happens if you do

    int foo(int a)
    {
    int b = a + 42;
    // do something to b
    return a;
    }

    Nothing special, right. You get the value, you use the value, and then
    you return the value. 'a' variable does get destroyed, but not before
    the temporary of type 'int' is created and returned.

    > I know that if within the body of the function above, I created a
    > pointer and assigned memory with new operator, I should not return the
    > pointer because it will be destroyed upon exit and I might be getting
    > garbage.


    Sounds wrong.

    > I know i'm missing some simple concept. Will appreciate your help.


    It's hard to explain if you can't grasp the [simple] concept of
    passing around values. If you can separate two concepts in your
    mind: of variables and values. Variables do have values, but you
    don't need a variable to have a value you can pass around. That's
    what an 'r-value' is.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Mar 7, 2007
    #2
    1. Advertising

  3. ali

    Kai-Uwe Bux Guest

    ali wrote:

    > Hi,
    >
    > When I pass a pointer as an argument of a method, is it safe if I
    > change the data pointed by the argument and return it upon
    > completion ?
    >
    > For example:
    >
    > Object* someFunction(Object* ob)
    > {
    > //do some manipulation and return
    > return ob;
    > }
    >
    > Are there things I need to be aware/cautious when doing this? I tried
    > with int pointer and got the results as expected (no error or garbage
    > output). I had thought it would give me garbage result because to my
    > undestanding, ob would be a local variable to someFunction, and should
    > have been destroyed upon return?


    You need to distinguish the pointer and the pointee. Both are objects:

    The pointer ob is an object of type Object*. It is local to someFunction.
    The pointee *ob is an object of type Object. It is not local.

    The pointer ob is destroyed when it does out of scope. That, however, has no
    effect on the pointee. It lives on and needs to be disposed off at some
    later time.


    > I know that if within the body of the function above, I created a
    > pointer and assigned memory with new operator, I should not return the
    > pointer because it will be destroyed upon exit and I might be getting
    > garbage.


    You don't know that because you can only know true statements. You might
    believe so; but you would be wrong: It is safe, to return a pointer from a
    function that has been allocated within the body of the function. There are
    even cases where it can be considered good design.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 7, 2007
    #3
  4. ali

    ali Guest

    On Mar 6, 10:13 pm, "ali" <> wrote:
    > Hi,
    >
    > When I pass a pointer as an argument of a method, is it safe if I
    > change the data pointed by the argument and return it upon
    > completion ?
    >
    > For example:
    >
    > Object* someFunction(Object* ob)
    > {
    > //do some manipulation and return
    > return ob;
    >
    > }
    >
    > Are there things I need to be aware/cautious when doing this? I tried
    > with int pointer and got the results as expected (no error or garbage
    > output). I had thought it would give me garbage result because to my
    > undestanding, ob would be a local variable to someFunction, and should
    > have been destroyed upon return?
    >
    > I know that if within the body of the function above, I created a
    > pointer and assigned memory with new operator, I should not return the
    > pointer because it will be destroyed upon exit and I might be getting
    > garbage.
    >
    > I know i'm missing some simple concept. Will appreciate your help.
    >
    > Thanks,
    >
    > Ali


    Ooh, now I get the idea .. thanks for all your help!
     
    ali, Mar 7, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    641
    John Carson
    Jul 30, 2003
  2. sairam
    Replies:
    2
    Views:
    378
    Steve Holden
    Apr 5, 2007
  3. Mark
    Replies:
    7
    Views:
    1,344
    Keith Thompson
    Feb 4, 2011
  4. AikidoGuy
    Replies:
    11
    Views:
    584
    Seebs
    Nov 21, 2011
  5. AzamSharp
    Replies:
    2
    Views:
    192
Loading...

Share This Page