Can Array Overruns Lead to Catastrophic Failures?

  • C/C++
  • Thread starter Lillyotv
  • Start date
  • Tags
    Array Lead
In summary, C++ does not provide bounds checking on array operations, but this is easily done with the std::vector class. Java does this, as does C#.
  • #1
Lillyotv
12
0
Can over running of array lead to the catastrophic failures?
If ‘Yes’ then why does not C++ provide bound checking on array operations and who is responsible to prevent array overruns?
 
Technology news on Phys.org
  • #2
First question: definitely. Obviously if you need a piece of data that doesn't exist, some operations will fail. My experience is in C# and not C++, but my understanding is that C-type languages can't ever be expected to catch all array bounding errors.

It's always better to write solid code that behaves in the way the programmer intended than to expect the compiler to catch errors for you.
 
  • #3
Lillyotv said:
Can over running of array lead to the catastrophic failures?
Yes, or worse. See http://en.wikipedia.org/wiki/Buffer_overflow

If ‘Yes’ then why does not C++ provide bound checking on array operations
Bounds checking is inefficient. C++, like C, is intended to allow the programmer to write efficient code, and so it would be inappropriate for standard C++ to enforce bounds checking.


and who is responsible to prevent array overruns?
It's the programmer's job to write correct code. If you are worried about this error, then you shouldn't use raw arrays; you should write a customized array class that does provide bounds checking.

In fact, the C++ standard std::vector class does provide bounds checking if you use the at member to access data.
 
  • #4
Sojourner01 said:
C-type languages can't ever be expected to catch all array bounding errors.
Java does. Wikipedia suggests that C# does too.
 
  • #5
Also, IIRC, the following is valid C++ code
Code:
int intarray[20];
int* ptr = intarray; 
// equivalently:
ptr = &intarray[0];
ptr += 4;
// now ptr points at either &intarray[1] or &intarray[4]
I can imagine it is hard for the compiler to check on every pointer assignment if it happens to point at an array, if the assignment would cause it to point outside it, and if the programmer didn't just mean to do it, as in
Code:
... // above code, then
int someInt;
ptr = &someInt; 
// should the compiler warn or not?
 
  • #6
The compiler cannot always determine whether an array access is out of bounds. Consider the cases of an array index formed from an argument passed to a function. How can the compiler possibly determine at compile time whether use of this computed index entails an out-of-bounds access? In languages that do check for out-of-bounds conditions, the check is almost always performed at execution time.

While C/C++ arrays are not accompanied with any bounds checking, the vector template in the C++ standard template library does provide this ability.
 
  • #7
Hurkyl said:
Bounds checking is inefficient. C++, like C, is intended to allow the programmer to write efficient code, and so it would be inappropriate for standard C++ to enforce bounds checking.
This is exactly correct. C/C++ is intended to allow the programmer to write efficient code, not to efficiently write code!
 
  • #8
Hurkyl said:
Java does. Wikipedia suggests that C# does too.

There's a world of difference between C/C++ and Java/C#, though :wink:
 
  • #9
Wikipedia suggests that C# does too.

Not the case. I've been grappling with out-of-bounds issues for some time with a project of mine. I'm fortunate that my project will come across one every runtime if any are present, so they're relatively easy to find.
 
  • #10
Sojourner01 said:
Not the case. I've been grappling with out-of-bounds issues for some time with a project of mine. I'm fortunate that my project will come across one every runtime if any are present, so they're relatively easy to find.

Yes it does. Bounds checking means that array access attempts are checked at runtime to make sure an access to a memory location outside the array's bounds doesn't occur. If such an access is attempted, a runtime error will occur (specifically, an IndexOutOfRangeException will be thrown, in the case of C#), rather than the access going through, which would cause unpredictable results.

You're thinking of compile-time bounds checking, which is largely impossible.
 
Last edited:
  • #11
Thanks a lot. I have the concept clear in my head now.
Cheers:)
 

1. What is an array overrun?

An array overrun occurs when a program tries to access a memory location beyond the boundaries of an array. This can happen when the program attempts to read or write data to an index that is outside the size of the array.

2. How can array overruns lead to catastrophic failures?

If an array overrun occurs, it can cause the program to access and modify unintended memory locations, leading to unexpected behavior or crashes. This can be especially dangerous if the overwritten memory contains critical data or instructions, resulting in serious consequences such as system crashes or security vulnerabilities.

3. What are the common causes of array overruns?

Array overruns can be caused by programming errors, such as using incorrect loop conditions or not properly checking the size of an array. They can also be caused by malicious attacks, where an attacker intentionally tries to access or modify the memory of a program.

4. How can array overruns be prevented?

To prevent array overruns, programmers should always validate input and ensure that array indexes are within the bounds of the array. Using secure coding practices and tools, such as bounds checking and memory access patterns, can also help identify and prevent potential array overrun errors.

5. Are there any programming languages that are more prone to array overruns?

Some programming languages, such as C and C++, do not perform automatic bounds checking on arrays, making them more prone to array overruns. However, this does not mean that these languages are inherently less secure. With proper coding practices and tools, array overruns can be prevented in any programming language.

Similar threads

  • Programming and Computer Science
Replies
31
Views
2K
  • Programming and Computer Science
Replies
6
Views
968
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Special and General Relativity
5
Replies
146
Views
6K
  • Programming and Computer Science
Replies
13
Views
2K
  • Computing and Technology
Replies
2
Views
719
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
30
Views
4K
Back
Top