1

Previously when manipulating an array, I can go over the array bounds and I know that C is lenient. Now when I access a value that out of those boundaries, I get an error [-Warray-bounds] and it prevents me from diving into memory. Code:

#include <stdio.h>
int main(void)
{
    int arr[] = {1,2,3,4,5}; 
    printf("arr [0] is %d\n", arr[0]); 
    printf("arr[10] is %d\n", arr[10]);     
}

errors:

address.c:12:31: warning: array index 10 is past the end of the array (which contains 5 elements) [-Warray-bounds]
    printf("arr[10] is %d\n", arr[10]);

Is that something new in a compiler, and if yes, how can I disable or enable it?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Run
  • 130
  • 6
  • 2
    Why do you want to disable it? A good answer depends on what you are trying to do. In most cases, the answer is you should not disable it. – Eric Postpischil Jan 21 '21 at 21:39
  • 2
    Also note it is not apt to describe C as “lenient” in this regard, any more than it is appropriate to say a parent is “lenient” when they let a young child play in the street where there may be traffic. A more accurate description is that the parent is negligent. C does not prevent you from playing in the street, but that is more because it is not protecting you than because it is permitting you to use more memory than you reserved. – Eric Postpischil Jan 21 '21 at 21:41
  • If you could do something it does not make it correct nor legal. – Slava Jan 21 '21 at 21:48
  • Does it say "error" or does it say "warning"? – user253751 Jan 21 '21 at 22:01
  • Note that adding tests to ensure no one ever goes out of bounds would slow down programs where the writers took care to never go out of bounds. C and C++ offer incredible speed partly because they leave it to the programmer to determine when to apply the breaks. – user4581301 Jan 21 '21 at 22:44
  • If the compiler can catch an out of bounds error for you, celebrate it because a lot of the time it cannot. For example, if you use the array through a pointer (because it is dynamically allocated or it has [decayed to a pointer](https://stackoverflow.com/questions/1461432/what-is-array-to-pointer-decay)), the length of the array is not available and the only one who can save your program is you. – user4581301 Jan 21 '21 at 22:47
  • *"I know that C is lenient"* --- No, no, no. C is not lenient, C is exact. The address one-past the end of an array is a VALID address that can be used when iterating past the last element, but CANNOT be derefenced to attempt to obtain the value there. – David C. Rankin Jan 21 '21 at 22:56

2 Answers2

2

i can go over array bound

You "can" in the sense that it doesn't affect the well-formedness of the program and thus won't necessitate the compiler to issue a diagnostic message. Despite not being required to, if a compiler is able to detect the bug, then it is allowed to warn the programmer.

However if you do this, then the behaviour of the program will be undefined. That means that nothing about the behaviour of the program is guaranteed by the language. Undefined behaviour is detrimental to the usefulness of the program and its avoidance is extremely important.

As such: Do not access an array out of bounds! It's a bad thing to do.

Is that something new in a compiler

"New" is relative. At least version 4.7.1 of GCC is able to detect this, and it was released about 9 years ago. Clang 3.0.0 is likewise able to detect it and it was released about 10 years ago.

how can i disable or enable it?

Compilers generally provide options to enable and disable specific warnings. The warning message that you quoted states which warning option has enabled it.

[-Warray-bounds]

I recommend to not disable the warning, and to fix the bug when ever you see this warning.

eerorika
  • 232,697
  • 12
  • 197
  • 326
0

Sounds like you turned on -Wall and using at least O2 optimizations in your code (assuming you are using GCC compiler). Not sure why you would want to access an array index that is out of bounds. All it will do is retrieve garbage values.

If you do not want to turn off all warnings, then you could use -Warray-bounds=0 to turn off that particular one.

LurkerZ
  • 92
  • 4
  • More conventionally, you disable a warning with `-Wno-array-bounds` (but 0 may also work if you can specify other non-zero values to modify the behaviour of the arrays bounds checking). – Jonathan Leffler Jan 21 '21 at 22:55