Portable integer type which can hold int, wchar_t, and void *

Discussion in 'C Programming' started by JesusWaffle@gmail.com, May 15, 2006.

  1. Guest

    I'm trying to portably define a type which is wide enough to hold an
    int, a wchar_t, or a void *. Here's what I've got:

    #include <stdint.h>
    #include <wchar.h>
    #include <limits.h>

    #if INT_MAX > INT32_MAX || WCHAR_MAX > INT32_MAX || \
    INTPTR_MAX > INT32_MAX
    typedef int64_t funky_type;
    #elif INT_MAX > INT16_MAX || WCHAR_MAX > INT16_MAX || \
    INTPTR_MAX > INT16_MAX
    typedef int32_t funky_type;
    #else
    typedef int16_t funky_type;
    #endif

    There seem to be a lot of language lawyers here, so: are there any
    corner cases that my definition misses? Will it be wide enough in all
    cases? Are there any simplifications I could make?
    , May 15, 2006
    #1
    1. Advertising

  2. Ben Pfaff Guest

    "" <> writes:

    > I'm trying to portably define a type which is wide enough to hold an
    > int, a wchar_t, or a void *. Here's what I've got:
    >
    > #include <stdint.h>
    > #include <wchar.h>
    > #include <limits.h>
    >
    > #if INT_MAX > INT32_MAX || WCHAR_MAX > INT32_MAX || \
    > INTPTR_MAX > INT32_MAX
    > typedef int64_t funky_type;
    > #elif INT_MAX > INT16_MAX || WCHAR_MAX > INT16_MAX || \
    > INTPTR_MAX > INT16_MAX
    > typedef int32_t funky_type;
    > #else
    > typedef int16_t funky_type;
    > #endif


    I have to say that I don't understand your logic here. Here is
    what occurs to me at first:

    #define MAX(A, B) ((A) > (B) ? (A) : (B))
    #define MAX3(A, B, C) (MAX(A, MAX(B, C)))

    #if MAX3(INT_MAX, WCHAR_MAX, INTPTR_MAX) == INT_MAX
    typedef int funky_type;
    #elif MAX3(INT_MAX, WCHAR_MAX, INTPTR_MAX) == WCHAR_MAX
    typedef wchar_t funky_type;
    #else /* MAX3(INT_MAX, WCHAR_MAX, INTPTR_MAX) == INTPTR_MAX */
    typedef intptr_t funky_type;
    #endif

    I don't understand the desire to tie funky_type to an int<N>_t
    type.
    --
    "I'm not here to convince idiots not to be stupid.
    They won't listen anyway."
    --Dann Corbit
    Ben Pfaff, May 15, 2006
    #2
    1. Advertising

  3. "" <> writes:
    > I'm trying to portably define a type which is wide enough to hold an
    > int, a wchar_t, or a void *.


    union funky_type {
    int i;
    wchar_t w;
    void *p;
    };

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, May 15, 2006
    #3
  4. Tomás Guest

    posted:

    > I'm trying to portably define a type which is wide enough to hold...



    An unsigned long long has enough value representation bits to store the
    value of an "int" or a "wchar_t".

    As for "void*", there's no such guarantee. However, "unsigned long long"
    will work 90% of the time.


    -Tomás
    Tomás, May 15, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vinu
    Replies:
    9
    Views:
    593
  2. Replies:
    5
    Views:
    815
    S.Tobias
    Jul 22, 2005
  3. Abhishek
    Replies:
    12
    Views:
    787
    Eric Sosman
    Jan 30, 2006
  4. Replies:
    1
    Views:
    392
    Victor Bazarov
    May 23, 2007
  5. Replies:
    3
    Views:
    1,080
    James Kanze
    Aug 15, 2008
Loading...

Share This Page