pointers in functions

Discussion in 'C Programming' started by raghu, Nov 14, 2007.

  1. raghu

    raghu Guest

    look at the following code
    #include <stdio.h>
    int * get_number(int i);

    main()
    {
    int *krrk[5];
    int i = 0;
    printf(" testing the address of a variable in the function\n i
    address in main %u\n", &i);
    krrk = get_number(i);
    printf("for i = %d address %u and its contents %d\n", i, krrk,
    *krrk);
    for(i = 1; i < 5; i++)
    {

    krrk = get_number(i);
    printf("for i = %d address %u and its contents %d\n", i, krrk,
    *krrk);
    printf("for i - 1 = %d address %u and its contents %d\n", i - 1,
    krrk[i - 1], *krrk[i - 1]);
    }
    for( i = 0 ; i < 5; i++)
    {
    printf(" for i = %d address of %d is %u\n", i, *krrk, krrk);
    }
    }
    int * get_number(int i)
    {
    return &i;
    }

    testing the address of a variable in the function
    i address i nmain 2280636
    for i = 0 address 2280576 and its contents 0
    for i = 1 address 2280576 and its contents 1
    for i -1 = 0 address 2280576 and its contents 4202572
    for i = 2 address 2280576 and its contents 2
    for i -1 = 1 address 2280576 and its contents 4202572
    for i = 3 address 2280576 and its contents 3
    for i -1 = 2 address 2280576 and its contents 4202572
    for i = 4 address 2280576 and its contents 4
    for i -1 = 3 address 2280576 and its contents 4202572

    for i = 0 address of 4202616 is 2280576
    for i = 1 address of 4202664 is 2280576
    for i = 2 address of 4202664 is 2280576
    for i = 3 address of 4202664 is 2280576
    for i = 4 address of 4202664 is 2280576

    Here I am returning a local variable of a function And capturing its
    address in a array of pointers. In the main function for the first
    access its giving the correct answer but for the second access its
    giving wrong(what ever order either 'for i = 1..' or 'for i - 1 =
    0..'). why it behaves like so?

    If we use msgQCreate in the same fashion its working fine why?

    awaitng for ur reply.

    -Raghu
    raghu, Nov 14, 2007
    #1
    1. Advertising

  2. raghu

    Guest

    raghu wrote:
    > int * get_number(int i)
    > {
    > return &i;
    > }


    The variable i only exists for as long as the function is running.
    While the function is running, &i is the address where the current
    value of i is stored. Afterwards, the value returned is the address
    where i *was* stored at, which is now free for the program to use for
    something else if it wants to. This is why you are getting problems.

    Don't take or use the address of something unless that something is
    still there!

    Hope that helps.
    Paul.
    , Nov 14, 2007
    #2
    1. Advertising

  3. raghu

    raghu Guest

    On Nov 14, 2:29 pm, wrote:
    > raghu wrote:
    > > int * get_number(int i)
    > > {
    > > return &i;
    > > }

    >
    > The variable i only exists for as long as the function is running.
    > While the function is running, &i is the address where the current
    > value of i is stored. Afterwards, the value returned is the address
    > where i *was* stored at, which is now free for the program to use for
    > something else if it wants to. This is why you are getting problems.
    >
    > Don't take or use the address of something unless that something is
    > still there!
    >
    > Hope that helps.
    > Paul.

    Yes I understood thank you. look at the following code and the authors
    claims that its working
    mac_ipc_send_ipc_msg ( char *mq_name,
    int mq_id,
    int options,
    IPC_MSG_TYPE msgtype,
    int msglen,
    char *data,
    void (*cb)(void *))
    {
    ipc_msg_hdr_t msg ;

    msg.type = msgtype;
    msg.len = msglen ;
    msg.data = data;
    msg.free_cb = cb;


    return vos_mq_send(mq_name,
    mq_id,
    (char *)&msg,
    sizeof(ipc_msg_hdr_t),
    (int)MSG_PRI_NORMAL);


    }


    In the above code vos_mq_send is similar msgQCreate() function.
    then what will be the behaviour of 'msg' variable. Will it works?
    The testers also saying that it works. I didn't understand why?
    Any comments on this.
    raghu, Nov 14, 2007
    #3
  4. raghu

    Remo D. Guest

    raghu ha scritto:

    > return vos_mq_send(mq_name,
    > mq_id,
    > (char *)&msg,
    > sizeof(ipc_msg_hdr_t),
    > (int)MSG_PRI_NORMAL);
    >
    > In the above code vos_mq_send is similar msgQCreate() function.
    > then what will be the behaviour of 'msg' variable. Will it works?
    > The testers also saying that it works. I didn't understand why?
    > Any comments on this.
    >


    In the first code you sent, you returned the address of a local variable
    which is meaningless.

    In this example you return the value of a function passing the address
    of a local variable as argument.

    Do you see the difference?

    In the first case you tried to use the address of a variable that no
    longer existed. In the second case the local variable msg will be
    deallocated only *after* the function vos_mq_send () has been computed.

    Remo.D
    Remo D., Nov 14, 2007
    #4
  5. raghu

    raghu Guest

    On Nov 14, 3:11 pm, "Remo D." <rdentato> wrote:
    > raghu ha scritto:
    >
    > > return vos_mq_send(mq_name,
    > > mq_id,
    > > (char *)&msg,
    > > sizeof(ipc_msg_hdr_t),
    > > (int)MSG_PRI_NORMAL);

    >
    > > In the above code vos_mq_send is similar msgQCreate() function.
    > > then what will be the behaviour of 'msg' variable. Will it works?
    > > The testers also saying that it works. I didn't understand why?
    > > Any comments on this.

    >
    > In the first code you sent, you returned the address of a local variable
    > which is meaningless.
    >
    > In this example you return the value of a function passing the address
    > of a local variable as argument.
    >
    > Do you see the difference?
    >
    > In the first case you tried to use the address of a variable that no
    > longer existed. In the second case the local variable msg will be
    > deallocated only *after* the function vos_mq_send () has been computed.
    >
    > Remo.D


    you are right. There is a difference in returning. But here we are
    passing the variable msg which is local to this function to
    vos_mq_send function. As I told earlier it is as similar to that of
    msgQCreate() it will store the msg varible in message queue memory.
    For the second call same memory might be assigned and the first node
    might be corrupted right.
    raghu, Nov 14, 2007
    #5
  6. raghu wrote:
    > On Nov 14, 2:29 pm, wrote:
    >> raghu wrote:
    >>> int * get_number(int i)
    >>> {
    >>> return &i;
    >>> }

    >> The variable i only exists for as long as the function is running.
    >> While the function is running, &i is the address where the current
    >> value of i is stored. Afterwards, the value returned is the address
    >> where i *was* stored at, which is now free for the program to use for
    >> something else if it wants to. This is why you are getting problems.
    >>
    >> Don't take or use the address of something unless that something is
    >> still there!
    >>
    >> Hope that helps.
    >> Paul.

    > Yes I understood thank you. look at the following code and the authors
    > claims that its working
    > mac_ipc_send_ipc_msg ( char *mq_name,
    > int mq_id,
    > int options,
    > IPC_MSG_TYPE msgtype,
    > int msglen,
    > char *data,
    > void (*cb)(void *))
    > {
    > ipc_msg_hdr_t msg ;
    >
    > msg.type = msgtype;
    > msg.len = msglen ;
    > msg.data = data;
    > msg.free_cb = cb;
    >
    >
    > return vos_mq_send(mq_name,
    > mq_id,
    > (char *)&msg,
    > sizeof(ipc_msg_hdr_t),
    > (int)MSG_PRI_NORMAL);
    >
    >
    > }
    >
    >
    > In the above code vos_mq_send is similar msgQCreate() function.
    > then what will be the behaviour of 'msg' variable. Will it works?
    > The testers also saying that it works. I didn't understand why?
    > Any comments on this.


    It's perfectly ok to pass the address of a local variable to another function in
    a call. The call to mac_ipc_send_ipc_msg remains active, and its local
    variables continue to exist, while vos_mq_send is executing; vos_mq_send can
    safely refer (indirectly) to msg, even though it's local to mac_ipc_send_ipc_msg.

    Now if vos_mq_send *returned* the value of &msg as its result, and
    mac_ipc_send_ipc_msg then returned that address to its caller, then you'd be in
    trouble, but that doesn't seem to be what's happening.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Looking for software development work in the San Diego area.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 14, 2007
    #6
  7. raghu

    Remo D. Guest

    raghu ha scritto:
    > On Nov 14, 3:11 pm, "Remo D." <rdentato> wrote:


    >> Do you see the difference?
    >>
    >> In the first case you tried to use the address of a variable that no
    >> longer existed. In the second case the local variable msg will be
    >> deallocated only *after* the function vos_mq_send () has been computed.
    >>
    >> Remo.D

    >
    > you are right. There is a difference in returning. But here we are
    > passing the variable msg which is local to this function to
    > vos_mq_send function. As I told earlier it is as similar to that of
    > msgQCreate() it will store the msg varible in message queue memory.
    > For the second call same memory might be assigned and the first node
    > might be corrupted right.
    >


    It's hard to say without knowing the details around vos_mq_send or
    msgQCreate.

    For example, one of the functions may create a copy of msg and return
    the new address while the other doesn't do it ...

    Anyway, if you look deeper into those functions keeping in mind what we
    suggested you, I'm sure you'll find the reason of this unexpected behaviour.

    Good hunting.

    Remo.D
    Remo D., Nov 14, 2007
    #7
  8. raghu

    pete Guest

    raghu wrote:
    >
    > On Nov 14, 3:11 pm, "Remo D." <rdentato> wrote:


    > > In the first case you tried to use the address of a variable that no
    > > longer existed. In the second case the local variable msg will be
    > > deallocated only *after* the
    > > function vos_mq_send () has been computed.
    > >
    > > Remo.D

    >
    > you are right. There is a difference in returning. But here we are
    > passing the variable msg which is local to this function to
    > vos_mq_send function.


    I don't think you know what "passing" means.
    What that really means is that you are initializing
    a parameter in vos_mq_send,
    with the value that msg has in the calling function.

    And like he said, the variable msg,
    exists until the calling function completes.

    --
    pete
    pete, Nov 15, 2007
    #8
  9. raghu

    CBFalconer Guest

    raghu wrote:
    >
    > look at the following code


    Why are you starting two independent threads, with the difference
    being the case of the first letter of the subject?

    --
    Chuck F (cbfalconer at maineline dot net)
    <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Nov 15, 2007
    #9
    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. S?ren Gammelmark
    Replies:
    1
    Views:
    1,867
    Eric Sosman
    Jan 7, 2005
  2. Marc Thrun
    Replies:
    15
    Views:
    850
    Tim Rentsch
    Oct 4, 2005
  3. newbie

    Pointers to char pointers in functions

    newbie, Sep 18, 2006, in forum: C Programming
    Replies:
    9
    Views:
    300
    August Karlstrom
    Sep 24, 2006
  4. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    656
  5. ajaybgr
    Replies:
    18
    Views:
    951
    Philip Lantz
    Sep 7, 2012
Loading...

Share This Page