Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Order of execution

Reply
Thread Tools

Order of execution

 
 
Sabiyur
Guest
Posts: n/a
 
      06-28-2006
Hi all,
one of the recent post gives the macro to do swap

#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)

This macro will work, if the execution is from left to right.

That is step 1) tmp=m
step 2) m=n
step 3) n=tmp

But I hope order of execution is decided by the compiler.
Does C specification is saying any thing about this order of exectuion?

Will this macro work on all compilers?

Plz share your thoughts...

Thanks
Sabi

 
Reply With Quote
 
 
 
 
Roberto Waltman
Guest
Posts: n/a
 
      06-28-2006
"Sabiyur" <(E-Mail Removed)> wrote:
>Hi all,
> one of the recent post gives the macro to do swap
>
>#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)
>
>This macro will work, if the execution is from left to right.
>
>That is step 1) tmp=m
> step 2) m=n
> step 3) n=tmp
>
>But I hope order of execution is decided by the compiler.
>Does C specification is saying any thing about this order of exectuion?
>
>Will this macro work on all compilers?
>

Yes, it will work on all C compilers. The comma operator used in the
macro guarantees left-to-right evaluation.

(Of course, tmp must be defined first.)

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      06-28-2006
"Sabiyur" <(E-Mail Removed)> wrote:

> one of the recent post gives the macro to do swap
>
> #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)
>
> This macro will work, if the execution is from left to right.


No, it won't; it will work, _if_ (and that's a much bigger if) tmp has
the same type as m and n, or a compatible one.

> That is step 1) tmp=m
> step 2) m=n
> step 3) n=tmp
>
> But I hope order of execution is decided by the compiler.
> Does C specification is saying any thing about this order of exectuion?


Yes. Not generally, but there's a sequence point at the commas. This
means that whatever trickery happens under the bonnet, the program much
at least behave as if it was executed left-to-right.

However, using this macro means that you have to declare a third object;
this object _must_ be called tmp; it must not hold an important value at
the moment this macro is called; and it must be declared to have a type
that is compatible with both m and n (so no using it to swap long
doubles one line, and structs the next line. So much for generality).

All this means that using this macro is probably rather more bother than
just writing this line out every time you need it. At least that means
you can swap several types of value, without having to resort to hacks
such as

{
the_appropriate_type tmp;
SWAP(object_1, object_2_;
}

(Oh, and of course by forgoing the macro you can also swap objects which
are themselves called tmp, but that's a lesser advantage.)

Richard
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      06-28-2006
Sabiyur schrieb:
> Hi all,
> one of the recent post gives the macro to do swap
>
> #define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)
>
> This macro will work, if the execution is from left to right.
>
> That is step 1) tmp=m
> step 2) m=n
> step 3) n=tmp
>
> But I hope order of execution is decided by the compiler.
> Does C specification is saying any thing about this order of exectuion?
>
> Will this macro work on all compilers?


C has a concept called "sequence points".
Between two consecutive sequence points, the order of execution
is not determined by the C standard -- it only has to be
semantically correct.
However, you can be sure that the code before the sequence point
is evaluated after the sequence point (or that your compiler /
platform behaves as if this is the case).
The comma operator gives you such a sequence point, i.e.
the preprocessed version of
tmp = (m)
will be executed before the preprocessed version of
(m) = (n), (n) = tmp
and, applying this rule once more,
(m) = (n)
is executed before
(n) = tmp

Read more on sequence points in the FAQ:
http://c-faq.com/expr/seqpoints.html
and the rest of chapter 3.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Roberto Waltman
Guest
Posts: n/a
 
      06-28-2006
Michael Mair wrote:

>>...
>>
>> Will this macro work on all compilers?

>
>C has a concept called "sequence points".
>Between two consecutive sequence points, the order of execution
>is not determined by the C standard -- it only has to be
>semantically correct.
>However, you can be sure that the code before the sequence point
>is evaluated <-----> after the sequence point (or that your compiler /


Insert // "before the code that is" \\

>platform behaves as if this is the case).
>The comma operator gives you such a sequence point, i.e.
>the preprocessed version of
> tmp = (m)
>will be executed before the preprocessed version of
> (m) = (n), (n) = tmp
>and, applying this rule once more,
> (m) = (n)
>is executed before
> (n) = tmp
>
>Read more on sequence points in the FAQ:
> http://c-faq.com/expr/seqpoints.html
>and the rest of chapter 3.
>
>Cheers
> Michael

 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      06-28-2006
