Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Compiler error for "wrong" sized type

Reply
Thread Tools

Compiler error for "wrong" sized type

 
 
Eric Sosman
Guest
Posts: n/a
 
      05-12-2006
David White wrote:
> Richard Heathfield wrote:
>
>>Well, I don't know about "better", but I like this:
>>
>>char DetectWrongSize[(sizeof(struct my_struct) == correct_size) * 2 -
>>1];
>>
>>If the struct is the wrong size, this will yield a negatively sized
>>array, which is illegal.

>
>
> Thanks. That's an improvement. Unfortunately, our Hi-Tech compiler doesn't pick it up
> because it treats an array size of -1 as unsigned!
>
> DW
>
> P.S. A colleague is terribly disappointed to learn that an array cannot have a negative
> size. He thinks it should be allowed, with the valid index range extending from size+1 to
> zero inclusive (e.g., -9 to 0 for a size of -10). I asked him what he would expect for a
> sizeof of such an array, but he can't decide whether it should be positive or negative.


Wow! You're a colleague of Paul Dirac?

Hmmm: Dirac's ideas could revolutionize memory management in
C-- (or would it be --C?). If "no memory at all" spontaneously
split into a chunk of allocated memory and a corresponding chunk
of unallocated anti-memory, a program could store data in the
allocated area without needing to call malloc(). Of course, one
would need to be careful: pointer aliasing could cause the memory
and anti-memory to come together and undergo mutual annihilation,
with the resulting, er, Gamma ray constituting an implementation-
defined signal. The task of keeping the allocation separated
from the anti-allocation is probably best left to Maxwell's Nasal
Demon.

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid


--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
 
 
 
Kenneth Brody
Guest
Posts: n/a
 
      05-12-2006
David White wrote:
[...]
> P.S. A colleague is terribly disappointed to learn that an array cannot
> have a negative size. He thinks it should be allowed, with the valid
> index range extending from size+1 to zero inclusive (e.g., -9 to 0 for
> a size of -10). I asked him what he would expect for a sizeof of such
> an array, but he can't decide whether it should be positive or negative.


http://astronomy.swin.edu.au/~pbourk...ry/minus1.html

--
+-------------------------+--------------------+-----------------------------+
| Kenneth J. Brody | www.hvcomputer.com | |
| kenbrody/at\spamcop.net | www.fptech.com | #include <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------------+
Don't e-mail me at: <(E-Mail Removed)>

 
Reply With Quote
 
 
 
 
tedu
Guest
Posts: n/a
 
      05-12-2006
David White wrote:
> Richard Heathfield wrote:
> > Well, I don't know about "better", but I like this:
> >
> > char DetectWrongSize[(sizeof(struct my_struct) == correct_size) * 2 -
> > 1];
> >
> > If the struct is the wrong size, this will yield a negatively sized
> > array, which is illegal.

>
> Thanks. That's an improvement. Unfortunately, our Hi-Tech compiler doesn't pick it up
> because it treats an array size of -1 as unsigned!


does it allow multiple case statements with the same value?

switch (0) {
case sizeof(struct my_struct) == correct_size:
case 0:
}

 
Reply With Quote
 
S.Tobias
Guest
Posts: n/a
 
      05-12-2006
tedu <(E-Mail Removed)> wrote:
> David White wrote:
>> Richard Heathfield wrote:
>> > Well, I don't know about "better", but I like this:
>> >
>> > char DetectWrongSize[(sizeof(struct my_struct) == correct_size) * 2 -
>> > 1];
>> >
>> > If the struct is the wrong size, this will yield a negatively sized
>> > array, which is illegal.

>>
>> Thanks. That's an improvement. Unfortunately, our Hi-Tech compiler doesn't pick it up
>> because it treats an array size of -1 as unsigned!

>
> does it allow multiple case statements with the same value?
>
> switch (0) {
> case sizeof(struct my_struct) == correct_size:
> case 0:
> }
>

Nit: a label reqires a following statement; make it:
switch (0) {
case sizeof(struct my_struct) == correct_size: ;
case 0: ;
}

(Otherwise it's a nice trick, I liked it.)

--
Stan Tobias
mailx `echo (E-Mail Removed)LID | sed s/[[:upper:]]//g`
 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      05-13-2006
S.Tobias wrote:
> tedu <(E-Mail Removed)> wrote:
> > David White wrote:
> >> Richard Heathfield wrote:
> >> > Well, I don't know about "better", but I like this:
> >> >
> >> > char DetectWrongSize[(sizeof(struct my_struct) == correct_size) * 2 -
> >> > 1];
> >> >
> >> > If the struct is the wrong size, this will yield a negatively sized
> >> > array, which is illegal.
> >>
> >> Thanks. That's an improvement. Unfortunately, our Hi-Tech compiler doesn't pick it up
> >> because it treats an array size of -1 as unsigned!

> >
> > does it allow multiple case statements with the same value?
> >
> > switch (0) {
> > case sizeof(struct my_struct) == correct_size:
> > case 0:
> > }
> >

> Nit: a label reqires a following statement; make it:
> switch (0) {
> case sizeof(struct my_struct) == correct_size: ;
> case 0: ;
> }
>
> (Otherwise it's a nice trick, I liked it.)


"case 0: ;" is a statement, so the first semicolon isn't necessary. The
second indeed is though.

 
Reply With Quote
 
David White
Guest
Posts: n/a
 
      05-13-2006
"tedu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> does it allow multiple case statements with the same value?


I don't know. I'll find out next week.

> switch (0) {
> case sizeof(struct my_struct) == correct_size:
> case 0:
> }


Good idea. Incidentally, I had a problem with this rule once. I allowed two
different characters in encoded text to have the same meaning, e.g.,
#define TOKEN_CHAR '#'
#define ALT_TOKEN_CHAR 't'

Since they have the same meaning, the switch that processed them looked like
this:
switch(ch)
{
....
case TOKEN_CHAR:
case ALT_TOKEN_CHAR:
/* process */
break;
....
}

But in some cases I didn't want to allow both characters, so I did this:
#define TOKEN_CHAR '#'
#define ALT_TOKEN_CHAR '#'

The switch then wouldn't compile, but it could have, since there's no clash.

DW



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiler error dependent name is not a type prefix with 'typename' toindicate a type Sanil C++ 2 12-14-2007 09:14 AM
Oddly sized sized password textbox thechaosengine ASP .Net 1 09-15-2005 07:34 PM
Compiler Error Message: The compiler failed with error code -1073741819 Ram ASP .Net 0 09-13-2005 09:52 AM
compiler error: argument of type "VALUE *" is incompatible with parameter of type "VALUE" me2faster@excite.com Ruby 1 05-05-2005 11:23 PM
Compiler Error Message: The compiler failed with error code 128. Yan ASP .Net 0 07-21-2003 10:49 PM



Advertisments