473,800 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Storing an integer: why "int"?

When you want to store an integer in C++, you use an integral type, eg.

int main()
{
unsigned char amount_legs_dog = 4;
}

In writing portable C++ code, there should be only two factors that
influence which integral type you choose:

A) Signedness. Do you want only positive values? Or do you want both
positive and negative values?

B) The minimum range for that type as specified by the C++ Standard.
The minimum range for "short" and "int" are identical. The following
statement is always true on all implementations :

sizeof(short) <= sizeof(int)

As this is so, why would one ever use the type "int" at all? It seem to have
no merit whatsoever. I will always use "short" in its place.

Any thoughts on this?

-JKop
Jul 22 '05
30 2383
> So it looks like, if you're writing a program for:

a) Speed: Then use "int"

b) Optimal memory usage: Then use "short"

But then we're left with: Which should we *typically* use?
BTW, what does it mean to say that a system is 32-Bit?


int is always at least as fast as short. Good compilers can optimize
your code immensly if you use int, since int is the bus width of the
processor you are targeting for.
The old DOS (until 6.x) was a 16 bit system. Windows 3.x was, too. So
an int was (propably - depending on your compiler) 16 bits (=short).
Nowadays it's 32 bits, but the 64 bit processor families are there and
new OSes will propably have compilers that define int to 64 bits,
since the processor can handle them faster than 32 bits.
Memory efficiency is not the smaller the better, since small memory
addresses might take longer to address than e.g. 32 bit aligned
addresses. I'm not sure and if I'm wrong, flame grill me.

Anyway, use short if you have a very large set of them and use long
where you need longer values. I don't use int at all, for exaclty this
reason - I want to know the size of my variables, not the "minimum"
size of them.
Just my .02$
-Gernot
Jul 22 '05 #11
JKop wrote:

Andre Kostur posted:
JKop <NU**@NULL.NULL > wrote in
news:Pn******** **********@news .indigo.ie:
But again I want to stress that we're writing portable
code. In writing portable code, one's decision on which
integral type to use should be based solely upon:

a) Signedness

b) The minimum range

From this, (again writing portable code), "int" appears to
have no merit whatsoever, and it seems that one should always use
"short" in its place.


By your own argument, there is no merit to using a short vs. an int.
Specifically:

a) Signedness - Both int and short have the same sign (as does unsigned
int, and unsigned short)

b) The minimum range - short's minimum is <= int's minimum. Thus
anything you can store in a short is going to fit in an int


But an "int" may possibly use more memory.


But usually you don't safe much memory by using short throughout
the program. The reason is: alignement.

If the compiler sees to consecutive short's, it may insert some
padding bytes between them to satisfy alignement requirements.

struct UseShort
{
short int A;
short int B;
};

struct UseInt
{
int A;
int B;
};

In most systems, even if sizeof(short) != sizeof(int), it will
happen that sizeof( UseShort ) == sizeof( UseInt ), because the
compiler inserted some extra bytes after UseShort::A to bring
UseShort::B onto an address which satisfies the alignement.

Since int represents is the 'natural' data type of a specific
architecture, it is safe to assume that it also fullfills the
alignment requirements without a need for padding bytes.

So in theory you are right: short may use less memory. But
you pay this price with more wasted memory due to padding.

PS: Of course most compilers allow you to change the alignement
by means of some pragma or compiler option. Usually you pay
for this by increased run time. There are eg. CPU's where eg memory
access *has to be* on an even address or else the CPU generates
an exception. A operating system function then kicks in, restarts
the load, but this time at a correctly aligned address, and uses
register manipulation to fetch the bytes you want.
--
Karl Heinz Buchegger
kb******@gascad .at
Jul 22 '05 #12

"JKop" <NU**@NULL.NULL > wrote in message
news:b8******** **********@news .indigo.ie...

The minimum range for "short" and "int" are identical. The following
statement is always true on all implementations :

sizeof(short) <= sizeof(int)

