sizeof(int)

A

Angus

Hello

Using MS compiler on Windows XP platform if I run this code:

int mysize = sizeof(int);
int mysize2 = sizeof(long);

both report 4. I was under the impression that an int on windows was
only 2 bytes. Or is it hardware dependent rather than operating
system dependent?
 
B

Balog Pal

Angus said:
Using MS compiler on Windows XP platform if I run this code:

int mysize = sizeof(int);
int mysize2 = sizeof(long);

both report 4. I was under the impression that an int on windows was
only 2 bytes. Or is it hardware dependent rather than operating
system dependent?

It is up to the compiler. (and stuff it must specify in dox).

MS compilers use 16 bit ints for Win16 platforms (i.e. Windows 3.11).
For WIN32 platform (i.e Win95, 98, ME, NT 4.0, 2000, XP, Vista) int is 32
bits.
 
S

Salt_Peter

Hello

Using MS compiler on Windows XP platform if I run this code:

int mysize = sizeof(int);
int mysize2 = sizeof(long);

both report 4. I was under the impression that an int on windows was
only 2 bytes. Or is it hardware dependent rather than operating
system dependent?

Its platform dependant. On a 32 bit system an int will typically be 32
bits. On a 64 bit system it'll be 64 bits and so on.

Your code should be transparent to the primitive integer's size
anyways. Consider a class:

class A
{
char c;
int n;
};

On your system, an object of A will typically not have a size of 5. It
would probably have a size of 8 due to alignment and padding (think
efficiency). On a 64 bit platform, 16. Again, your code shouldn't care
which platform is involved.
 
F

Fred

Its platform dependant. On a 32 bit system an int will typically be 32
bits. On a 64 bit system it'll be 64 bits and so on.

Not true. Many 64-bit platforms (such as HP) have 32-bit ints (but 64-
bit longs).
 
J

Juha Nieminen

Fred said:
Not true. Many 64-bit platforms (such as HP) have 32-bit ints (but 64-
bit longs).

Moreover, even if you are compiling a 64-bit binary using MSVC++, long
will be 32-bit (AFAIK). The standard perfectly allows it to do that.
 
I

Ian Collins

Fred said:
Not true. Many 64-bit platforms (such as HP) have 32-bit ints (but 64-
bit longs).

Most 64-bit platforms use either LLP64 (windows) which has 32 int and
long with 64 bit long long or LP64 (anything Unix like) which has 32 int
with 64 bit long and long long.
 
B

Bo Persson

Ian said:
Most 64-bit platforms use either LLP64 (windows) which has 32 int
and long with 64 bit long long or LP64 (anything Unix like) which
has 32 int with 64 bit long and long long.

Yes, they are all the "natural" size on each platform. For the
different definitions of "natural".


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top