Why Use 'Int' Instead of 'Long'? (With today's compilers) 
Author Message
 Why Use 'Int' Instead of 'Long'? (With today's compilers)

I am curious why one would use int instead of long with many of the
compilers today. I have found that by printing the sizeof(int) and the
sizeof(long) to be 4 bytes (32-bits) under OSs like Windows NT and the
Unix flavors I have access to (with gcc). This is the same for
sizeof(long). So, what benefit is there in using ints? If I always
used longs that I could use bigger numbers if I needed them, without
taking more space than ints. Is this right or am I way off track?
Thanks in advance,

Abu Wawda



Wed, 29 Jul 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)

Quote:

>I am curious why one would use int instead of long with many of the
>compilers today. I have found that by printing the sizeof(int) and the
>sizeof(long) to be 4 bytes (32-bits) under OSs like Windows NT and the
>Unix flavors I have access to (with gcc). This is the same for
>sizeof(long). So, what benefit is there in using ints? If I always
>used longs that I could use bigger numbers if I needed them, without
>taking more space than ints. Is this right or am I way off track?

You're way off track.

1. I'm typing this message on a modern Unix system where sizeof(long) is 8
   on all the compilers (including gcc).  Using long instead of int will
   be a waste of memory.

2. On systems where sizeof(int) == sizeof(long), using long instead of
   int buys you nothing, because both types can represent exactly the
   same range of integers.

If you write code that should be maximally portable (very few people
actually do) use long whenever you need a range broader than that
guaranteed for int (-32767..32767).  

Using long when int is enough may result in a waste of memory, cpu cycles
or both.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Wed, 29 Jul 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)
] I am curious why one would use int instead of long with many of the
] compilers today. I have found that by printing the sizeof(int) and the
] sizeof(long) to be 4 bytes (32-bits) under OSs like Windows NT and the
] Unix flavors I have access to (with gcc). This is the same for
] sizeof(long). So, what benefit is there in using ints?

    On some platforms (like 16-bit MS-DOS), int is shorter than long.
It's also faster.

] If I always
] used longs that I could use bigger numbers if I needed them, without
] taking more space than ints. Is this right or am I way off track?

    If the two are actually the same size, then they hold the same range
of numbers, and they have the same performance--there's no advantage to
using one or the other.  You don't get more numbers in a long just
because it's called "long" in your source code.

    You do get a larger range of _guaranteed_ numbers (guaranteed by
ANSI), but only because ints can be 16-bit quantities on some platforms,
not because long somehow packs more data into the same space, or 4-byte
ints are somehow hobbled to only accept 2-byte numbers.

                                           -- Bill K.

Bill Kaufman               | " While not a master of intellect, the blatantly

                           |   escape his keen eye! "          -- Bob Burden



Thu, 30 Jul 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)
ints and longs are not always going to be the same size; even if
they are the same, many times it is prudent to use long instead
of int for portability purposes.

Quote:

> I am curious why one would use int instead of long with many of the
> compilers today. I have found that by printing the sizeof(int) and the
> sizeof(long) to be 4 bytes (32-bits) under OSs like Windows NT and the
> Unix flavors I have access to (with gcc). This is the same for
> sizeof(long). So, what benefit is there in using ints? If I always
> used longs that I could use bigger numbers if I needed them, without
> taking more space than ints. Is this right or am I way off track?
> Thanks in advance,

> Abu Wawda


--
David C. Wyatt (The Other Dave)          Tel.  (410) 381-2270
Meridian Sciences, Inc.                  FAX:  (410) 381-2167
10015 Old Columbia Road, Suite A-200     e-mail:

Columbia, Maryland  21046                WWW:
http://www.mersci.com/Tod


Fri, 31 Jul 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)

Quote:

