Out of bound array

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

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.

0 Likes
3 Replies
Anonymous
Not applicable

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.

0 Likes
Anonymous
Not applicable

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

0 Likes
Anonymous
Not applicable

Hi arenosoft,

   

 

   

Thanks for sharing the info.

0 Likes