473,387 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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 '06 #1
3 1865
"Je*********@gmail.com" <Je*********@gmail.com> 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
May 15 '06 #2
"Je*********@gmail.com" <Je*********@gmail.com> 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) ks***@mib.org <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.
May 15 '06 #3
Je*********@gmail.com 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
May 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Ptp | last post by:
is there a integer data type of one byte in gcc? I know delphi have fundamental integer types include byte, shortint... the byte types is unsigned 8-bit, such as below: procedure...
38
by: Alan | last post by:
I want to change a 3 digits integer to characters, how can i do that? the 3 digits integer maybe 123, 23 or 3 I want to change the integer to "123", " 23" or " 3" thx
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
5
by: Arnaud Legrand | last post by:
Hello, I have a question about portability and I have not found the answer in the FAQ. I have different modules to build. All of them have the same private part and a common public part (though...
131
by: pemo | last post by:
Is C really portable? And, apologies, but this is possibly a little OT? In c.l.c we often see 'not portable' comments, but I wonder just how portable C apps really are. I don't write...
13
by: Tomás | last post by:
Let's start off with: class Nation { public: virtual const char* GetName() const = 0; } class Norway : public Nation { public: virtual const char* GetName() const
1
by: Slain | last post by:
I am a beginner and have some confusion with respect to pointers and strings. It seems that the pointers with dealing with integer arrays behave differently, as opposed to strings. Can some one...
21
by: no1zson | last post by:
I do not even know how to correctly ask this question. I have an item field in the code I am about to post. Simple intger meant to be an item number for a cd. The user enters this number. Over the...
3
by: Martin T. | last post by:
Hello. I tried to overload the operator<< for implicit printing of wchar_t string on a char stream. Normally using it on a ostream will succeed as std::operator<<<std::char_traits<char> will...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.