why integer size says 4 bytes on 64 bit processor

Matthew Seaman m.seaman at infracaninophile.co.uk
Mon May 25 12:00:40 UTC 2009


Shakil Khan wrote:

> Pardon me if I am writing this mail to the wrong group as I am too new to
> BSD and programming stuff. You can redirect me to right group without
> howling. By seeing some recent conversations(About the top status ;)) in
> this group it made me nervous to ask for a silly question like this, but it
> intrigued my mind too much so thought to delve into it keeping aside my
> fear.

If you're not sure which is the right list, then questions at .... is a good place
to start.  I'd hope no one would fear to ask what they needed to know in this
list, or it will have become pretty useless for its intended function.  

> I have a 64bit intel processor, Dual core machine and I have installed 64
> bit Linux as well as FreeBSD and thought of seeing the size of integer. On
> both the platform my integer is showing 4 bytes which is 32 bit. I thought
> integer are the most basic of the data types and governs the architecture as
> 32 bit or 64 bit, so why integer shows me 4bytes instead of 8. Does this
> means that even on 64 bit architecture we are limited to just 4GB of RAM.

Not at all.  'int' is just one of the basic types mandated by the various
C specifications over the years.  There are 4 basic integer types -- together
with their unsigned counterparts:

      char     unsigned char   --- usually 1 byte[*].
      short    unsigned short  --- usually 2 bytes
      int      unsigned        --- usually 4 bytes
      long     unsigned long   --- nowadays usually 8 bytes

Now, the C standards only require that the lengths of these types fulfil
the following condition:

     short <= int <= long

and there are some older CPU architectures where short and int are 2 bytes
and long is 4 bytes, but those are exceedingly rare nowadays and you won't
run into anything like that unless you work on embedded systems.

In general the sizes above are what pretty much all current CPUs since about
2000 have supported -- mostly because that's what the Intel x86 series used.
Before then there were quite a few CPUs where 'long' was 4 bytes, but those
sizes were otherwise about the same.

However, it's not the size of 'int' which is the important thing.  It's
the size of pointers.

The big deal with 64bit vs 32bit architectures is having registers of the
appropriate length that you can do 64bit integer operations natively.  This
particularly applies to memory address manipulation.  In principle you can
make a composite integer type of any length by stringing together as many 
shorter types as you want, but in practice that's too inefficient for most
purposes, especially anything as fundamental as memory addressing. So pointers
are generally no longer than the widest hardware registers on the CPU.  Hence
the length of pointers (memory addresses) is the fundamental distinction.
On a 32bit architecture, pointers are 4bytes long and can address up to 
4294967296 bytes (4GiB) of RAM. On a 64bit machines pointers are 8bytes long
and can address 18446744073709551616 bytes (16EiB) of RAM.  The Intel Core 2
Duo is capable of running in either 32 or 64 bit mode.  This C snippet will
show you how long some important types are on your system:

#include <stdio.h>

int
main (int argc, char *argv)
{
  printf( "int:\t%d\nlong:\t%d\npointer:\t%d\n", sizeof(int),
                  sizeof(long), sizeof(void*) );
}

	Cheers,

	Matthew

[*] char can sometimes be an unsigned quantity, in which case there
    should be a 'signed char' equivalent.

-- 
Dr Matthew J Seaman MA, D.Phil.                   7 Priory Courtyard
                                                  Flat 3
PGP: http://www.infracaninophile.co.uk/pgpkey     Ramsgate
                                                  Kent, CT11 9PW

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 259 bytes
Desc: OpenPGP digital signature
Url : http://lists.freebsd.org/pipermail/freebsd-questions/attachments/20090525/37e71996/signature.pgp


More information about the freebsd-questions mailing list