3 Replies Latest reply on Apr 12, 2011 11:42 PM by gautam.das.g

    Out of bound array

    yeffry.zakizon

      While using PSOC5 i notice the device doesn't crash when I access out of bound array.

         

      #define MAX_ARRAY 16

         

      int array[MAX_ARRAY];

         

      int a = [array MAX_ARRAY]; //array should be in range 0 - 15

         

       

         

      I'm using PSOC creator with GCC compiler in debug mode.

        • 1. Re: Out of bound array
          gautam.das.g

          Hi arenosoft,

             

           

             

          I tested the code you are using on PSoC5.

             

          #define MAX_ARRAY 16  /*  Line 1  */

             

          int array[MAX_ARRAY];     /*  Line 2  */

             

          int a = array [MAX_ARRAY];  /*  Line 3  */

             

           

             

          In Line 3, the Square Braces is used around 'MAX_ARRAY'.

             

          The compilation gives a warning stating that variable 'a' is unused.

             

          I executed the code in 'Debug' mode

             

          In GCC, a variable declared as int is allocated 32bits, whereas in Keil it is allocated 16 bits.

             

          When the memory window was observed, the variable 'a' was updated with a value which is subsequent to the location of the array declared.

             

          As you have mentioned there is no error or warning message about the out of bound of the array during build.

          • 2. Re: Out of bound array
            yeffry.zakizon

            Hi,

               

            Thank you for your reply.

               

            My sample code is not correct, yours is correct.

               

            I found out that if I use optimization level (Release mode), GCC will give warning about subscript out of range.

               

            GCC won't give warning if optimization turn off.

               

             

               

            Thanks

            • 3. Re: Out of bound array
              gautam.das.g

              Hi arenosoft,

                 

               

                 

              Thanks for sharing the info.