piclist 2008\07\23\141909a >
Thread: What to do about compiler bug and source code?
www.piclist.com/techref/index.htm?key=what+about+compiler
picon face BY : Tomás Ó hÉilidheemail (remove spam text)



Martin wrote:
> Postfix increment (n++) occurs after the value is used.
> i.e:
>
> b=0;
> a = b++ + 4;
>
> results in:
> b==1
> a==4
>  


Indeed.



{Quote hidden}

Your code is fine. I'll explain the problem I had:

Let's say we have a function that returns a pointer, something like:

   int *Func1(void)
   {
       static int i;

       return &i;
   }

And let's say we invoke this function as follows:

   *Func1() = 55;

Now, instead of setting it's value to 55, let's set it to the return
value of a function called "Func2":

   *Func1() = Func2();

OK now, looking at the above statement, I ask you: Which function gets
called first, Func1 or Func2? The C Standard says that this behaviour is
"unspecified", which means that the compiler can evaluate either one of
them first, and it doesn't even have to document in its manual which one
of them gets evaluated first.

So now if we look at the original code that was something like:

   *p++ = tolower(*p);

If the right-hand side gets evaluated first, then it will behave as
though it were:

   *p = tolower(*p);
    p = p + 1;

However, if the left-hand side gets evaluated first, then it will behave
as though it were:

   p = p + 1;
   *p = tolower(*p);

The best way to avoid these problems is to shy away from reading and
writing the same variable's value in one line of code.

Note that this problem is very similar to, but distinct from, the
undefined behaviour that results from reading and writing a variable's
value without a sequence point in between. An example of a sequence
point violation would be:   j = i++  *   i++;

<488775F9.10905@lavabit.com> 7bit

In reply to: <488765E0.3060804@nnytech.net>
See also: www.piclist.com/techref/index.htm?key=what+about+compiler
Reply You must be a member of the piclist mailing list (not only a www.piclist.com member) to post to the piclist. This form requires JavaScript and a browser/email client that can handle form mailto: posts.
Subject (change) What to do about compiler bug and source code?

month overview.

new search...