As this is so, why would one ever use the type "int" at all? It seem to have no merit whatsoever. I will always use "short" in its place.


Whoa, if the sign is "less than or equal to", then why are you acting like
it's "less than"?
Jul 22 '05 #13
Karl Heinz Buchegger wrote:
JKop wrote:
Andre Kostur posted:

JKop <NU**@NULL.NULL > wrote in
news:Pn***** *************@n ews.indigo.ie:
But again I want to stress that we're writing portable
code. In writing portable code, one's decision on which
integral type to use should be based solely upon:

a) Signedness

b) The minimum range

From this, (again writing portable code), "int" appears to
have no merit whatsoever, and it seems that one should always use
"short" in its place.

By your own argument, there is no merit to using a short vs. an int.
Specifically :

a) Signedness - Both int and short have the same sign (as does unsigned
int, and unsigned short)

b) The minimum range - short's minimum is <= int's minimum. Thus
anything you can store in a short is going to fit in an int


But an "int" may possibly use more memory.

But usually you don't safe much memory by using short throughout
the program. The reason is: alignement.

If the compiler sees to consecutive short's, it may insert some
padding bytes between them to satisfy alignement requirements.

struct UseShort
{
short int A;
short int B;
};

struct UseInt
{
int A;
int B;
};

In most systems, even if sizeof(short) != sizeof(int), it will
happen that sizeof( UseShort ) == sizeof( UseInt ), because the
compiler inserted some extra bytes after UseShort::A to bring
UseShort::B onto an address which satisfies the alignement.

Since int represents is the 'natural' data type of a specific
architecture, it is safe to assume that it also fullfills the
alignment requirements without a need for padding bytes.

So in theory you are right: short may use less memory. But
you pay this price with more wasted memory due to padding.

PS: Of course most compilers allow you to change the alignement
by means of some pragma or compiler option. Usually you pay
for this by increased run time. There are eg. CPU's where eg memory
access *has to be* on an even address or else the CPU generates
an exception. A operating system function then kicks in, restarts
the load, but this time at a correctly aligned address, and uses
register manipulation to fetch the bytes you want.


On x86 processors in 32-bit mode (e.g. Linux or MS-Windows on a PC),
instructions operating on 16-bit values need a prefix code. In other
words when working with 16-bit values the code becomes larger and slower.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl
Jul 22 '05 #14
"JKop" <NU**@NULL.NULL > wrote in message
news:b8******** **********@news .indigo.ie...
In writing portable C++ code, there should be only two factors that
influence which integral type you choose:

A) Signedness. Do you want only positive values? Or do you want both
positive and negative values?

B) The minimum range for that type as specified by the C++ Standard.
The minimum range for "short" and "int" are identical. The following
statement is always true on all implementations :

sizeof(short) <= sizeof(int)

As this is so, why would one ever use the type "int" at all? It seem to have no merit whatsoever. I will always use "short" in its place.

Any thoughts on this?


This analysis is correct as far as it goes, but it doesn't go very far.

What it leaves out is the question of *why* you are using integral types in
the first place.

In my experience, almost all uses of integral types fall into two
categories:

1) Counting
2) Computation

If you are using an integral type for counting, you should probably be using
an unsigned type. Beyond that, the correct type to use depends on what you
are counting.

If you are counting elements of a data structure from the standard library,
you should be using that data structure's size_type member. So, for
example, if you wish to represent an index in a vector<string>, you should
not use int, short, or the unsigned variants thereof. Instead, you should
use vector<string>: :size_type. If you want to deal with the difference
between two vector<string> indices, which therefore might be negative, use
vector<string>: :difference_typ e.

If you are counting elements of a built-in array, or another data structure
that will fit in memory, you should use size_t. If you need a number
commensurate with the size of memory that might be negative, use ptrdiff_t.

In short, if you are counting, you should usually not use the built-in types
directly.

Now, what about computation? Most of the time, you should be using long or
unsigned long unless you have a reason to do otherwise. After all, that's
the only way that you're assured of not being limited to 16 bits.

