sizeof(int)

R

Richard Cavell

Hi,

Is there some kind of canonical list, or would someone like to give a
brief rundown, as to:

sizeof(int)
sizeof(long int)
sizeof(long long)
etc

or perhaps even the vector types, for current hardware (Pentium
4/Athlon/G4/G5/Playstation 3/etc) used with current compilers? Or
perhaps a list of what size int_t you can define and expect the
processor to handle natively?
 
V

Victor Bazarov

Richard Cavell said:
Is there some kind of canonical list, or would someone like to give a
brief rundown, as to:

sizeof(int)
sizeof(long int)
sizeof(long long)
etc

sizeof(int) is the size of an int object expressed in bytes.
sizeof(long int) is the size of a long int object.
sizeof(long long) is a syntax error since C++ has no "long long" type.

I am not sure what "canonical list" you're talking about.
or perhaps even the vector types, for current hardware (Pentium
4/Athlon/G4/G5/Playstation 3/etc) used with current compilers? Or perhaps
a list of what size int_t you can define and expect the processor to
handle natively?

If you need something compiler-specific, please ask in a newsgroup
dedicated to that compiler. C++ discussed here is compiler-independent.

If you need something hardware-specific, please ask in a newsgroup
dedicated to that hardware. C++ is a hardware-independent language.

V
 
R

Richard Cavell

C++ is a hardware-independent language.

No, it ain't.

I have to write my program differently depending on what these sizeofs
are. So it's not hardware-independent at all.
 
V

Victor Bazarov

Richard Cavell said:
No, it ain't.

I have to write my program differently depending on what these sizeofs
are. So it's not hardware-independent at all.

It's not the language, silly. It's your algorithm, it's what you want
to do, that makes it hardware-dependent.
 
D

DHOLLINGSWORTH2

Richard Cavell said:
Hi,

Is there some kind of canonical list, or would someone like to give a
brief rundown, as to:

sizeof(int)
sizeof(long int)
sizeof(long long)
etc

or perhaps even the vector types, for current hardware (Pentium
4/Athlon/G4/G5/Playstation 3/etc) used with current compilers? Or perhaps
a list of what size int_t you can define and expect the processor to
handle natively?

usually an int is the size of the register used, on the 286 this was 16
bits, a long was 32.
on 386 protected, 486, int is 32 bits, and long int, is 32 bits as well,
Double Word is 64 bits.
A char is always 8 bits. The ratio of Char size to int size will tell you
the hardware type, ie the register width.
 
R

Richard Cavell

It's not the language, silly. It's your algorithm, it's what you want
to do, that makes it hardware-dependent.

That makes no sense at all. If C++ ran inside a virtual machine then it
wouldn't matter to me whether my processor could do 16-bit integers at a
time, or 32, or whatever. But my program actually does different things
based on sizeof(int) whether I want it to or not.
 
J

Jonathan Turkanis

Richard said:
That makes no sense at all. If C++ ran inside a virtual machine then
it wouldn't matter to me whether my processor could do 16-bit
integers at a time, or 32, or whatever. But my program actually does
different things based on sizeof(int) whether I want it to or not.

The following is a hardware-dependent program, in your sense:

#include <iostream>

int main()
{
if (sizeof(int) == 19)
std::cout << "hello 19 world\n";
else
std::cout << "hello non-19 world\n";
}

Is this what bothers you? You don't want your program to be able to detect
anything that varies from system to system?

It would be more worthwile to ask about ways to write programs which work on a
variety of platforms. There are lots of ways to do it.

Jonathan
 
S

Shezan Baig

Richard said:
That makes no sense at all. If C++ ran inside a virtual machine then it
wouldn't matter to me whether my processor could do 16-bit integers at a
time, or 32, or whatever. But my program actually does different things
based on sizeof(int) whether I want it to or not.


What exactly is your program trying to do? A portable program should
not need to depend on sizeof(int) being a certain size, unless you're
doing some form of externalization.
 
P

Phil Staite

Richard said:
On 27/2/05 2:28 PM, Victor Bazarov wrote:
That makes no sense at all. If C++ ran inside a virtual machine then it
wouldn't matter to me whether my processor could do 16-bit integers at a
time, or 32, or whatever. But my program actually does different things
based on sizeof(int) whether I want it to or not.


Then I suggest you have sizeof(int) in your program, such as:

if( sizeof(int) == 2 )
{
// oh schnitt, a 16 bit machine!!!
}
else if( sizeof(int) == 4 )
{
// ahh, mainstream life...
}
else if( sizeof(int) == 8 )
{
// where the {expletive} am I now?
}

If you're curious on your machine, just dump them:

#include<iostream>

int main( int argc, char* argv[] )
{
std::cout << "int " << sizeof(int) << std::endl;
std::cout << "long " << sizeof(long) << std::endl;
return 0;
}
 
R

Ron Natalie

