Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
June 08, 2001 undetected negative or zero size. | ||||
---|---|---|---|---|
| ||||
Hi, The compiler compiled the following code. int main() { int array1[0]; int array2[-1]; return 0; } I believe the compiler should issue the errors instead. |
June 08, 2001 Re: undetected negative or zero size. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suradet Jitprapaikulsarn | <g>
I can not believe people actually try this!!!
When you buy a car do you also run it into the crash barrier to
see how it looks after wards?? <g>
Suradet Jitprapaikulsarn wrote:
> Hi,
>
> The compiler compiled the following code.
>
> int main()
> {
> int array1[0];
> int array2[-1];
>
> return 0;
> }
>
> I believe the compiler should issue the errors instead.
|
June 08, 2001 Re: undetected negative or zero size. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suradet Jitprapaikulsarn | -1 is 0xffffffff so array[-1] is a valid BIG array
Roland
Suradet Jitprapaikulsarn a écrit :
> Hi,
>
> The compiler compiled the following code.
>
> int main()
> {
> int array1[0];
> int array2[-1];
>
> return 0;
> }
>
> I believe the compiler should issue the errors instead.
|
June 08, 2001 Re: undetected negative or zero size. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suradet Jitprapaikulsarn | Version 8.16 does issue error for the negative sized array. Not very descriptive (internal error cgcod 585) but the error message is there nevertheless.
Btw by the language definition there is no bounds checking on arrays in C, so do not expect one, unless there is a code/data overflow. In a 32 bit world, an array of 1073741828 integers takes up exactly 16 bytes because of modulo 2^32 arithmetic (I will let you figure that one out).
Suradet Jitprapaikulsarn wrote:
> Hi,
>
> The compiler compiled the following code.
>
> int main()
> {
> int array1[0];
> int array2[-1];
>
> return 0;
> }
>
> I believe the compiler should issue the errors instead.
|
June 27, 2002 Re: undetected negative or zero size. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jan Knepper | In article <3B204AFD.6B5CCD75@smartsoft.cc>, Jan Knepper (jan@smartsoft.cc) says... > <g> > I can not believe people actually try this!!! > When you buy a car do you also run it into the crash barrier to > see how it looks after wards?? <g> The negative array size is handy for implementing what is known as a "compile-time assert". It is an assert which does not have to execute and can be used to verify constant expressions. It has zero runtime cost and can be used in contexts where a normal assert is difficult to arrange. Here is what I have used to good effect: #define BuildAssert(expr) extern char _rgcAssert[(expr)?1:-1] The failure is a little cryptic, but it is easy to accompany the assertion with a comment saying what is being enforced and why. > Suradet Jitprapaikulsarn wrote: > > > Hi, > > > > The compiler compiled the following code. > > > > int main() > > { > > int array1[0]; > > int array2[-1]; > > > > return 0; > > } > > > > I believe the compiler should issue the errors instead. -- -Larry Brasfield (address munged, s/sn/h/ to reply) |
June 28, 2002 Re: undetected negative or zero size. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Larry Brasfield | Agreed. Compile-time asserts are extremely useful, and cost nothing. They should be facilitated by [-ve] errors as a priority "Larry Brasfield" <larry_brasfield@snotmail.com> wrote in message news:MPG.1784fe26d69af93a989680@news.digitalmars.com... > In article <3B204AFD.6B5CCD75@smartsoft.cc>, > Jan Knepper (jan@smartsoft.cc) says... > > <g> > > I can not believe people actually try this!!! > > When you buy a car do you also run it into the crash barrier to > > see how it looks after wards?? <g> > > The negative array size is handy for implementing > what is known as a "compile-time assert". It is > an assert which does not have to execute and can > be used to verify constant expressions. It has > zero runtime cost and can be used in contexts > where a normal assert is difficult to arrange. > > Here is what I have used to good effect: > #define BuildAssert(expr) extern char _rgcAssert[(expr)?1:-1] > > The failure is a little cryptic, but it is easy > to accompany the assertion with a comment saying > what is being enforced and why. > > > Suradet Jitprapaikulsarn wrote: > > > > > Hi, > > > > > > The compiler compiled the following code. > > > > > > int main() > > > { > > > int array1[0]; > > > int array2[-1]; > > > > > > return 0; > > > } > > > > > > I believe the compiler should issue the errors instead. > > -- > -Larry Brasfield > (address munged, s/sn/h/ to reply) |
June 28, 2002 Re: undetected negative or zero size. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Larry Brasfield | Sure! #include <stdio.h> int main ( int, char **, char ** ) { int a [ 128 ]; int *b = ( a + 64 ); for ( int i = 0 ; i < 128 ; i++ ) a [ i ] = i - 64; // ... for ( i = -64 ; i < 64 ; i++ ) printf ( "%3d ", b [ i ] ); } Larry Brasfield wrote: > In article <3B204AFD.6B5CCD75@smartsoft.cc>, > Jan Knepper (jan@smartsoft.cc) says... > > <g> > > I can not believe people actually try this!!! > > When you buy a car do you also run it into the crash barrier to > > see how it looks after wards?? <g> > > The negative array size is handy for implementing > what is known as a "compile-time assert". It is > an assert which does not have to execute and can > be used to verify constant expressions. It has > zero runtime cost and can be used in contexts > where a normal assert is difficult to arrange. > > Here is what I have used to good effect: > #define BuildAssert(expr) extern char _rgcAssert[(expr)?1:-1] > > The failure is a little cryptic, but it is easy > to accompany the assertion with a comment saying > what is being enforced and why. > > > Suradet Jitprapaikulsarn wrote: > > > > > Hi, > > > > > > The compiler compiled the following code. > > > > > > int main() > > > { > > > int array1[0]; > > > int array2[-1]; > > > > > > return 0; > > > } > > > > > > I believe the compiler should issue the errors instead. > > -- > -Larry Brasfield > (address munged, s/sn/h/ to reply) |
June 28, 2002 Re: undetected negative or zero size. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jan Knepper | Sorry for being a bit thick, Jan, but I don't get the point you are making with this code. You haven't put a -ve literal into an array declaration, as in int ar[-12]; so it is no surprise that this works. Maybe you are meaning to illustrate something completely different. ???? "Jan Knepper" <jan@smartsoft.cc> wrote in message news:3D1BB23F.CBEE2452@smartsoft.cc... > Sure! > > #include <stdio.h> > > > > int main ( int, char **, char ** ) > { > int a [ 128 ]; > int *b = ( a + 64 ); > > for ( int i = 0 ; i < 128 ; i++ ) > a [ i ] = i - 64; > > // ... > > for ( i = -64 ; i < 64 ; i++ ) > printf ( "%3d ", b [ i ] ); > } > > > > Larry Brasfield wrote: > > > In article <3B204AFD.6B5CCD75@smartsoft.cc>, > > Jan Knepper (jan@smartsoft.cc) says... > > > <g> > > > I can not believe people actually try this!!! > > > When you buy a car do you also run it into the crash barrier to > > > see how it looks after wards?? <g> > > > > The negative array size is handy for implementing > > what is known as a "compile-time assert". It is > > an assert which does not have to execute and can > > be used to verify constant expressions. It has > > zero runtime cost and can be used in contexts > > where a normal assert is difficult to arrange. > > > > Here is what I have used to good effect: > > #define BuildAssert(expr) extern char _rgcAssert[(expr)?1:-1] > > > > The failure is a little cryptic, but it is easy > > to accompany the assertion with a comment saying > > what is being enforced and why. > > > > > Suradet Jitprapaikulsarn wrote: > > > > > > > Hi, > > > > > > > > The compiler compiled the following code. > > > > > > > > int main() > > > > { > > > > int array1[0]; > > > > int array2[-1]; > > > > > > > > return 0; > > > > } > > > > > > > > I believe the compiler should issue the errors instead. > > > > -- > > -Larry Brasfield > > (address munged, s/sn/h/ to reply) > > > |
June 28, 2002 Re: undetected negative or zero size. | ||||
---|---|---|---|---|
| ||||
Posted in reply to Suradet Jitprapaikulsarn |
Suradet Jitprapaikulsarn a écrit :
> Hi,
>
> The compiler compiled the following code.
>
> int main()
> {
> int array1[0];
> int array2[-1];
>
> return 0;
> }
>
> I believe the compiler should issue the errors instead.
int array2[-1]
is equivalent to
int array2[0xffffffff];
roland
|
Copyright © 1999-2021 by the D Language Foundation