sizeof(int)

J

Jack Klein

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 you are using int when you shouldn't be.
 
J

Jack Klein

What do all of these references to x86 compatible and other desktop
processors have to do with anything? They comprise something like 15%
of the processors made each year.
usually an int is the size of the register used, on the 286 this was 16
bits, a long was 32.

Um, there are literally billions of 8-bit processors sold every year,
where sizeof int is 16, but the size of a register is 8.
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.

No, it's not. There are platforms where char has 16 bits. At least
one with 24 bits, and several where char has 32 bits.

You seem to think that C++ exists to standardize the various modes of
the x86 and desktop style processors. This is an incorrect notion.
 
D

David Lindauer

Shezan said:
Why can't a char be 16 bits?

it can. Most compilers for a given processor will declare it as 8 bits,
if the processor supports accesses on 8-bit entities. However please be
aware that there *are* processors that cannot access 8-bit entities, so
sizeof(char) is *not* guaranteed to be exactly 8 bits.

David
 
J

Jack Klein

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.

Interesting. Just today I was working with an architecture where char
has 16 bits, which is indeed the smallest addressable storage unit.

What possible gain from defining a char as 8 bits when a byte is 16
bits? You can only store one char per byte, unless you use bit fields
or bit-wise operators to merge and separate them.

And there are platforms where char and in fact all the integer types
are 32 bits. Period.
 
D

David Lindauer

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

on *most* processors this is true. As I mentioned some processors cannot
access data in byte-sized chunks. That is why the standard describes the
relationship of the various integral types in relative terms rather than
giving specific sizes.
 
V

Victor Bazarov

David Lindauer said:
on *most* processors this is true.

On most processors WHAT is true? And WTF does it have to do with
processors?
C++ Standard defines a byte as the smallest addressable unit of computer
memory and says that sizeof(char) is 1 byte. Where, on what processors, I
ask, would it NOT be true?
As I mentioned some processors cannot
access data in byte-sized chunks.

Here we go again. You're still falling into the trap that "byte" means 8
bits.
It does NOT. If your hardware allows a C++ implementation to exist on it
(or
*for* it), that means that _bytes_ *in C++ sense* cat be addressed. Period.
That is why the standard describes the
relationship of the various integral types in relative terms rather than
giving specific sizes.

Pardon? The Standard describes everything in terms of _bytes_ because some
processors cannot address "byte-sized chunks"? Am I reading this wrong or
have you made a few wrong turns yourself?

V
 
R

Ron Natalie

Jack said:
else if (sizeof(int) == 1)
{
// either a 16, 24, 32 bit platform (probably DSP)
// or a Cray???
}

Cray's I know about. The Cray really has two data sizes 24 bit
pointers and 64 everything else, that's hardware. The C compiler
in fact emulates 8 bit chars in software (by packing them into 64
containers) and sizeof() every other numeric type AND pointers are
8 (64 bits). The char pointer is made up by shoving the word pointer
into the low 24 bits and the byte within word offset in the high three bits.
 
R

Ron Natalie

Jack said:
Interesting. Just today I was working with an architecture where char
has 16 bits, which is indeed the smallest addressable storage unit.

What possible gain from defining a char as 8 bits when a byte is 16
bits? You can only store one char per byte, unless you use bit fields
or bit-wise operators to merge and separate them.

And there are platforms where char and in fact all the integer types
are 32 bits. Period.

I was imagining a case where the minimum addressable storage unit is
8 bits, but the basic character size is 16 bits (or larger). You
can't do this in C++.
 
C

Clark S. Cox III

on *most* processors this is true. As I mentioned some processors cannot
access data in byte-sized chunks.

*bzzzzzt*
ITYM: "some processors cannot access data in *octet*-sized chunks".
As far as C++ is concerned, every processor can access byte sized chunks.
 

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,575
Members
45,052
Latest member
KetoBeez

Latest Threads

Top