Accessing an Array Out of Bounds Gives No Error, Why

Accessing an array out of bounds gives no error, why?

Welcome to every C/C++ programmer's bestest friend: Undefined Behavior.

There is a lot that is not specified by the language standard, for a variety of reasons. This is one of them.

In general, whenever you encounter undefined behavior, anything might happen. The application may crash, it may freeze, it may eject your CD-ROM drive or make demons come out of your nose. It may format your harddrive or email all your porn to your grandmother.

It may even, if you are really unlucky, appear to work correctly.

The language simply says what should happen if you access the elements within the bounds of an array. It is left undefined what happens if you go out of bounds. It might seem to work today, on your compiler, but it is not legal C or C++, and there is no guarantee that it'll still work the next time you run the program. Or that it hasn't overwritten essential data even now, and you just haven't encountered the problems, that it is going to cause — yet.

As for why there is no bounds checking, there are a couple aspects to the answer:

  • An array is a leftover from C. C arrays are about as primitive as you can get. Just a sequence of elements with contiguous addresses. There is no bounds checking because it is simply exposing raw memory. Implementing a robust bounds-checking mechanism would have been almost impossible in C.
  • In C++, bounds-checking is possible on class types. But an array is still the plain old C-compatible one. It is not a class. Further, C++ is also built on another rule which makes bounds-checking non-ideal. The C++ guiding principle is "you don't pay for what you don't use". If your code is correct, you don't need bounds-checking, and you shouldn't be forced to pay for the overhead of runtime bounds-checking.
  • So C++ offers the std::vector class template, which allows both. operator[] is designed to be efficient. The language standard does not require that it performs bounds checking (although it does not forbid it either). A vector also has the at() member function which is guaranteed to perform bounds-checking. So in C++, you get the best of both worlds if you use a vector. You get array-like performance without bounds-checking, and you get the ability to use bounds-checked access when you want it.

Why can I go out of bounds with the assignment opeator but not with the {}

Both cases are completly different. The first one is array element assignement. The second is array initialization. The rules state that it is an error if you provide more initializers that the array can hold. The rules state also, that element access behind the array end is not an error but undefined behaviour. In other languages, there is bound checking at runtime for array access, but not in C++. So the reason it behaves like it does, is that the C++ standard commitee decided so.

You could of course ask why the C++ standard commitee decided so. I think the answer is, that C++ tries to be as backward compatible as possible and the rules for array element access have always been that way.

Also the initialization can be evaluated and checked at compile time, whereas the element access happens during runtime and the compiler cannot foresee if there is actually an access behind the end of the array.

Checking if a Dynamic array out of bounds

If you know the container size at compile time, use a static container. If you don't know the container size at compile time, there is now way to catch out of bounds errors at compile time but you can catch and handle out of bounds errors at runtime, e.g. with an STL container.

A dynamic array in C++ is std::vector. Your code would look like

std::vector<int> array(10);
int i = 11;

// dynamic array out of bounds
try {
int number = array.at(i); // with out of bounds check
} catch (std::out_of_range &ex) {
// handle out of bounds exception
}
int number2 = array[i]; // faster, but without out of bounds check

operator[] accesses the element without check and at checks if the index is out of bounds.

You should avoid raw new/new[]/delete/delete[].

I am getting Array out of bounds Exception but i don't know why the error is occurring [duplicate]

You need to check if k exceeds lenght of given input then don't add character into your array and break the loop, check the below code,

public static void main(String args[]){
Scanner sc= new Scanner (System.in);
System.out.println("Enter the string");
String str=sc.next();
char arr[][]=new char[5][5];
char a[]=str.toCharArray();
int l=a.length;
int k=0;
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(k != l) {
arr[i][j]=a[k];// this is the place where the error is occurred.
k++;
}else {
break;
}
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}

No out of bounds error

  1. C doesn't check array boundaries. A segmentation fault will only occur if you try to dereference a pointer to memory that your program doesn't have permission to access. Simply going past the end of an array is unlikely to cause that behaviour. Undefined behaviour is just that - undefined. It may appear to work just fine, but you shouldn't be relying on its safety.
  2. Your program causes undefined behaviour by accessing memory past the end of the array. In this case, it looks like one of your str[i] = c writes overwrites the value in i.
  3. C++ has the same rules as C does in this case.


Related Topics



Leave a reply



Submit