473,890 Members | 1,372 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Natural size: int


On modern 32-Bit PC's, the following setup is common:

char: 8-Bit
short: 16-Bit
int: 32-Bit
long: 32-Bit

"char" is commonly used to store text characters.
"short" is commonly used to store large arrays of numbers, or perhaps wide
text characters (via wchar_t).
"int" is commonly used to store an integer.
"long" is commonly used to store an integer greater than 65535.

Now that 64-Bit machines are coming in, how should the integer types be
distributed? It makes sense that "int" should be 64-Bit... but what should
be done with "char" and "short"? Would the following be a plausible setup?

char: 8-Bit
short: 16-Bit
int: 64-Bit
long: 64-Bit

Or perhaps should "short" be 32-Bit? Or should "char" become 16-Bit (i.e.
16 == CHAR_BIT).

Another semi-related question:

If we have a variable which shall store the quantity of elements in an
array, then should we use "size_t"? On a system where "size_t" maps to
"long unsigned" rather than "int unsigned", it would seem to be inefficient
most of the time. "int unsigned" guarantees us at least 65535 array
elements -- what percentage of the time do we have an array any bigger than
that? 2% maybe? Therefore would it not make sense to use unsigned rather
than size_t to store array lengths (or the positive result of subtracting
pointers)?

--

Frederick Gotham
Aug 8 '06
78 3921
On 2006-08-08 18:56:53 -0400, jacob navia <ja***@jacob.re mcomp.frsaid:
Keith Thompson wrote:
>Al Balmer <al******@att.n etwrites:
>>On Tue, 08 Aug 2006 19:42:19 +0200, jacob navia
<ja***@jacob. remcomp.frwrote :

For unix systems, gcc decided that
char 8, short 16, int 32, long 64, long long 64

For *some* Unix systems, perhaps.

On HP-UX, a long is still 32 bits.

gcc has 32-bit longs on some systems, 64-bit longs on others. For the
most part, the division is between "32-bit systems" and "64-bit
systems", though neither phrase is particularly meaningful. I know
that some versions of HP-UX are 32-bit systems, so it wouldn't
surprise me to see 32-bit longs. If there are 64-bit versions of
HP-UX, I'd expect gcc to have 64-bit long on that system, though I
wouldn't depend on it.

I *think* that gcc typically has sizeof(long) == sizeof(void*). In
fact, all the systems I currently use, perhaps even all the systems
I've ever used, have sizeof(long)==s izeof(void*) (either 32 or 64
bits), though of course there's no requirement for them to be the same
size.

That's a VERY wise decision.

Microsoft's decision of making sizeof(long) < sizeof(void *)
meant a LOT OF WORK at a customer's site recently. It was a basic
assumption of thir code.
Well, it could be argued that it was a good decision as it helped to
expose flaws in their code.
--
Clark S. Cox, III
cl*******@gmail .com

Aug 9 '06 #31
In article <11************ **********@m73g 2000cwd.googleg roups.com"Ancie nt_Hacker" <gr**@comcast.n etwrites:
....
Case (1): I need an integer that can represent the part numbers in a
1996 Yugo, well known to range from 0 to 12754. Pascal leads the way
here: var PartNumber: 0..12754.
type PartNumber is range 0 .. 12754;
Case (2): I need a real that can represent the age of the universe, in
Planck units, about 3.4E-42 to 12.E84. The accuracy of measurement is
only plus or minus 20 percent, so two significant digits is plenty.
PL/I is the only language I know of that can adequately declare this:
DECLARE AGE FLOAT BINARY (2,85). Which hopefully the compiler will map
to whatever floating-point format can handle that exponent.
type Age is digits 2 range 3.4E-42 .. 12.0E84;
case (3): I need an integer that can do exact math with decimal prices
from 0.01 to 999,999,999,99. COBOL and PL/I can do this.
type Amount is delta 0.01 range 0.01 .. 999_999_999.99;
case (4): I need a 32-bit integer. Pascal and PL/I are the only
languages that can do this: var I32: -$7FFFFFFF..$7FF FFFFF; PL/I:
DECLARE I32 FIXED BINARY (32).
type I32 is range -16#7FFFFFFF .. 16#7FFFFFFF;
case (5): I need whatever integer format is fastest on this CPU and
is at least 32 bits wide. Don't know of any language that has this
capability.
Long_Integer

