4 ms·
I did this test as a break from sitting in front of Keil C51. These questions highlight a very important issue: there are platforms and compilers out there that
by kosma 10y ago
I did this test as a break from sitting in front of Keil C51. These questions highlight a very important issue: there are platforms and compilers out there that will bite you in the ass if you try to unconsciously apply what you learned while using gcc.
Under Keil C51, alignment is always 1. This is legal C.
Under Keil C51, int is 16-bit. This is legal C.
Under Keil C51, sizeof(pointer) is anything from 1 to 3. This is an extension, but a very popular one.
Don't assume that just because you can write crash-free code on x86, you "know" C.
- Gibbon1 10y agoWith gcc on an AVR an int is 16 bits and alignment is 1 as well. That said I ported a bunch of stuff from the AVR to a 32 bit ARM Cotrex and the mostly non hardware dependent stuff stuff just worked. On the other hand stdint.h is an old friend to me. Amusing bit int apple = 74000; // fails on 8 bit machines
- zokier 10y ago> Under Keil C51, sizeof(pointer) is anything from 1 to 3. This is an extension, but a very popular one I think this is the funniest thing in this all. You might cross all your t's and dot your i's, and you still may trip in a pitfall when your compiler/platform vendor happens to actually diverge from the standard. Such is life with decades old language with at least as many implementations.
- kosma 10y agoSuch is the price of using C on Harvard machines. It's even more pronounced on '51 as even the smallest $.5 chip has four (!) different address spaces (direct/register ram, indirect ram, xram and code) - and bigger chips have even more as they support memory banking. The real question is: why doesn't C support Harvard architecture natively, without ugly hacks?
- gue5t 10y agoIt's been years and I still have nightmares about using Keil toolchains.
- gridspy 10y agoAnd yet they are far better than the Modula II 8051 compiler we used to use. At least Keil C51 does static analysis on function locals to overlay them and create a "stack." We used to do that by hand.
- userbinator 10y agoint is 16-bit. This is legal C For x86 in 16-bit realmode (i.e. when you're targeting PCs on MS-DOS), that is also true. Likewise, pointers can be 2 or 4 bytes. On the other hand, if you are working with such an "exotic" (i.e. not ILP32/LLP64/LP64) platform, you probably already realise that a lot of other things are also very different and the fact that the syntax looks much the same and it's still called "C" is the least of your worries. Microchip's PIC microcontrollers also come to mind as being an architecture with a C compiler, yet with very different characteristics from the "usual" x86/ARM. (AVRs, Z80s, and the like are more similar to 16-bit x86 restricted to a single segment, a "SIP16" data model.)