Sequence point question

G

Gene

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.
 
T

Thad Smith

Gene said:
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.
 
R

Richard Heathfield

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:
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.
 
F

Flash Gordon

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.
 
G

Gene

Gene wrote, On 23/06/07 05:20:


<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.

Thanks! This is very helpful, as are all the answers to the original
question.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top