question about pointers inside functions

Discussion in 'C Programming' started by Fernando Barsoba, Nov 14, 2005.

  1. Hi all,

    I'm trying to reassign the address of a pointer inside a function and
    return it once the function has done its things... However, the address
    returned is not the address I wanted to return.. let me explain it
    better with the actual example:

    caller function:

    > struct authenHeader * AH;
    > setAuhenticationH(authenHeader, sizeof(authenHeader), AH);


    Memory address for AH here is 0x610fd552

    callee function:

    > struct authenHeader * setAuhenticationH(uint8_t *autH, unsigned short length, struct authenHeader *AH) {
    >
    > int authenHeader_size;
    >
    > authenHeader_size = sizeof(struct authenHeader);
    > bzero(autH, authenHeader_size);
    > AH = (struct authenHeader *)autH;


    Memory address after assignment is 0x22ec30
    >
    > AH->nexth = 4;
    > AH->payloadlen = 4; /* for tests, standard case: 3 32-bit word fixed portion + 96-bit authentication
    > value. In 32-bit words, 3+3-2 = 4 */
    > AH->resvd = 0;
    > AH->spi = 0;
    > AH->seqnum = 0x00;
    > return AH;
    > }


    The memory address returned is supposedly 0x22ec30, but when going back
    to the caller function, the memory address is 0x610fd552.

    Clearly, I'm doing something wrong conceptually.. any idea on how to
    solve this?

    Thanks,

    FBM
     
    Fernando Barsoba, Nov 14, 2005
    #1
    1. Advertising

  2. Fernando Barsoba

    mazsx Guest

    Hi,
    Fernando Barsoba wrote:
    > Hi all,
    >
    > I'm trying to reassign the address of a pointer inside a function and
    > return it once the function has done its things... However, the address
    > returned is not the address I wanted to return.. let me explain it
    > better with the actual example:
    >
    > caller function:
    >
    > > struct authenHeader * AH;
    > > setAuhenticationH(authenHeader, sizeof(authenHeader), AH);

    >
    > Memory address for AH here is 0x610fd552
    >
    > callee function:
    >
    > > struct authenHeader * setAuhenticationH(uint8_t *autH, unsigned short length, struct authenHeader *AH) {
    > > int authenHeader_size;
    > >
    > > authenHeader_size = sizeof(struct authenHeader);
    > > bzero(autH, authenHeader_size);
    > > AH = (struct authenHeader *)autH;

    >
    > Memory address after assignment is 0x22ec30
    > >
    > > AH->nexth = 4;
    > > AH->payloadlen = 4; /* for tests, standard case: 3 32-bit word fixed portion + 96-bit authentication
    > > value. In 32-bit words, 3+3-2 = 4 */
    > > AH->resvd = 0;
    > > AH->spi = 0;
    > > AH->seqnum = 0x00;
    > > return AH;
    > > }

    >
    > The memory address returned is supposedly 0x22ec30, but when going back
    > to the caller function, the memory address is 0x610fd552.
    >
    > Clearly, I'm doing something wrong conceptually.. any idea on how to
    > solve this?
    >
    > Thanks,
    >
    > FBM


    Your setAuhenticationH function is supposed to return a pointer to
    struct, as it indeed does.
    You seem to expect that the third argument of your function gets this
    value after the function returns. You should better write
    AH=setAuhenticationH(....

    By the way:
    - do you use the value AH in your callee? You passed an uninitilazied
    value, that you immediately
    modify. So probably you simply do not need to declare the function
    with three args.
    - bzero is deprecated and non standand, use memset instead
    - the first argument of your function is identical to the name of the
    sturcture. This is allowed,
    since structure tags use a name space other than the variables do.
    But be sure that you
    are not confused by this.
    - I would better use size_t for the value of the sizeof operator

    mazsx
     
    mazsx, Nov 14, 2005
    #2
    1. Advertising

  3. Fernando Barsoba

    ferbar Guest

    mazsx wrote:
    > Hi,
    > Fernando Barsoba wrote:
    > > Hi all,
    > >
    > > I'm trying to reassign the address of a pointer inside a function and
    > > return it once the function has done its things... However, the address
    > > returned is not the address I wanted to return.. let me explain it
    > > better with the actual example:
    > > ...

    > Your setAuhenticationH function is supposed to return a pointer to
    > struct, as it indeed does.
    > You seem to expect that the third argument of your function gets this
    > value after the function returns. You should better write
    > AH=setAuhenticationH(....
    >


    Thanks a lot for your response.. I did as you said and it's working.

    > By the way:
    > - do you use the value AH in your callee? You passed an uninitilazied
    > value, that you immediately
    > modify. So probably you simply do not need to declare the function
    > with three args.
    > - bzero is deprecated and non standand, use memset instead
    > - the first argument of your function is identical to the name of the
    > sturcture. This is allowed,
    > since structure tags use a name space other than the variables do.
    > But be sure that you
    > are not confused by this.
    > - I would better use size_t for the value of the sizeof operator


    And thanks also for these other points..

    FBM
    >
    > mazsx
     
    ferbar, Nov 14, 2005
    #3
  4. Fernando Barsoba

    Jack Klein Guest

    On Mon, 14 Nov 2005 19:23:14 GMT, Fernando Barsoba
    <> wrote in comp.lang.c:

    In addition to what mazsx said, all of which was good advice, I notice
    a few things.

    > Hi all,
    >
    > I'm trying to reassign the address of a pointer inside a function and
    > return it once the function has done its things... However, the address
    > returned is not the address I wanted to return.. let me explain it
    > better with the actual example:
    >
    > caller function:
    >
    > > struct authenHeader * AH;


    Perhaps you are omitting some of your code, but as the line above is
    written, it defines a pointer to a structure, but does not initialize
    it to point to an actual structure, or indeed anything useful at all.
    If this definition is at file scope, that is outside of any function,
    then the pointer is initialized to NULL. If this definition is inside
    a function, AH contains uninitialized gibberish, and any use of its
    value causes undefined behavior.

    > > setAuhenticationH(authenHeader, sizeof(authenHeader), AH);

    >
    > Memory address for AH here is 0x610fd552


    You statement above is ambiguous. When you are talking about a
    pointer, there are two addresses involved. Assuming the pointer
    points to a valid object, it contains the address of the object
    pointed to. But since a pointer is an object itself, the pointer has
    an address, as well as holds an address, and the two addresses are
    usually different.

    > callee function:
    >
    > > struct authenHeader * setAuhenticationH(uint8_t *autH, unsigned short length, struct authenHeader *AH) {
    > >
    > > int authenHeader_size;
    > >
    > > authenHeader_size = sizeof(struct authenHeader);
    > > bzero(autH, authenHeader_size);
    > > AH = (struct authenHeader *)autH;


    Now here's the thing that really got my attention. There is no
    guarantee at all, unless the address in 'autH' came from malloc(), or
    from taking the address of a real authenHeader structure, that the
    address is correctly aligned for the structure.

    If it is not properly aligned, all the access to it below, and most
    likely in the caller when you return the value properly, cause
    undefined behavior. On some hardware architectures that merely means
    they take longer to execute. On others, you can generate a trap that
    could cause your program to crash, or your operating system to shut it
    down.

    > Memory address after assignment is 0x22ec30
    > >
    > > AH->nexth = 4;
    > > AH->payloadlen = 4; /* for tests, standard case: 3 32-bit word fixed portion + 96-bit authentication
    > > value. In 32-bit words, 3+3-2 = 4 */
    > > AH->resvd = 0;
    > > AH->spi = 0;
    > > AH->seqnum = 0x00;
    > > return AH;
    > > }

    >
    > The memory address returned is supposedly 0x22ec30, but when going back
    > to the caller function, the memory address is 0x610fd552.
    >
    > Clearly, I'm doing something wrong conceptually.. any idea on how to
    > solve this?
    >
    > Thanks,


    Why do you need to create a structure out of a pointer to raw memory?
    Why can't you create an instance of the structure and use it instead?

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
     
    Jack Klein, Nov 15, 2005
    #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. S?ren Gammelmark
    Replies:
    1
    Views:
    1,902
    Eric Sosman
    Jan 7, 2005
  2. Marc Thrun
    Replies:
    15
    Views:
    865
    Tim Rentsch
    Oct 4, 2005
  3. newbie

    Pointers to char pointers in functions

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

    pointers, pointers, pointers...

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

Share This Page