Pelles C > Bug reports

Casting bug?

(1/1)

Abdulai:
I'have a problem with casting priorities in Pelles C V9.00.9

Example:

--- Code: ---#include <stdio.h>

char Buffer[0x1000] ;
char *pBuf = Buffer ;

int main(int argc, char *argv[])
{
    *((short int*)pBuf)++ = 0xA0D ;  // Wrong compilation

    return 0;
}
--- End code ---

Other compilers, including version 7.00.355 , compile this line correctly.

That is: First 0xA0D is assigned and then the pointer is incremented (+2).

But this version first increases the pointer (+2) and then assigns 0xA0D.


Congratulations for your amazing work.

Pelle:
This isn't standard C, so any result is possible (and may differ between releases, unfortunately for you).

Akko:
Another comment from me after half a century of programming experience:

That example would not have happened had you followed any recommended good coding style.
Keep it simple, be more verbose, only one action per line, etc.
Let the compiler do the optimization!
Writing convoluted code can become even counterproductive for the optimizer!

Your profit: less errors, easier debugging, better readability, better maintenance.

PabloMack:
I have run into what I think is a casting bug and I think it is proper C
because the cast is an RValue. I greatly simplified the example just
for illustration of what I am trying to do. This example is just supposed
to copy an 8-byte value from a 64-bit integer to an 8-byte array. Endian-ness
shouldn't matter as it should just copy 8 bytes in memory order.
The Cmp image should end up exactly the same as the Val original,
but it doesn't. It looks like it is pre-incrementing instead of post-
incrementing.  The code does work correctly when I remove the auto
increments and add them on separate lines.

#define ub unsigned char
#define uo unsigned long long

uo Val=0xFEDCBA9876543210;
uo Nxt=0xBEEFABBACADF2468;
ub Cmp[8];

//Test Copy
void copy_uo(ub *Dst,uo *Src)
{
  int ix=0;
  while (ix < 8 )
    Dst[ix++] = *((ub *)Src)++;
}

void main()
{
  //Test Copy
  copy_uo(Cmp,&Val);

  //Print Original
  printf("Val:$%016llX\n",Val);
  printf("Cmp:$%016llX\n",*((uo *)Cmp));
}

Output:
Val:$FEDCBA9876543210
Cmp:$68FEDCBA98765432

Pelle:
This isn't standard C. Cast on an l-value (due to the increment) is a Microsoft extension. There is a known problem with this extension in the current version of Pelles C, and the next version will fix this and give a more expected result, but it's still an extension that should be avoided at all cost (at least IMO).

Navigation

[0] Message Index

Go to full version