(If it is defined of course, if not, there probably are no 32 bit wide
integers available. But if it is defined, it is at least 32 bits wide.)

This all in Ada.
--
dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
Aug 9 '06 #32
jacob navia <ja***@jacob.re mcomp.frwrote:
Keith Thompson wrote:
I *think* that gcc typically has sizeof(long) == sizeof(void*). In
fact, all the systems I currently use, perhaps even all the systems
I've ever used, have sizeof(long)==s izeof(void*) (either 32 or 64
bits), though of course there's no requirement for them to be the same
size.

That's a VERY wise decision.
Why? Whatever good does it do? Don't tell me you have the beginner's
habit of casting void *s back and forth to integers...
Microsoft's decision of making sizeof(long) < sizeof(void *)
meant a LOT OF WORK at a customer's site recently. It was a basic
assumption of thir code.
It is a very, very unwise assumption - as you experienced.

Richard
Aug 9 '06 #33

Richard Bos wrote:
"Ancient_Hacker " <gr**@comcast.n etwrote:
The biness of C having wink-wink recognized defacto binary integer
widths is IMHO just way below contempt. The way was shown many
different times since 1958, why can't we get something usable, portable
and reasonable now quite a ways into the 21st century?

Darling, if you want Ada, you know where to find her.

Richard
!! ewwww. yuck. Somehow after I took one look at Ada, I just put it
out of my mind. I think a lot of people did that. Maybe when there's
a open source Ada compiler that generates good code and doesnt give me
the feeling I'm writing a cruise-missle guidance program..

Aug 9 '06 #34
jacob navia wrote:
Al Balmer wrote:
>On Tue, 08 Aug 2006 19:42:19 +0200, jacob navia
<ja***@jacob.r emcomp.frwrote:

>>For unix systems, gcc decided that
char 8, short 16, int 32, long 64, long long 64


For *some* Unix systems, perhaps.

On HP-UX, a long is still 32 bits.
Interesting...

What does gcc do in those systems?

It follows HP or remains compatible with itself?
gcc is retargetable; it has multiple backends. Your question makes no sense.
gcc doesn't make specific assumptions and then looks for platforms with
which it's compatible; gcc is ported by adapting the backend to the platform.

If it didn't, it would be a whole lot less portable, and a whole lot less
useful.

S.
Aug 9 '06 #35
"Ancient_Hacker " <gr**@comcast.n etwrites:
Richard Bos wrote:
>"Ancient_Hacke r" <gr**@comcast.n etwrote:
The biness of C having wink-wink recognized defacto binary integer
widths is IMHO just way below contempt. The way was shown many
different times since 1958, why can't we get something usable, portable
and reasonable now quite a ways into the 21st century?

Darling, if you want Ada, you know where to find her.

Richard

!! ewwww. yuck. Somehow after I took one look at Ada, I just put it
out of my mind. I think a lot of people did that. Maybe when there's
a open source Ada compiler that generates good code and doesnt give me
the feeling I'm writing a cruise-missle guidance program..
<OT>
There is an open source Ada compiler; it's called GNAT, and it's part
of gcc. Nobody can do anything about how you feel when you're writing
code, though.

You complain that no one language provides a set of features. When
someone mentions one that does, you reaction is "ewwww. yuck." Hmm.
</OT>

--
Keith Thompson (The_Other_Keit h) 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.
Aug 9 '06 #36
Ancient_Hacker wrote:
The funny thing is this issue was partly solved in 1958, 1964, and in
1971.

