‹ BackHN Continuity

Thread

C's Flexible Integer Sizes Were Not a Design Mistake

113 points · 166 comments · ibobev

  1. adrian_b · · focus · HN ↗
    I agree that the C flexible integer sizes were still necessary at the time of its creation, when some important computers still had word sizes that were not powers of two.

    Nonetheless, I started to use C for programming only in 1990, when I got access to the Microsoft C and Borland Turbo C compilers.

    At that time, 36 years ago, the C flexible integer sizes were already obsolete.

    Since that time until now, while using C on a great variety of computers, from servers and workstations to the smallest microcontrollers, I have seen plenty of portability problems created by the existence of the flexible integer sizes.

    The only programs that had no portability problems were those that never used the flexible integer sizes, but only integers with a definite size, e.g. 8-bit, 16-bit, 32-bit or 64-bit.

    While sizeof solves the problems of memory allocation or copying, it does not help in preventing unexpected integer overflows, because even the size of "char" may be unknown, and even if the size of "char" is known, writing code with multiple paths that would check or prevent overflow for different integer sizes is very cumbersome.

    Flexible integer sizes would work well only on the old computers, where integer overflow generated a hardware exception, so installing an overflow handler would have been sufficient to make the C code work correctly regardless of the size of the native integers.

    1. nickcw · · focus · HN ↗
      The fixed size int types int32_t and friends weren't introduced until C99. Microsoft held out until 2013 before it put <inttypes.h> into Visual Studio!

      So there has been a really long time in C's evolution where we haven't had fixed size types which has been a super annoying mess of #ifdefs in portable code.

      The variable size ints have allowed some super weird architectures though. I remember looking at the datasheet for the Motorola 56000 DSP and noting that the C compiler set char = short = int = 24 bits! That was because the hardware could not address anything smaller than 24 bits. I think long could be 48 bits.

      1. ThrowawayB7 · · focus · HN ↗
        The product is named Visual C++, that it happened to do C was always kind of a sideshow as far as I could tell. Aside from that, I seem to recall that VC++ had the WORD, DWORD, and eventually QWORD macros to specify unsigned 16, 32 and 64 bit types respectively.
        1. pjmlp · · focus · HN ↗
          They considered C done, for Windows SDK purposes.

          <a href="https:&#x2F;&#x2F;herbsutter.com&#x2F;2012&#x2F;05&#x2F;03&#x2F;reader-qa-what-about-vc-and-c99" rel="nofollow">https:&#x2F;&#x2F;herbsutter.com&#x2F;2012&#x2F;05&#x2F;03&#x2F;reader-qa-what-about-vc-an...

          A few years later they backtracked on it,

          <a href="https:&#x2F;&#x2F;devblogs.microsoft.com&#x2F;cppblog&#x2F;c11-and-c17-standard-support-arriving-in-msvc" rel="nofollow">https:&#x2F;&#x2F;devblogs.microsoft.com&#x2F;cppblog&#x2F;c11-and-c17-standard-...

          Until the SFI came into play,

          <a href="https:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;trust-center&#x2F;security&#x2F;secure-future-initiative" rel="nofollow">https:&#x2F;&#x2F;www.microsoft.com&#x2F;en-us&#x2F;trust-center&#x2F;security&#x2F;secure...

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.