C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

sizeof(int)
Goto page 1, 2  Next
 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Richard Cavell
Guest





PostPosted: Sat Feb 26, 2005 9:45 am    Post subject: sizeof(int) Reply with quote



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?
Back to top
Victor Bazarov
Guest





PostPosted: Sat Feb 26, 2005 4:03 pm    Post subject: Re: sizeof(int) Reply with quote



"Richard Cavell" <richardcavell (AT) mail (DOT) com> wrote...
Quote:
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.

Quote:
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



Back to top
Richard Cavell
Guest





PostPosted: Sun Feb 27, 2005 1:04 am    Post subject: Re: sizeof(int) Reply with quote



On 27/2/05 3:03 AM, Victor Bazarov wrote:

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

Back to top
Victor Bazarov
Guest





PostPosted: Sun Feb 27, 2005 3:28 am    Post subject: Re: sizeof(int) Reply with quote

"Richard Cavell" <richardcavell (AT) mail (DOT) com> wrote...
Quote:
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.



Back to top
DHOLLINGSWORTH2
Guest





PostPosted: Sun Feb 27, 2005 4:55 am    Post subject: Re: sizeof(int) Reply with quote


"Richard Cavell" <richardcavell (AT) mail (DOT) com> wrote

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



Back to top
Shezan Baig
Guest





PostPosted: Sun Feb 27, 2005 5:00 am    Post subject: Re: sizeof(int) Reply with quote

DHOLLINGSWORTH2 wrote:
Quote:
A char is always 8 bits.


Why can't a char be 16 bits?


Back to top
Richard Cavell
Guest





PostPosted: Sun Feb 27, 2005 5:13 am    Post subject: Re: sizeof(int) Reply with quote

On 27/2/05 2:28 PM, Victor Bazarov wrote:
Quote:
"Richard Cavell" <richardcavell (AT) mail (DOT) com> 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.

Back to top
Jonathan Turkanis
Guest





PostPosted: Sun Feb 27, 2005 5:27 am    Post subject: Re: sizeof(int) Reply with quote

Richard Cavell wrote:
Quote:
On 27/2/05 2:28 PM, Victor Bazarov wrote:
"Richard Cavell" <richardcavell (AT) mail (DOT) com> 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 worldn";
else
std::cout << "hello non-19 worldn";
}

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




Back to top
Shezan Baig
Guest





PostPosted: Sun Feb 27, 2005 5:28 am    Post subject: Re: sizeof(int) Reply with quote

Richard Cavell wrote:
Quote:
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.


Back to top
Phil Staite
Guest





PostPosted: Sun Feb 27, 2005 6:20 am    Post subject: Re: sizeof(int) Reply with quote

Richard Cavell wrote:
Quote:
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;
}


Back to top
Ron Natalie
Guest





PostPosted: Sun Feb 27, 2005 2:53 pm    Post subject: Re: sizeof(int) Reply with quote

Shezan Baig wrote:
Quote:
DHOLLINGSWORTH2 wrote:

A char is always 8 bits.



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.

Back to top
Victor Bazarov
Guest





PostPosted: Sun Feb 27, 2005 3:39 pm    Post subject: Re: sizeof(int) Reply with quote

"Ron Natalie" <ron (AT) sensor (DOT) com> wrote...
Quote:
Shezan Baig wrote:
DHOLLINGSWORTH2 wrote:

A char is always 8 bits.



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.

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



Back to top
Carl Muller
Guest





PostPosted: Sun Feb 27, 2005 4:41 pm    Post subject: Re: sizeof(int) Reply with quote

"DHOLLINGSWORTH2" <DHOLLINGSWORTH2 (AT) cox (DOT) net> wrote

Quote:
"Richard Cavell" <richardcavell (AT) mail (DOT) com> wrote in message
news:cvpg29$3ai$1 (AT) nnrp (DOT) waia.asn.au...
[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.

Back to top
Jerry Coffin
Guest





PostPosted: Sun Feb 27, 2005 6:09 pm    Post subject: Re: sizeof(int) Reply with quote

Richard Cavell wrote:

[ ... ]

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

--
Later,
Jerry.

The universe is a figment of its own imagination.


Back to top
Ioannis Vranos
Guest





PostPosted: Sun Feb 27, 2005 6:21 pm    Post subject: Re: sizeof(int) Reply with quote

Richard Cavell wrote:

Quote:
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/tg/detail/-/0735615470/qid=1091414100/sr=1-7/ref=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



--
Ioannis Vranos

http://www23.brinkster.com/noicys

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.