Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > sizeof(int)

Reply
Thread Tools

sizeof(int)

 
 
Richard Cavell
Guest
Posts: n/a
 
      02-26-2005
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?
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-26-2005
"Richard Cavell" <(E-Mail Removed)> wrote...
> 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


 
Reply With Quote
 
 
 
 
Richard Cavell
Guest
Posts: n/a
 
      02-27-2005
On 27/2/05 3:03 AM, Victor Bazarov wrote:

> 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.
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-27-2005
"Richard Cavell" <(E-Mail Removed)> wrote...
> On 27/2/05 3:03 AM, Victor Bazarov wrote:
>
>> 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.


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


 
Reply With Quote
 
DHOLLINGSWORTH2
Guest
Posts: n/a
 
      02-27-2005

"Richard Cavell" <(E-Mail Removed)> wrote in message
news:cvpg29$3ai$(E-Mail Removed)...
> 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.


 
Reply With Quote
 
Shezan Baig
Guest
Posts: n/a
 
      02-27-2005
DHOLLINGSWORTH2 wrote:
> A char is always 8 bits.



Why can't a char be 16 bits?

 
Reply With Quote
 
Richard Cavell
Guest
Posts: n/a
 
      02-27-2005
On 27/2/05 2:28 PM, Victor Bazarov wrote:
> "Richard Cavell" <(E-Mail Removed)> wrote...
>
>>On 27/2/05 3:03 AM, Victor Bazarov wrote:
>>
>>
>>>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.

>
>
> 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.
 
Reply With Quote
 
Jonathan Turkanis
Guest
Posts: n/a
 
      02-27-2005
Richard Cavell wrote:
> On 27/2/05 2:28 PM, Victor Bazarov wrote:
>> "Richard Cavell" <(E-Mail Removed)> wrote...
>>> On 27/2/05 3:03 AM, Victor Bazarov wrote:


>>>> 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.

>>
>>
>> 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.


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



 
Reply With Quote
 
Shezan Baig
Guest
Posts: n/a
 
      02-27-2005
Richard Cavell wrote:
> On 27/2/05 2:28 PM, Victor Bazarov wrote:
> > 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.



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.

 
Reply With Quote
 
Phil Staite
Guest
Posts: n/a
 
      02-27-2005
Richard Cavell wrote:
> 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;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments