Thread overview
undetected negative or zero size.
Jun 08, 2001
Jan Knepper
Jun 27, 2002
Larry Brasfield
Jun 28, 2002
Matthew Wilson
Jun 28, 2002
Jan Knepper
Jun 28, 2002
Matthew Wilson
Jun 08, 2001
Roland
Jun 08, 2001
John Kohr
Jun 28, 2002
Roland
June 08, 2001
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
<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
-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
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
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
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
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
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

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