"tmp123" <> writes:
> Emmanuel Delahaye wrote:
>> tmp123 a écrit :
[...]
>> > or to add more
>> > variables to it. Sometimes only modify code is allowed (i.e: patching
>> > firmware in real time systems without stop them).
>>
>> Sounds to be a twisted way of thinking...
>
> No, it sounds like a real situation. Imagine you have a lot of machines
> controling some public service of your country. Imagine these machines
> have one process with lots of threads, each one controlling one user
> session. Now, ops, you find an error on code: two variables must be
> swap.
>
> What you do? Stop all country? Update a few code is easy, but better
> not to change in a live system the stack structure. Thus, use a trick
> like a^=b^... could be the difference between a big problem and a
> critical problem.
I can imagine the need to patch live code under some circumstances,
but doing so is not C programming.
It's conceivable, I suppose, that in the process of doing so you might
need to use the xor trick to swap two variables, but it hardly seems
likely.
> Moreover, in the previous post there was a lot of person saying "to
> swap you need a local variable". Well, like it is explained in the
> first level of any good programming course, there are more options.
Sure, there are more options, but no better ones. The dangers of the
xor trick are explained in the C FAQ. Some CPUs may have swap
instructions, but there's no direct way to use them from C (though a
decent optimizing compiler should be able to generate one from the
obvious code).
The way to swap two variables in C is:
temp = x;
x = y;
y = x;
If you feel the need to use some other method, you should explain why
the obvious technique won't work for you.
[...]
>> So what ? It seems that you are having hard time to find a real failiure
>> in Christian's code. Never mind, making a fool of yourself in public was
>> definitely your choice.
>
> But never ignorant.
Never? I find that difficult to believe. My own areas of ignorance
are vast (though I try to acknowledge them); I would expect the same
of any finite being.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.