My experience is that integer computation is actually relatively rare. Most
of the time, integers are used for counting, and in that context, it is
better to use the library-defined synonyms for the integral types than it is
to use those types directly.
Jul 22 '05 #15
JKop <NU**@NULL.NULL > wrote in news:UG******** **********@news .indigo.ie:
Andre Kostur posted:
JKop <NU**@NULL.NULL > wrote in
news:Pn******** **********@news .indigo.ie:
But again I want to stress that we're writing portable
code. In writing portable code, one's decision on which
integral type to use should be based solely upon:

a) Signedness

b) The minimum range

From this, (again writing portable code), "int" appears to
have no merit whatsoever, and it seems that one should always use
"short" in its place.


By your own argument, there is no merit to using a short vs. an int.
Specifically:

a) Signedness - Both int and short have the same sign (as does
unsigned int, and unsigned short)

b) The minimum range - short's minimum is <= int's minimum. Thus
anything you can store in a short is going to fit in an int


But an "int" may possibly use more memory.


Possibly... but mimimum memory usage wasn't in your list of criteria.
And as someone else has pointed out (Karl), whatever memory savings you
thought you had, may be consumed by the compiler anyway in order to word-
align your variables (which int's already are).
c) Word alignment - int is supposed to be the natural word length for
the platform, short has no such suggestion. Thus you have the
possibility of a more efficient (run-time) program.

So on the two points you mention, there is no benefit to using a
short vs. an int, and adding the third point tips the scales in
favour of int.


Apart ofcourse from the "int" possibly using more memory.


Again, not in your list of criteria.
However, I don't agree with the basic premise upon which your
argument is based. I belive that there are other concerns.

They way I look at it is that there's many integral types provided in
C++. The only difference between them in signedness and range. As
such, one's decision on which to choose can only be based upon those
two factors.


So by your own statement, you consider run-time efficiency, or memory
footprint to be completely irrelevant considerations. Interesting, since
as far as I know, both of these criteria are _very_ important criteria to
professional programmers (as to which is more important, it depends on
various constraints that the programmer may have to work in...)
Jul 22 '05 #16
Gernot Frisch <Me@privacy.net > wrote:
int is always at least as fast as short. Good compilers can optimize
your code immensly if you use int, since int is the bus width of the
processor you are targeting for.
The old DOS (until 6.x) was a 16 bit system. Windows 3.x was, too. So
an int was (propably - depending on your compiler) 16 bits (=short).
Nowadays it's 32 bits, but the 64 bit processor families are there and
new OSes will propably have compilers that define int to 64 bits,
since the processor can handle them faster than 32 bits.


I would guess that most compilers will keep int at 32 bits in order to
break as little code as possible (code that incorrectly assumed the
size of int).
Jul 22 '05 #17
no****@nowhere. com wrote:
I would guess that most compilers will keep int at 32 bits in order to
break as little code as possible (code that incorrectly assumed the
size of int).


Many people used to think the same thing in the 16 bit era...

--
Salu2
Jul 22 '05 #18
Karl Heinz Buchegger posted:
JKop wrote:

Andre Kostur posted:
> JKop <NU**@NULL.NULL > wrote in
> news:Pn******** **********@news .indigo.ie:
>
>> But again I want to stress that we're writing portable >> code. In writing portable code, one's decision on which >> integral type to use should be based solely upon:
>>
>> a) Signedness
>>
>> b) The minimum range
>>
>> From this, (again writing portable code), "int" appears to >> have no merit whatsoever, and it seems that one should always use >> "short" in its place.
>
> By your own argument, there is no merit to using a short vs. an int. > Specifically:
>
> a) Signedness - Both int and short have the same sign (as does > unsigned int, and unsigned short)
>
> b) The minimum range - short's minimum is <= int's minimum. Thus > anything you can store in a short is going to fit in
an int
But an "int" may possibly use more memory.
But usually you don't safe much memory by using short

throughout the program. The reason is: alignement.