In 1958 Grace Hopper and Co. designed COBOL so you could actually
declare variables and their allowed range of values! IIRC something
like:

001 DECLARE MYPAY PACKED-DECIMAL PICTURE "999999999999V9 99"
001 DECLARE MYPAY USAGE IS COMPUTATIONAL-/1/2/3

Miracle! A variable with predictable and reliable bounds! Zounds!
COBOL more than made up for its innovative and genuinely useful approach to
type declarations with its innovative and genuinely horrendous approaches to
syntax, control flow and type conversions. That said, it's good to remember
the ways in which COBOL didn't suck.

<snip>
What the typical user REALLY needs, and may not be doable in any simple
or portable way, is a wide choice of what one wants:
Well, it's still a C group, so let's look at how far our language of
topicality gets us.
Case (1): I need an integer that can represent the part numbers in a
1996 Yugo, well known to range from 0 to 12754. Pascal leads the way
here: var PartNumber: 0..12754.
Although C offers less flexibility and safety in this regard, an adequate
approximation is to use the smallest type guaranteed to contain your range.
In this case, "short" (or "int" if time is of more concern than space, since
"int" is intended to be more "natural" to the platform).

You can use a typedef to abstract away from the actual type and convey the
purpose to humans, but C does not allow true subtyping, so you'd have to do
any range checks yourself. This obviously encourages a style where these
checks are done as little as possible, or possibly never, which is a clear
drawback.
Case (2): I need a real that can represent the age of the universe, in
Planck units, about 3.4E-42 to 12.E84. The accuracy of measurement is
only plus or minus 20 percent, so two significant digits is plenty.
PL/I is the only language I know of that can adequately declare this:
DECLARE AGE FLOAT BINARY (2,85). Which hopefully the compiler will map
to whatever floating-point format can handle that exponent.
And which will hopefully not introduce any glaring rounding or accuracy
errors when involved in calculations with other floating-point types. That
the language offers this is neat, but in order to use these types
effectively special attention is required, which reduces the advantage over
not having these types natively and having to think about the calculations
from the beginning.
case (3): I need an integer that can do exact math with decimal prices
from 0.01 to 999,999,999,99. COBOL and PL/I can do this.
C cannot do this natively, so you'll need libraries. Luckily, C also makes
it possible to implement such libraries efficiently. This is a good way of
highlighting the differences in philosophy.
case (4): I need a 32-bit integer. Pascal and PL/I are the only
languages that can do this: var I32: -$7FFFFFFF..$7FF FFFFF; PL/I:
DECLARE I32 FIXED BINARY (32).
You can do this in C, but only if your platform actually provides a native
integer type of exactly 32 bits. Then #include <stdint.hand use int32_t.
If your platform doesn't have <stdint.h>, porting won't be for free, but not
exactly involved either.

This boils down to the question of *why* you need a 32-bit integer. Do you
absolutely, positively, have to have an integral type that takes up exactly
32 bits, or else your algorithm will simply fail or be inapplicable? Then
int32_t is exactly what you mean.

If you'd merely like an integer type with range
[-2,147,483,647;2 ,147,483,647] which may use more bits if this is
convenient, then you're back to case (1), and for C "long" will do the
trick. Again, range checking you will have to handle yourself.
case (5): I need whatever integer format is fastest on this CPU and
is at least 32 bits wide. Don't know of any language that has this
capability.
That's odd, because this is just one of those things C was practically made
to do. I should think C's "long" will get you as close as possible. Or
#include <stdint.hand use int_fast32_t to emphasize "fastest". In any
case, if such an integer format exists at all, you will surely find it
implemented in the platform's C compilers.

<snip>
The biness of C having wink-wink recognized defacto binary integer
widths is IMHO just way below contempt.
You're doing the language a disservice by describing it this way. C has
well-defined constraints on its integer types, designed to allow a broad
range of platforms to provide integer calculations in a way most suited to
that platform. It's true that this shifts the burden to the programmer in a
way that may be considered inappropriate for many applications, but it's
certainly not "wink-wink" or "de facto" -- and who cares if it started out
this way?

