 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
andrew_nuss@yahoo.com Guest
|
Posted: Fri May 04, 2007 8:28 am Post subject: compiler assert for sizeof(char)==1 |
|
|
Hi,
Lets say I want to have a compile time assert that sizeof(char) == 1?
What about the idea that my compiler should not allow a zero length
array. Thus I tried declaring:
typedef char assertsizeofchar[1-sizeof(char)];
I discovered on Intel compiler that if sizeof(char) were 2, this cpp
file would not compile. And to my surprise, when sizeof(char) is 1,
the above does not give a compile error even though we have a zero
length array.
Thus my question is, why does the following not at least give a
warning on Intel compiler?
typedef char dummyarraytype[0];
Is there a compiler independent compile-time way to verify that
sizeof(char) is 1 on my platform? I'm looking for a solution nearly
as simple as my above attempt.
Thanks,
Andy
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Fri May 04, 2007 9:10 am Post subject: Re: compiler assert for sizeof(char)==1 |
|
|
On 4 Maj, 10:28, "andrew_n...@yahoo.com" <andrew_n...@yahoo.com>
wrote:
| Quote: | Lets say I want to have a compile time assert that sizeof(char) == 1?
|
sizeof(char) is 1 by definition (5.3.3/1), always.
--
Maciej Sobczak
http://www.msobczak.com/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
andrew_nuss@yahoo.com Guest
|
Posted: Fri May 04, 2007 10:41 pm Post subject: Re: compiler assert for sizeof(char)==1 |
|
|
| Quote: | Ahem, if that is just an example, it is a bad one, because sizeof (char) is
by definition 1. It might be 32 bits large, but its size remains 1 by
definition!
|
Yes, it was just an example, as well as confusion from other postings
on this group that seemed to imply that sizeof(char) was 4 on some
compilers. If what you say is true, then I wonder what would happen
if I had to do pointer arithmetic with char* pointers. Would I be
guaranteed that:
char* pch = ...;
pch += 3;
That the pch "array" is 1-byte packed and that the pointer advances by
3 bytes, or could on some platforms the pch "array" really be an array
of "ints" and the pointer advance would be by 12 bytes.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Jack Klein Guest
|
Posted: Sat May 05, 2007 1:21 pm Post subject: Re: compiler assert for sizeof(char)==1 |
|
|
On Fri, 4 May 2007 16:41:58 CST, "andrew_nuss (AT) yahoo (DOT) com"
<andrew_nuss (AT) yahoo (DOT) com> wrote in comp.lang.c++.moderated:
| Quote: | Ahem, if that is just an example, it is a bad one, because sizeof (char) is
by definition 1. It might be 32 bits large, but its size remains 1 by
definition!
Yes, it was just an example, as well as confusion from other postings
on this group that seemed to imply that sizeof(char) was 4 on some
compilers.
|
Perhaps you misinterpreted those other posts. They might have implied
that CHAR_BIT is greater than 8, and that is true on some processor
types, specifically DSPs. Chars with 16 and 32 bits are common on
these platform, and 24 bits is not unheard of.
Where your misunderstanding comes in is by supposing that if a char
has 16 bits it must have a size of two bytes. That is not true in C
and C++, where sizeof(char) is indeed always 1 by definition.
The confusion stems from the fact that sloppy usage has led to most
people (other than C and C++ programmers!) thinking that the term
"byte" is synonymous with 8 bits. In fact the proper term for a unit
of exactly 8 bits is "octet", but the incorrect usage of "byte" is too
widespread to ever be corrected.
| Quote: | If what you say is true, then I wonder what would happen
if I had to do pointer arithmetic with char* pointers. Would I be
guaranteed that:
char* pch = ...;
pch += 3;
That the pch "array" is 1-byte packed and that the pointer advances by
3 bytes, or could on some platforms the pch "array" really be an array
of "ints" and the pointer advance would be by 12 bytes.
|
Let's make it even simpler:
char pch[6] = { 0, 1, 2, 3, 4, 5 };
char cptr = pch;
Now pch is an array of chars, each of which is 1 byte in size, each of
those bytes containing 8 or more bits. The fact that each of those
chars contains 32 bits does not make them ints or longs, they are
still chars even if they happen to be the same size and have the same
number of bits as ints or longs.
If char has 32 bits, it is still has a size of one byte. If int has
32 bits on the same implementation, sizeof(int) is 1 on that
implementation.
And after:
cptr += 3;
cptr will point to the point to the fourth char in pch, which will be
a char with the value 3.
--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
andrew_nuss@yahoo.com Guest
|
Posted: Sat May 05, 2007 9:23 pm Post subject: Re: compiler assert for sizeof(char)==1 |
|
|
| Quote: |
Let's make it even simpler:
char pch[6] = { 0, 1, 2, 3, 4, 5 };
char cptr = pch;
Now pch is an array of chars, each of which is 1 byte in size, each of
those bytes containing 8 or more bits. The fact that each of those
chars contains 32 bits does not make them ints or longs, they are
still chars even if they happen to be the same size and have the same
number of bits as ints or longs.
If char has 32 bits, it is still has a size of one byte. If int has
32 bits on the same implementation, sizeof(int) is 1 on that
implementation.
And after:
cptr += 3;
cptr will point to the point to the fourth char in pch, which will be
a char with the value 3.
Yes, but what if I am doing my own heap management and creating blocks |
and doing pointer arithmetic on true byte values. Simple example:
enum { header_size = 16 };
char* p = static_cast<char*>(malloc(4 );
p += header_size; // then do other stuff and return p
The point is that I need to be confident that I can move the pointer
to the malloc'ed object exactly 16 bytes. Is this trouble on some
platforms? Is there a proper assert for platforms where that won't
work?
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sun May 06, 2007 8:19 pm Post subject: Re: compiler assert for sizeof(char)==1 |
|
|
andrew_nuss (AT) yahoo (DOT) com wrote:
| Quote: | Let's make it even simpler:
char pch[6] = { 0, 1, 2, 3, 4, 5 };
char cptr = pch;
Now pch is an array of chars, each of which is 1 byte in size, each of
those bytes containing 8 or more bits. The fact that each of those
chars contains 32 bits does not make them ints or longs, they are
still chars even if they happen to be the same size and have the same
number of bits as ints or longs.
If char has 32 bits, it is still has a size of one byte. If int has
32 bits on the same implementation, sizeof(int) is 1 on that
implementation.
And after:
cptr += 3;
cptr will point to the point to the fourth char in pch, which will be
a char with the value 3.
Yes, but what if I am doing my own heap management and creating blocks
and doing pointer arithmetic on true byte values. Simple example:
enum { header_size = 16 };
char* p = static_cast<char*>(malloc(4 );
p += header_size; // then do other stuff and return p
The point is that I need to be confident that I can move the pointer
to the malloc'ed object exactly 16 bytes. Is this trouble on some
platforms? Is there a proper assert for platforms where that won't
work?
Did you read the other responses to your post? What do you mean by a |
byte? Do you mean an octet? In which case on a system with a char that
has anything other than 8 bits the answer is that you cannot do this.
If you add n to p it will now point to the (n+1)th char. If header_size
is counted in octets you are out of luck on any system where a char has
more than 8-bits.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Alberto Ganesh Barbati Guest
|
Posted: Mon May 07, 2007 1:55 am Post subject: Re: compiler assert for sizeof(char)==1 |
|
|
News Admin ha scritto:
| Quote: | andrew_nuss (AT) yahoo (DOT) com wrote:
Thus my question is, why does the following not at least give a
warning on Intel compiler?
typedef char dummyarraytype[0];
Because the Standard does not require a warning. Your dummyarraytype is
a perfectly good type, just one that cannot be instantiated.
|
That's not true. The declaration itself is ill-formed in C++, according
to 8.3.4/1.
Ganesh
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Sebastian Redl Guest
|
Posted: Mon May 07, 2007 1:57 am Post subject: Re: compiler assert for sizeof(char)==1 |
|
|
On Sat, 5 May 2007, andrew_nuss (AT) yahoo (DOT) com wrote:
| Quote: | Yes, but what if I am doing my own heap management and creating blocks
and doing pointer arithmetic on true byte values.
|
What's a "true byte value"? Do you mean octets?
| Quote: | Simple example:
enum { header_size = 16 };
char* p = static_cast<char*>(malloc(4 );
p += header_size; // then do other stuff and return p
The point is that I need to be confident that I can move the pointer
to the malloc'ed object exactly 16 bytes. Is this trouble on some
platforms? Is there a proper assert for platforms where that won't
work?
|
If you want to ensure that a char is equivalent to an octet, the condition
you need to test is CHAR_BITS == 8. And this is trouble, for example on
some embedded platforms.
Of course, as far as C++ is concerned, you will move the pointer exactly
16 bytes. It's just that C++ might disagree with you about what a "byte"
is.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| 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
|
|