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

J

JesusWaffle

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?
 
B

Ben Pfaff

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.
 
T

Tomás

(e-mail address removed) 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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top