NO

Author Topic: Casting bug?  (Read 262 times)

Offline Abdulai

  • Member
  • *
  • Posts: 0
Casting bug?
« on: December 15, 2019, 10:20:59 pm »
I'have a problem with casting priorities in Pelles C V9.00.9

Example:
Code: [Select]
#include <stdio.h>

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

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

    return 0;
}

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.

Offline Pelle

  • Administrator
  • Member
  • *****
  • Posts: 1993
    • http://www.smorgasbordet.com
Re: Casting bug?
« Reply #1 on: December 22, 2019, 06:31:42 pm »
This isn't standard C, so any result is possible (and may differ between releases, unfortunately for you).
/Pelle

Offline Akko

  • Member
  • *
  • Posts: 15
Re: Casting bug?
« Reply #2 on: December 30, 2019, 09:00:54 am »
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.