>>I am curious why one would use int instead of long with many of the
>>compilers today. I have found that by printing the sizeof(int) and the
>>sizeof(long) to be 4 bytes (32-bits) under OSs like Windows NT and the
>>Unix flavors I have access to (with gcc). This is the same for
>>sizeof(long). So, what benefit is there in using ints? If I always
>>used longs that I could use bigger numbers if I needed them, without
>>taking more space than ints. Is this right or am I way off track?

I've had a lot of code break because sizeof(int) != sizeof(long) on the
operating system of the machine I'm currently sitting at (A DEC Alpha
running OSF/1). On these, a long is 64 bits and an int is 32 bits.

On a related note, is it a truism that sizeof(void *) == sizeof(long int)?
It is on everything I've worked with, but is it universal?

--
-------------------------------------------------------------------------------
   Why do people surf the Information Superhighway? Won't they get run over?
               http://www-hons-cs.cs.st-andrews.ac.uk/~dg
              Sun-Earther David Daton Given  of Lochcarron



Fri, 31 Jul 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)

: On a related note, is it a truism that sizeof(void *) == sizeof(long int)?
: It is on everything I've worked with, but is it universal?

No.  With the Keil C compiler for the 8051 family of microcontrollers,
sizeof(void *) == 3.

--
Mark C. Orton
employed by (but not speaking for)
Pulse Communications, Inc.



Sun, 02 Aug 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)

Quote:

> I've had a lot of code break because sizeof(int) != sizeof(long) on the
> operating system of the machine I'm currently sitting at (A DEC Alpha
> running OSF/1). On these, a long is 64 bits and an int is 32 bits.

> On a related note, is it a truism that sizeof(void *) == sizeof(long int)?
> It is on everything I've worked with, but is it universal?

No. sizeof(void*) gives the size of the pointer used in the machine. It depends on
the memory access architecture. For example, in a PC running in real mode,
you can have near or far pointers, depending on the memory model of the program,
which differ in size: sizeof(void far *) > sizeof(void far *).

Standard states that sizeof(int) is the size of the register(s) used for arithmetics
and sizeof(long int) >= sizeof(int). Also, it's stated that sizeof(int) >=
sizeof(short int) >= sizeof(char). There is no statement to define the size of a
pointer related to anything.

Later,
 AriL
--
All my opinions are mine and mine alone.



Sun, 02 Aug 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)

Quote:
>On a related note, is it a truism that sizeof(void *) == sizeof(long int)?
>It is on everything I've worked with, but is it universal?

No. On a 8086 system with small memory models, you have

  sizeof(long int) == 4

but

  sizeof(void*) == 2
--
Henning Makholm - math and CS student - University of Copenhagen



Sun, 02 Aug 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)

Quote:
> On a related note, is it a truism that sizeof(void *) == sizeof(long int)?
> It is on everything I've worked with, but is it universal?

The C standard allows a pointer to be converted to an integral type,
but allows the sizes to be implementation defined.
There are systems where pointers are smaller than longs. Take x86 small
model systems where pointer and ints are 16 bits and longs are 32 bits.
Also, Digital Unix allows programs to exist in 32 bit mode with 32 bit
pointers if the programmer so desires.
--
Jerry Feldman              Digital Unix Development Environment
Digital Equipment Corp.    Mailstop: ZKO3-3/Y25

Nashua, NH 03062-9987      (603)881-2970


Sun, 02 Aug 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)

: On a related note, is it a truism that sizeof(void *) == sizeof(long int)?
: It is on everything I've worked with, but is it universal?

No.   I'm using an 8086 C Compiler for my Psion 3a PDA.   The Psion uses
the small memory model where there is a restriction of 64K code & data
segments.   Therefore sizeof(void *) is 2 but sizeof(long int) is 4.

        Keith
--
Keith Walker, ICL, Lovelace Rd., Bracknell, Berkshire, United Kingdom RG12 8SN



Sun, 02 Aug 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)

Quote:
>On a related note, is it a truism that sizeof(void *) == sizeof(long int)?
>It is on everything I've worked with, but is it universal?