Shezan said:
Why can't a char be 16 bits?
It can be, but it is unlikely. Unfortunately one of the
great defects in C++ is that char has a double role: that
of the basic character AND that of the smallest addressable
storage unit. Ideally, bytes and characters should be dissassociated.
 
V

Victor Bazarov

Ron Natalie said:
It can be, but it is unlikely. Unfortunately one of the
great defects in C++ is that char has a double role: that
of the basic character AND that of the smallest addressable
storage unit. Ideally, bytes and characters should be dissassociated.

I think you misunderstand the role of a char WRT basic character set.
Nothing prevents a 32-bit char from containing an element from the basic
character set. The requirement is only that the char type to be large
enough for that. There is no requirement that it has to be no larger
than necessary to contain the basic characters. There is no defect in
the language, only in your understanding of it.

V
 
C

Carl Muller

DHOLLINGSWORTH2 said:
news:[email protected]... [snip]
A char is always 8 bits. The ratio of Char size to int size will tell you
the hardware type, ie the register width.

Not always. A char is at least 8 bits, but on the hardware I program
it is 16 bits (as is int, so sizeof(int)==1). There are other chip
manufacturers in the world than Intel you know.
 
J

Jerry Coffin

Richard Cavell wrote:

[ ... ]
That makes no sense at all. If C++ ran inside a virtual machine then it
wouldn't matter to me whether my processor could do 16-bit integers at a
time, or 32, or whatever. But my program actually does different things
based on sizeof(int) whether I want it to or not.

If you need something you're certain is at least 32 bits, use long. If
you need something you need to be certain is only 16 bits, use a
bitfield.

In the end, you're right: running in a VM could provide greater
isolation from the hardware, and with it greater portability. OTOH, you
need to use SOME language to implement the VM, and the operating system
it runs on, and he device drivers IT uses to talk to the hardware --
and right now, the languages of choice for all those things UNDER the
VM are C and C++. If you insist on them running inside of the VM, you
have to come up with something else to implement all the other things
under the VM.

Based on experience, I'd put the chances at roughly 99% that if you
attempted to create a language to replace C and C++ in those roles,
you'd end up with something that did NOT provide as good of a tradeoff
between portability and access to the hardware as C and C++ provide
right now. Of all the gazillions of attempts at such programming
languages, there's only about _one_ that's really competitive with C or
C++ for these tasks.
 
I

Ioannis Vranos

Richard said:
That makes no sense at all. If C++ ran inside a virtual machine then it
wouldn't matter to me whether my processor could do 16-bit integers at a
time, or 32, or whatever. But my program actually does different things
based on sizeof(int) whether I want it to or not.


I do not think the VM makes the difference you are thinking it does. A
VM is a machine as its name implies with its own assembly language.


For example, .NET is a CLI VM and here is a book about .NET's (CLI) VM
assembly language:


http://www.amazon.com/exec/obidos/t...f=sr_1_7/102-2992266-3703320?v=glance&s=books



CLI assembly language is defined in the freely available CLI standard,
so you can view in the specification how it looks like:


http://www.ecma-international.org/publications/standards/Ecma-335.htm
 
D

DHOLLINGSWORTH2

Ron Natalie said:
It can be, but it is unlikely. Unfortunately one of the
great defects in C++ is that char has a double role: that
of the basic character AND that of the smallest addressable
storage unit. Ideally, bytes and characters should be dissassociated.

You are correct, I should have said "Byte".
 
V

Victor Bazarov

DHOLLINGSWORTH2 said:
You are correct, I should have said "Byte".

In C++ terms 'char' and 'byte' are interchangeable. You should have
said "octet", maybe.

V
 
R

Ron Natalie

I think you misunderstand the role of a char WRT basic character set.

I understand it perfoectly.
Nothing prevents a 32-bit char from containing an element from the basic
character set.

And who cares? The issue is not the maximum size. The issue is that if
you choose to use a larger character size (imagine UNICODE as the basic
character set), you can't make the char 32 bits if you want to still be
able to address 8 bit pieces of memory somewhere.
The requirement is only that the char type to be large
enough for that. There is no requirement that it has to be no larger
than necessary to contain the basic characters. There is no defect in
the language, only in your understanding of it.

My understnading is fine, your understanding of the limitations is what
is deficient.
 
J

Jack Klein

Then I suggest you have sizeof(int) in your program, such as:

if( sizeof(int) == 2 )
{
// oh schnitt, a 16 bit machine!!!
}
else if( sizeof(int) == 4 )
{
// ahh, mainstream life...
}
else if( sizeof(int) == 8 )
{
// where the {expletive} am I now?
}

else if (sizeof(int) == 1)
{
// either a 16, 24, 32 bit platform (probably DSP)
// or a Cray???
}
If you're curious on your machine, just dump them:

#include<iostream>

int main( int argc, char* argv[] )
{
std::cout << "int " << sizeof(int) << std::endl;
std::cout << "long " << sizeof(long) << std::endl;
return 0;
}
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top