Sequence point question

Discussion in 'C Programming' started by Gene, Jun 23, 2007.

  1. Gene

    Gene Guest

    Hello all. I do not have access to the ANSI standard doc. Perhaps
    someone can help me with this. Suppose I have this situation:

    typedef void *PTR;

    struct cons_s {
    PTR car, cdr;
    };

    struct root_record_s {
    ...
    struct cons_s *p;
    };

    PTR cons(struct root_record_s *rr, PTR car, PTR cdr)
    {
    ...
    rr->p->cdr = gc_malloc(rr, sizeof(struct cons_s));
    ...
    }

    where gc_malloc may change the value of rr->p. Is there anything in
    the ANSI standard that says rr->p->cdr will be the "correct" l-value,
    e.g. the new one after gc_malloc() makes the change? Or must the
    assignmet be split into two to ensure correct sequencing? ...

    PTR cons(struct root_record_s *rr, PTR car, PTR cdr)
    {
    PTR tmp;
    ...
    tmp = gc_malloc(rr, sizeof(struct cons_s));
    rr->p->cdr = tmp;
    ...
    }

    Many thanks.
     
    Gene, Jun 23, 2007
    #1
    1. Advertising

  2. Gene

    Thad Smith Guest

    Gene wrote:

    > rr->p->cdr = gc_malloc(rr, sizeof(struct cons_s));
    >
    > where gc_malloc may change the value of rr->p. Is there anything in
    > the ANSI standard that says rr->p->cdr will be the "correct" l-value,
    > e.g. the new one after gc_malloc() makes the change?


    No.

    > Or must the
    > assignmet be split into two to ensure correct sequencing?


    Yes. Even though calling a function results in sequences points, the
    point at which rr->p is evaluated is unspecified with regard to when the
    function is called.

    --
    Thad
     
    Thad Smith, Jun 23, 2007
    #2
    1. Advertising

  3. Gene said:

    > Hello all. I do not have access to the ANSI standard doc. Perhaps
    > someone can help me with this. Suppose I have this situation:
    >

    <snip>

    > PTR cons(struct root_record_s *rr, PTR car, PTR cdr)
    > {
    > ...
    > rr->p->cdr = gc_malloc(rr, sizeof(struct cons_s));
    > ...
    > }
    >
    > where gc_malloc may change the value of rr->p. Is there anything in
    > the ANSI standard that says rr->p->cdr will be the "correct" l-value,
    > e.g. the new one after gc_malloc() makes the change?


    No, 'fraid not. As you thought, you have to use a temp to be sure of the
    code doing what you want.

    --
    Richard Heathfield <http://www.cpax.org.uk>
    Email: -www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999
     
    Richard Heathfield, Jun 23, 2007
    #3
  4. Gene

    Flash Gordon Guest

    Gene wrote, On 23/06/07 05:20:
    > Hello all. I do not have access to the ANSI standard doc. Perhaps


    <snip>

    If you go to http://clc-wiki.net/wiki/c_standard there are links to
    where you can obtain drafts of the various versions of the C standard
    and information about the different types, including why I use the ISO
    (or BSI) standard rather than the ANSI one.

    Others have answered your main question correctly so I won't.
    --
    Flash Gordon
     
    Flash Gordon, Jun 23, 2007
    #4
  5. Roland Pibinger, Jun 23, 2007
    #5
  6. Gene

    Gene Guest

    On Jun 23, 5:18 am, Flash Gordon <> wrote:
    > Gene wrote, On 23/06/07 05:20:
    >
    > > Hello all. I do not have access to the ANSI standard doc. Perhaps

    >
    > <snip>
    >
    > If you go tohttp://clc-wiki.net/wiki/c_standardthere are links to
    > where you can obtain drafts of the various versions of the C standard
    > and information about the different types, including why I use the ISO
    > (or BSI) standard rather than the ANSI one.
    >
    > Others have answered your main question correctly so I won't.
    > --
    > Flash Gordon


    Thanks! This is very helpful, as are all the answers to the original
    question.
     
    Gene, Jun 24, 2007
    #6
    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. stef mientki
    Replies:
    13
    Views:
    644
    stef mientki
    Oct 20, 2007
  2. somenath

    Question regarding sequence point

    somenath, Nov 26, 2007, in forum: C Programming
    Replies:
    4
    Views:
    298
    santosh
    Nov 26, 2007
  3. somenath
    Replies:
    4
    Views:
    306
  4. somenath
    Replies:
    0
    Views:
    268
    somenath
    Dec 14, 2007
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,345
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page