Talk:Stdint.h

From Wikipedia, the free encyclopedia

This article is supported by the Wikipedia:WikiProject C++. Please visit the project page for more details on the project.

[edit] Verification code

I've removed the following piece of text:

[edit] Verification Code

This simple union may be added to force the compiler to verify that the sizes are what they say they are for every build.

static union
{
        char   int8_t_incorrect        [sizeof(  int8_t) == 1];
        char   uint8_t_incorrect       [sizeof( uint8_t) == 1];
        char   int16_t_incorrect       [sizeof( int16_t) == 2];
        char   uint16_t_incorrect      [sizeof(uint16_t) == 2];
        char   int32_t_incorrect       [sizeof( int32_t) == 4];
        char   uint32_t_incorrect      [sizeof(uint32_t) == 4];
        char   int64_t_incorrect       [sizeof( int64_t) == 8]; /* Optional */
        char   uint64_t_incorrect      [sizeof(uint64_t) == 8]; /* Optional */
};

I have about three problems with it:

  1. The author seems to assume that sizeof returns a size in 8-bit bytes, but a byte is the smallest addressable unit suitable to store a character, which is not necessarily equal to 8 bits. It would seem to me that it is perfectly legal to e.g. have sizeof(int32_t) == 1 on a platform that has 32-bit addressing.
  2. The author seems to assume that the compilation fails when an array of length 0 is declared, but this is perfectly legal in C99 (where these new integer types are introduced in the first place) and many/most non-C99-compliant compilers allow 0 length arrays as an extension. Therefore, the verification code is only able to verify anything on pre-C99 compilers that do not allow 0 length arrays as an extension but that do provide the required definitions in stdint.h. This seems like a very rare situation, making the verification code useless in most cases.
  3. Even if the code above worked correctly (see 1) and reliably (see 2) I don't think an encyclopedia is the place to document programming tips-'n'-tricks. I'd like to see at least a reputable source (e.g. a standards document or a published programming book) document this trick; in that case, the article text should reference that source.

So if someone wants to add this back in, please address the issues above first, and make sure you cite your sources. Thanks! 130.89.167.52 (talk) 17:31, 25 April 2008 (UTC)