"De facto" assumptions are the reason programmers get the worst of both
worlds: C leaves the types relatively unconstrained to promote portability,
yet (poor) programmers will assume that the integer types will be identical
across platforms. You can prefer one or the other, but shouldn't be
expecting the opposite of what you actually have and be dismayed by the results.
<soap>
I think it's high time a language has the ability to do the very basic
and simple things programmers need to write portable software:
The one thing programmers need to write portable software is a good grasp of
the ways in which different computer systems are equal, and the ways in
which they are different, and the ways in which this might matter to your
specific program. No individual language will confer such insight.
the ability to specify, unambiguously, what range of values they need to
represent, preferably in decimal, binary, floating, fixed, and even
arbitrary bignum formats. Not to mention hardware-dependent perhaps bit
widths. There's no need for the compiler to be able to actually *do*
any arbitrarily difficult arithmetic, but at least give the programmer
the ability to ASK and if the compiler is capable, and get DEPENDABLE
math. I don't think this is asking too much.
If there's no need for the compiler to be able to do what you ask, then how
is your program portable? You're asking for a system that implements what
you want it to implement, or else your program will not run on it. This is
portability by demanding the system be suitable for your program, rather
than the other way around.

All programming languages are a compromise between allowing the programmer
to write down exactly what they want and allowing the programmer to write
down that which is effectively translatable and executable. You can take
individual languages to task for making the wrong compromises for your
purposes, but taking them to task for compromising at all is slightly
disingenuous.

As others have mentioned, Ada goes a long way towards implementing the
numerical paradise you envision. Of course, Ada has its own problems.
The way was shown many different times since 1958, why can't we get
something usable, portable and reasonable now quite a ways into the 21st
century?
Reasonability is in the eye of the beholder, but anyone who would deny C's
usability or portability, even when only restricted to numerical types, is
being deliberately obtuse.

If you're asking in general how languages are created and adopted, and why
the Right Language always seems to lose to the Right Now Language, that's
quite another topic.

S.
Aug 9 '06 #37
On Wed, 09 Aug 2006 20:40:52 +0200, Skarmander
<in*****@dontma ilme.comwrote:
>You can use a typedef to abstract away from the actual type and convey the
purpose to humans, but C does not allow true subtyping, so you'd have to do
any range checks yourself. This obviously encourages a style where these
checks are done as little as possible, or possibly never, which is a clear
drawback.
Perhaps, but it also allows a style where such checks are done only
when necessary.

--
Al Balmer
Sun City, AZ
Aug 9 '06 #38
"Stephen Sprunk" <st*****@sprunk .orgwrote
>
Do you really care so much about the extra two or three letters it takes
to use a type that is _guaranteed_ to work that you're willing to accept
your program randomly breaking?
Yes. If we just have one variable in scope, a bit of gibberish at the start
and end of the type is neither here nor there.
But if we've got several in scope, and we need another gibberish prefix like
xlu_ to represent the library we're using, and another gibberish suffix like
U8 because someone has insisted on that notation, and the code is littered
with gibberish casts and gibberish comments, and gibberish constructs like
for(;;) and "123 = xlu_i_U8;" then the gibberish accumulates, and suddenly
you find youself in the situation where the code is no longer readable.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.

Aug 9 '06 #39
"jacob navia" <ja***@jacob.re mcomp.frwrote in message
news:44******** *************@n ews.orange.fr.. .
Ian Collins wrote:
>LP64 is by far the most common model on UNIX and UNIX like
systems and the main reason is probably pragmatic - it's the most
straightforwar d model to port 32 bit applications to.

Microsoft disagrees... :-)
MS also disagrees with C standard (and hundreds of other standards)
compliance; they're hardly an authority unless your only goal is to be
compatible with their stuff.