Roberto Waltman schrieb:
> Michael Mair wrote:
>>>...
>>>
>>>Will this macro work on all compilers?

>>
>>C has a concept called "sequence points".
>>Between two consecutive sequence points, the order of execution
>>is not determined by the C standard -- it only has to be
>>semantically correct.
>>However, you can be sure that the code before the sequence point
>>is evaluated <-----> after the sequence point (or that your compiler /

>
> Insert // "before the code that is" \\


That's what I meant to write -- thank you

Cheers
Michael

>>platform behaves as if this is the case).



--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
Stephen Sprunk
Guest
Posts: n/a
 
      06-28-2006
"Sabiyur" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> But I hope order of execution is decided by the compiler.
> Does C specification is saying any thing about this order of exectuion?


The compiler can do anything it wants as long as the resulting program
behaves as if it did exactly what you said, i.e. it should be impossible to
detect it's done anything differently.

You'll see this "as if" rule referred to frequently on c.l.c.

> Will this macro work on all compilers?


It should work the same on any compiler (there's reasons it may fail, but it
would fail on all in those cases).

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin


--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Chris Hills
Guest
Posts: n/a
 
      06-28-2006
In article <(E-Mail Removed) .com>,
Sabiyur <(E-Mail Removed)> writes
>Hi all,
> one of the recent post gives the macro to do swap
>
>#define SWAP(m, n) (tmp = (m), (m) = (n), (n) = tmp)
>
>This macro will work, if the execution is from left to right.
>
>That is step 1) tmp=m
> step 2) m=n
> step 3) n=tmp
>
>But I hope order of execution is decided by the compiler.
>Does C specification is saying any thing about this order of exectuion?
>
>Will this macro work on all compilers?
>
>Plz share your thoughts...


http://www.phaedsys.demon.co.uk/chri...swengtips3.htm


--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ http://www.velocityreviews.com/forums/(E-Mail Removed) www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/



 
Reply With Quote
 
gangchen.oz@gmail.com
Guest
Posts: n/a
 
      06-29-2006
the result I got from following calculation is (i=10, j=2):
(i * j++) + (i * j) = 40
(i * j) + (i * j++) = 40
(i * j--) + (i * j) = 40
(i * j) + (i * j--) = 40
(i * ++j) + (i * j) = 60
(i * j) + (i * ++j) = 60
(i * ++j) + (i * --j) = 40
(i * --j) + (i * ++j) = 40

I realized that the '+' operator is not a sequence point, which means
either left or right part could have side effects. Still feel a little
puzzled, please someone could explain this.

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      06-29-2006
(E-Mail Removed) said:

> the result I got from following calculation is (i=10, j=2):
> (i * j++) + (i * j) = 40


Undefined behaviour.

> (i * j) + (i * j++) = 40


Undefined behaviour.

> (i * j--) + (i * j) = 40


Undefined behaviour.

> (i * j) + (i * j--) = 40


Undefined behaviour.

> (i * ++j) + (i * j) = 60


Undefined behaviour.

> (i * j) + (i * ++j) = 60


Undefined behaviour.

> (i * ++j) + (i * --j) = 40


Undefined behaviour.

> (i * --j) + (i * ++j) = 40


Undefined behaviour.

> I realized that the '+' operator is not a sequence point, which means
> either left or right part could have side effects. Still feel a little
> puzzled, please someone could explain this.


Undefined behaviour - and the C Standard does not attempt to explain or
predict the results of undefined behaviour.

In other words, when you break the rules, you're on your own.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
private data stashed in local/global execution context of PyEval_EvalCode disappears down the execution stack sndive@gmail.com Python 9 11-14-2007 10:31 PM
doubt in FLI Program and order of execution priya VHDL 0 10-03-2005 12:55 PM
Re: A question about order of execution? John Saunders ASP .Net 2 07-15-2004 06:44 PM
Execution order of Validation Controls Bijoy Naick ASP .Net 1 06-08-2004 04:31 PM
Execution order of PageLoad for user controls =?Utf-8?B?QmlsbCBCb3Jn?= ASP .Net 2 03-06-2004 03:01 PM



Advertisments