Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 25 May 2009 13:00:20 +0100
From:      Matthew Seaman <m.seaman@infracaninophile.co.uk>
To:        Shakil Khan <korikov.zk@gmail.com>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: why integer size says 4 bytes on 64 bit processor
Message-ID:  <4A1A8854.2010306@infracaninophile.co.uk>
In-Reply-To: <717ed9590905250355o3f3b836bm9e2e5abee03d50b2@mail.gmail.com>
References:  <717ed9590905250355o3f3b836bm9e2e5abee03d50b2@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
This is an OpenPGP/MIME signed message (RFC 2440 and 3156)
--------------enig07E367EEFA40A5CA0A3D5156
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: quoted-printable

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 ;)) i=
n
> this group it made me nervous to ask for a silly question like this, bu=
t 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@.... 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. =20

> 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 thou=
ght
> integer are the most basic of the data types and governs the architectu=
re as
> 32 bit or 64 bit, so why integer shows me 4bytes instead of 8. Does thi=
s
> means that even on 64 bit architecture we are limited to just 4GB of RA=
M.

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 -- toge=
ther
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 <=3D int <=3D long

and there are some older CPU architectures where short and int are 2 byte=
s
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 ab=
out
2000 have supported -- mostly because that's what the Intel x86 series us=
ed.
Before then there were quite a few CPUs where 'long' was 4 bytes, but tho=
se
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.  Th=
is
particularly applies to memory address manipulation.  In principle you ca=
n
make a composite integer type of any length by stringing together as many=
=20
shorter types as you want, but in practice that's too inefficient for mos=
t
purposes, especially anything as fundamental as memory addressing. So poi=
nters
are generally no longer than the widest hardware registers on the CPU.  H=
ence
the length of pointers (memory addresses) is the fundamental distinction.=

On a 32bit architecture, pointers are 4bytes long and can address up to=20
4294967296 bytes (4GiB) of RAM. On a 64bit machines pointers are 8bytes l=
ong
and can address 18446744073709551616 bytes (16EiB) of RAM.  The Intel Cor=
e 2
Duo is capable of running in either 32 or 64 bit mode.  This C snippet wi=
ll
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.

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


--------------enig07E367EEFA40A5CA0A3D5156
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.11 (FreeBSD)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEAREIAAYFAkoaiFwACgkQ8Mjk52CukIyBKwCglEg4ZT44pgxgA+Bbd1nk7mAO
xsoAnjaTbhUKUMpcZn2B1h3bae8mLftF
=hUHQ
-----END PGP SIGNATURE-----

--------------enig07E367EEFA40A5CA0A3D5156--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4A1A8854.2010306>