No.  It's false on MSDOS (in the small and medium memory models), as well
as in most C implementations on 16-bit platforms, including the original
Ritchie compiler.  Most likely, it's false on AS/400 as well, unless
sizeof(long) == 16 on that platform.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Sun, 02 Aug 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)

Quote:
>Standard states that sizeof(int) is the size of the register(s) used for arithmetics

Chapter and verse, please.  I couldn't find such a statement in the
C standard and I'm typing this text on a machine where the size of an
int is half the size of the integer registers.  This is actually very
common, all people who run 16-bit code on 386 (or better) PC's are in
the same situation.

I also used C compilers on 8-bit platforms, where the size of an int was
twice the size of a register.

Dan
--
Dan Pop
CERN, CN Division

Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland



Sun, 02 Aug 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)

Quote:
> On a related note, is it a truism that sizeof(void *) == sizeof(long int)?
> It is on everything I've worked with, but is it universal?

  I don't think it is universal ...
  The size of pointers varies from machine to machine, but the
size of an int is defined in ANSI to be at least 2 chars. And
the size of long int must be strictly bigger than short int,
and must be bigger than or equal to int.

  Since there is an lower bound on the size of long int, but
the size of a pointer has no bound, sizeof (void *) == sizeof
(long int) is not universal.

Mongkok


http://www.undergrad.math.uwaterloo.ca/~jyetse/



Sun, 02 Aug 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)

Quote:
> No. sizeof(void*) gives the size of the pointer used in the machine.
> It depends on the memory access architecture. For example, in a PC
> running in real mode, you can have near or far pointers, depending on
> the memory model of the program, which differ in size:
> sizeof(void far *) > sizeof(void far *).

  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  Windows can't be _that_ crazy, can it? :)

[No.  Originally, that read
        > sizeof(void far *) > sizeof(near far *).
 Then he wrote and pointed out the "obvious" typo.  So I corrected it -
 except I replaced the wrong word.  I am planning to get some sleep
 within the next week or two. -mod]


/*   Indeed, C++ is a bit of an oddball of a language ... given the way that *
 * it requires private parts to be visible.  This increases the strength of  *
 * coupling dramatically...                       -- Dr. Rich Artym          */



Sun, 02 Aug 1998 03:00:00 GMT  
 Why Use 'Int' Instead of 'Long'? (With today's compilers)

<snip>
   Standard states that sizeof(int) is the size of the register(s) used for arithmetics
   and sizeof(long int) >= sizeof(int). Also, it's stated that sizeof(int) >=
   sizeof(short int) >= sizeof(char). There is no statement to define the size of a
   pointer related to anything.

Except for everything being >= sizeof(char), I do not think that the
standard makes any statements about the relative sizeof's. It defines
a larger integral type as one that is able to represent all values
representable in a smaller integral type of the same signedness. As
integral types can have unused bits, this does not directly say
anything about their size.

Whether the standard states it or not, it is likely to be the most
common implementation :-)

Cheers
Tanmoy
--

Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
<http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]



Sun, 02 Aug 1998 03:00:00 GMT  
 
 [ 48 post ]  Go to page: [1] [2] [3] [4]

 Relevant Pages 

1. long long in switch 'case'??????

2. Y2K Solutions for Legacy 'C' Today

3. Why 'struct foo *x' instead of 'foo *x'

4. DTPicker withour 'Today' option

5. '+=' : conversion from 'int' to 'unsigned char', possible loss of data

6. Q : ',' in float's instead of '.'

7. Getting an item by it's name (instead of it's index#)

8. C1001: INTERNAL COMPILER ERROR (compiler files 'msc1.cpp', line 2844)

9. Assigning and printing long long int's

10. C1001: INTERNAL COMPILER ERROR (compiler file 'msc1.cpp', line 1794)

11. INTERNAL COMPILER ERROR - compiler file 'msc1.cpp', line 1794)

12. '0', '1', '2', '3' ...???

 

 
Powered by phpBB® Forum Software