www.digitalmars.com         C & C++   DMDScript  

c++ - undetected negative or zero size.

reply Suradet Jitprapaikulsarn <suradet writeme.com> writes:
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.
Jun 07 2001
next sibling parent reply Jan Knepper <jan smartsoft.cc> writes:
<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.

Jun 07 2001
parent reply Larry Brasfield <larry_brasfield snotmail.com> writes:
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)
Jun 27 2002
next sibling parent "Matthew Wilson" <matthew thedjournal.com> writes:
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)

Jun 27 2002
prev sibling parent reply Jan Knepper <jan smartsoft.cc> writes:
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)

Jun 27 2002
parent "Matthew Wilson" <matthew thedjournal.com> writes:
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)


Jun 27 2002
prev sibling next sibling parent Roland <rv ronetech.com> writes:
-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.

Jun 08 2001
prev sibling next sibling parent John Kohr <jjkohr1 home.com> writes:
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.

Jun 08 2001
prev sibling parent Roland <rv ronetech.com> writes:
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
Jun 28 2002