NO

Author Topic: bug ?, variable pointer handling (very stubid way)  (Read 2615 times)

jackz

  • Guest
bug ?, variable pointer handling (very stubid way)
« on: July 24, 2008, 10:24:12 PM »
not sure if this is really bug or undocumented behavior.
but gcc and vc++ gives right values.

pelles c 5.00.8

Code: [Select]
main()
{
char k[15],h='m',*l=k;
*l++=h;      /*m*/
*l++=h-4;   /*i*/
        *l++=h+1;   /*n*/
*l++=*(l-1); /*should be n, is 0x00*/
}

severach

  • Guest
Re: bug ?, variable pointer handling (very stubid way)
« Reply #1 on: July 27, 2008, 08:57:42 PM »
You have used the variable being incremented more than once. The standard does not define how such expressions are to be evaluated so compilers are free to use whatever method is most convenient. Rewrite your code to eliminate the ambiguity.

I have found that compilers attempting to perform the increment in an expression at the exact right time end up producing less optimized code than if the increment was separated. Separating the increment gives the optimizer more freedom. It may be short hand for you but it is extra work for the compiler. Separate the increment and eliminate all of the problems.

Mikeall

  • Guest
Re: bug ?, variable pointer handling (very stubid way)
« Reply #2 on: July 30, 2008, 09:44:12 AM »
On page 47 of K&R it shows
  s[ j++ ] = s[ i ];
and states this is the same as
  s[ j ] = s[ i ];
  j++;
Although in this case the variables are different, it seems reasonable that one would expect that the increment occur after the lvalue assignment is made even when the variables are the same.  This would also make Pelles code port easier to most other compilers.

BTW, this syntax:
  char k[15], h = 'm', *l = k;
rather than:
  char k[15], h = 'm';
  char *l = k;
generates an Error Code 42; then if you attempt to edit and save the source file, a dialog says that the process cannot access the file because it is still in use.  Instead, you have to restart the IDE to release the file.  Although the error may be correct, the process needs to release the file.
( Pelles v5.00.8 )