Programming Oddities sizeof

Comments [0]

One of the strangest programming concepts that I came across was working with an embedded processor, Analog Devices SHARC ADSP-21062, on a project. I was shocked when I found out the following:

sizeof(char) == sizeof(short) == sizeof(int) == 1

I didn't think this was possible, but sure enough it was. After reviewing the C standard I realized that this was allowed and the only requirement was: sizeof(char) <= sizeof(short) <= sizeof(int).

This actually had a profound affect on a later project using the same processor family when I was porting an embedded GUI, Nano-X or formerly microwindows, to that processor. The issue was mainly with assumptions on casting to (char). This affected several areas in the source code as well as the fonts. The assumption was that casting to (char) automatically bit-masked the value to 8-bits:

result = (char) value;    // assumed 8-bit result
result = value & 0x00FF;  // explicit bitmask

The solution was to locate all casts and determine the impact and convert them to casts. I ended up using a preprocessor macro to maintain compatibility and speed where it would use the cast on normal operating systems and processors but use the bitmask on the Analog Devices processor.

Comment Section

Comments are closed.