Topic: Size of chars


Author: whatiscpp@yahoo.com (John the newbie)
Date: Tue, 2 Apr 2002 00:28:35 GMT
Raw View
Hi everybody,

I have four simple questions concerning the size of chars.

1) Why is it required that sizeof(char) == 1, while sizeof(bool) is
unspecified?

2) Is it correct to say that char is the smallest type that an
implementation can represent?

3) Is it correct to say that a byte has always CHAR_BIT bits?

4) Does it make sense something like typedef unsigned char byte?

Thanks a lot for your insights :)

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Tue, 2 Apr 2002 02:41:40 GMT
Raw View
John the newbie wrote:
>
> Hi everybody,
>
> I have four simple questions concerning the size of chars.
>
> 1) Why is it required that sizeof(char) == 1, while sizeof(bool) is
> unspecified?

sizeof(char)==1 is a key part of the definition of what char means. It's
the smallest addressable unit of memory. Originally, 'char' was also
meant to be the type that represents characters, and that's still one of
it's primary meanings. However, it's arguably the case that the true
character type is now wchar_t. I personally have never had any need to
use wchar_t, in 22 years of work as a C programmer; but outside the US,
it's a pretty common need.

sizeof(bool), on the other hand, is implementation-defined, to allow an
implemention freedom to use, for example, an 'int' to store a bool
value. On most platforms, using an 'int' would be much faster than
storing it in a smaller amount of space. If it's really important to you
that bits be stored in the minimum amount of space, you should look into
bit-fields, or std::bitset, or std::vector<bool>. Single bits can't be
stored efficiently, they have to be packed together with other sub-byte
sized fields is space is an issue.

> 2) Is it correct to say that char is the smallest type that an
> implementation can represent?

Yes, no type can take up less space than a char. A bit-field can take up
less space, but there's no such thing as a bit-field type. A 'bool' only
actually uses 1 bit, but it must waste enough space to occupy at least
one entire byte, and possibly more.

> 3) Is it correct to say that a byte has always CHAR_BIT bits?

Yes.

> 4) Does it make sense something like typedef unsigned char byte?

Yes.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Gennaro Prota <gennaro_prota@yahoo.com>
Date: Tue, 2 Apr 2002 18:02:07 GMT
Raw View
On Tue,  2 Apr 2002 00:28:35 GMT, whatiscpp@yahoo.com (John the
newbie) wrote:

>2) Is it correct to say that char is the smallest type that an
>implementation can represent?

With your formulation of the question, the answer is no, since other
types may have sizeof =3D=3D 1 (i.e. there isn't "the smallest" type).

But I think you were asking if is there a type that can have a zero
size. IMHO the short answer is: there's no complete type for which
sizeof yields 0 (see the postscript however); but note that I'm
talking about types, not objects.

Consider infact, that there's a difference between what sizeof yields
whan applied to a given type T (what is called the "size of the
type"), and how many bytes do instances of T occupy (different
instances of T can even occupy different numbers of bytes, depending,
for example, on whether they're complete objects or sub-objects).

For instance, given:

class A {};

class B : public A {
A m1;
A m2;
};

A a;
B b;

it's always true that

sizeof (A) > 0;
sizeof (a) > 0;   // =A71.8 p5 and =A79p3

Anyhow, in each instance of B, the A base sub-object as well as m1 or
m2 are allowed to occupy no storage (a kind of optimization that can
be important in many cases)

But be careful: m1 and m2 are not allowed to have the same address
(indeed it must be &m2 > &m1).

Quite confusing, don't you think?


>3) Is it correct to say that a byte has always CHAR_BIT bits?

Yes, provided that you intend a byte as "the unit of storage on this
implementation" (as it is actually intended in the C++ standard)

>4) Does it make sense something like typedef unsigned char byte?

As said above yes (see also =A73.9p4)


P.S.: giving the actual meaning of sizeof, I'm quite sure that there
was no intent to allow sizeof(built-in type) to be 0 (that's why I
gave the answer above), anyhow can you point me where it's stated in
the standard? 5.3.3 talks about "object representation", which is in
turn defined (in 3.9p4) through sizeof, so I don't see how to resolve
the definition.


Genny.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Ron Natalie <ron@sensor.com>
Date: Tue, 2 Apr 2002 22:08:53 GMT
Raw View

Gennaro Prota wrote:
>
> On Tue,  2 Apr 2002 00:28:35 GMT, whatiscpp@yahoo.com (John the
> newbie) wrote:
>
> >2) Is it correct to say that char is the smallest type that an
> >implementation can represent?
>
> With your formulation of the question, the answer is no, since other
> types may have sizeof == 1 (i.e. there isn't "the smallest" type).

How about, no type can have a smaller size than char.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]