If the compiler sees to consecutive short's, it may insert some padding bytes between them to satisfy alignement requirements.
struct UseShort
{
short int A;
short int B;
};

struct UseInt
{
int A;
int B;
};

In most systems, even if sizeof(short) != sizeof(int), it will happen that sizeof( UseShort ) == sizeof( UseInt ), because the compiler inserted some extra bytes after UseShort::A to bring UseShort::B onto an address which satisfies the alignement.
Since int represents is the 'natural' data type of a specific architecture, it is safe to assume that it also fullfills the alignment requirements without a need for padding bytes.

So in theory you are right: short may use less memory. But you pay this price with more wasted memory due to padding.
PS: Of course most compilers allow you to change the alignement by means of some pragma or compiler option. Usually you pay for this by increased run time. There are eg. CPU's where eg memory access *has to be* on an even address or else the CPU generates an exception. A operating system function then kicks in, restarts the load, but this time at a correctly aligned address, and uses register manipulation to fetch the bytes you want.


Well if sizeof(short) < sizeof(int), then

sizeof(short[49]) < sizeof(int[49]) as they'll be no
padding.
-JKop
Jul 22 '05 #19
Andre Kostur posted:
JKop <NU**@NULL.NULL > wrote in
news:UG******** **********@news .indigo.ie:
Andre Kostur posted:
JKop <NU**@NULL.NULL > wrote in
news:Pn******** **********@news .indigo.ie:

But again I want to stress that we're writing portable code. In writing portable code, one's decision on which integral type to use should be based solely upon:

a) Signedness

b) The minimum range

From this, (again writing portable code), "int" appears to have no merit whatsoever, and it seems that one should always use "short" in its place.

By your own argument, there is no merit to using a short vs. an int. Specifically:

a) Signedness - Both int and short have the same sign (as does unsigned int, and unsigned short)

b) The minimum range - short's minimum is <= int's minimum. Thus anything you can store in a short is going to fit in an int

But an "int" may possibly use more memory.
Possibly... but mimimum memory usage wasn't in your list

of criteria. And as someone else has pointed out (Karl), whatever memory savings you thought you had, may be consumed by the compiler anyway in order to word- align your variables (which int's already are).
c) Word alignment - int is supposed to be the natural
word length for the platform, short has no such suggestion. Thus you have the possibility of a more efficient (run-time) program.

So on the two points you mention, there is no benefit to using a short vs. an int, and adding the third point tips the scales in favour of int.
Apart ofcourse from the "int" possibly using more memory.
Again, not in your list of criteria.
However, I don't agree with the basic premise upon which your argument is based. I belive that there are other
concerns.

They way I look at it is that there's many integral types provided in C++. The only difference between them in signedness and range. As such, one's decision on which to choose can only be based upon those two factors.


So by your own statement, you consider run-time

efficiency, or memory footprint to be completely irrelevant considerations. Interesting, since as far as I know, both of these criteria are _very_ important criteria to professional programmers (as to which is more important, it depends on various constraints that the programmer may have to work in...)


memory was in fact in my criteria. The reason I pick the
smallest integral type with sufficent range is because
it's the one that uses the least memory and has sufficent
range.

-JKop
Jul 22 '05 #20

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

Similar topics

24
2380
by: Apotheosis | last post by:
The problem professor gave us is: Write a program which reads two integer values. If the first is less than the second, print the message "up". If the second is less than the first, print the message "down" If the numbers are equal, print the message "equal" If there is an error reading the data, print a message containing the word "Error" and perform exit( 0 ); And this is what I wrote:
134
9100
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
15
19973
by: Andreas Eibach | last post by:
.... but I have an unsigned long value in the printf. This warning came when I used gcc 4.x to compile. .... unsigned long offset = 0; .... Well OK, an "easy" way would be instead of printf ("eof found at offset %08x", offset); to do a type cast like
0
9690
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
9550
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
10501
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...
0
6811
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
5469
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
5603
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2944
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.