IL32LLP64 makes sense when either (a) 32-bit ops are faster than
64-bit ones, or (b) you know the vast majority of the codebase assumes
longs are only 32-bit. Both apply to MS's target market for their
compiler and OS; they don't apply to the general UNIX world.
I am not saying that gcc's decision is bad, I am just stating this
as
a fact without any value judgement. Gcc is by far the most widely
used compiler under Unix, and they decided LP64, what probably is
a good decision for them.
I'm sure whoever came up with the Linux ABI consulted the GCC folks,
but such things are primarily determined by the OS developers, not the
compiler developers. If the compiler doesn't follow the OS API, it's
close to useless.
I have to follow the lead compiler in each system. By the way, the
lead compiler in an operating system is the compiler that compiled
the Operating System: MSVC under windows, gcc under linux, etc.
You're making distinctions where they aren't needed. Each platform
has an official ABI (or two), and that ABI is determined jointly by
the compiler folks and the OS folks. If you weren't included in that
discussion, all you can do is follow the ABI that they specified.

Heck, that's the position GCC is in on most platforms, and they have a
hell of a lot more influence than you do.

S

--
Stephen Sprunk "God does not play dice." --Albert Einstein
CCIE #3723 "God is an inveterate gambler, and He throws the
K5SSS dice at every possible opportunity." --Stephen Hawking

--
Posted via a free Usenet account from http://www.teranews.com

Aug 9 '06 #40

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

Similar topics

0
1329
by: C. Barnes | last post by:
Summary: Sorts strings in a way that seems natural to humans. If the strings contain integers, then the integers are ordered numerically. For example, sorts into the order . Code:
3
1750
by: Derek Basch | last post by:
Hello All, I need to sort a list using an unnatural sequence. I have a list like so: foo = print foo.sort()
1
2430
by: Connelly Barnes | last post by:
Summary: Sorts strings in a way that seems natural to humans. If the strings contain integers, then the integers are ordered numerically. For example, sorts into the order . Code: #---------------------------------------------------------
4
2198
by: Andrew E | last post by:
Hi all I've written a python program that adds orders into our order routing simulation system. It works well, and has a syntax along these lines: ./neworder --instrument NOKIA --size 23 --price MARKET --repeats 20 etc However, I'd like to add a mode that will handle, say:
7
1883
by: tommaso.gastaldi | last post by:
This is a curious question. I'd like to know your opinion. I am attaching a drawing resize to a mouse wheel event. Frankly, to me was most natural that, if I mouse wheel UP, the shape gets bigger, while it gets smaller when mouse wheel DOWN. I just noticed that Photoshop does the opposite. Any though about the most natural way (or perhaps are both "natural") ?
1
1766
by: Hendri Adriaens | last post by:
Hi, I have an onmouseover script that displays a div with some comment. There are several links getting this onmouseover functionality, all with their own comment text. Now I want the div to have a max width, but if the natural width is smaller than the max width, it should get the natural width. commentObj is the div and commentObj.offsetWidth returns the natural width, as long as we didn't set it to anything else (it seems). As soon as...
1
2639
by: Steven Bird | last post by:
NLTK ó the Natural Language Toolkit ó is a suite of open source Python modules, data sets and tutorials supporting research and development in natural language processing. It comes with 50k lines of code, 300Mb of datasets, and a 360 page book which teaches both Python and Natural Language Processing. NLTK has been adopted in at least 40 university courses. NLTK is hosted on sourceforge, and is ranked in the top 200 projects. ...
3
4325
by: anon538 | last post by:
I am creating an application that has to check for natural numbers. By natural numbers, I mean positive whole numbers. //natural needs to equal 1,2,3, etc. if(minutes % 60 == natural) { minuteField.value = minutes; } How can I can I do this? Thanks.
10
8086
by: Ben | last post by:
Hi at all I am looking for the natural size of an image I found this function : while ((imgHeight = image.getHeight(this)) == -1 ) { // loop until image loaded
0
9979
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9826
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11234
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10925
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7171
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5854
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6058
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4276
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.