 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Richard Cavell Guest
|
Posted: Sat Feb 26, 2005 9:45 am Post subject: sizeof(int) |
|
|
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
|
Posted: Sat Feb 26, 2005 4:03 pm Post subject: Re: sizeof(int) |
|
|
"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
|
Posted: Sun Feb 27, 2005 1:04 am Post subject: Re: sizeof(int) |
|
|
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
|
Posted: Sun Feb 27, 2005 3:28 am Post subject: Re: sizeof(int) |
|
|
"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
|
Posted: Sun Feb 27, 2005 4:55 am Post subject: Re: sizeof(int) |
|
|
"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
|
Posted: Sun Feb 27, 2005 5:00 am Post subject: Re: sizeof(int) |
|
|
DHOLLINGSWORTH2 wrote:
| Quote: | A char is always 8 bits.
|
Why can't a char be 16 bits?
|
|
| Back to top |
|
 |
Richard Cavell Guest
|
Posted: Sun Feb 27, 2005 5:13 am Post subject: Re: sizeof(int) |
|
|
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
|
Posted: Sun Feb 27, 2005 5:27 am Post subject: Re: sizeof(int) |
|
|
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
|
Posted: Sun Feb 27, 2005 5:28 am Post subject: Re: sizeof(int) |
|
|
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
|
Posted: Sun Feb 27, 2005 6:20 am Post subject: Re: sizeof(int) |
|
|
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
|
Posted: Sun Feb 27, 2005 2:53 pm Post subject: Re: sizeof(int) |
|
|
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
|
Posted: Sun Feb 27, 2005 3:39 pm Post subject: Re: sizeof(int) |
|
|
"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
|
Posted: Sun Feb 27, 2005 4:41 pm Post subject: Re: sizeof(int) |
|
|
"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
|
Posted: Sun Feb 27, 2005 6:09 pm Post subject: Re: sizeof(int) |
|
|
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
|
|
| Back to top |
|
 |
|
|
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
|
|