7 Most Common Programming and Coding Mistakes

4. Integer Overflow or Wraparound

An Integer Overflow error occurs when you attempt to store a larger value into an integral type than will fit. When this happens, the larger value is truncated and the operation ends up storing an unpredictable result. For example, a 2-byte unsigned short can store a maximum value of 65535. Now, imagine adding two short values such as 65530 and 10 and storing the result in a short. The result (65545) will not fit in a short, leaving some unpredictable value in the target short as a result of truncation. When subsequently using this value in another operation (such as an array index), you will end up with unpredictable results.computer science computer science computer science computer science computer science

A C Example:

short a = 65530, b = 10;
short c = a + b;
// on my computer, c has the unexpected value: 4

5. Improper Validation of an Array Index

Another very common error encountered in software is Improper Validation of an Array Index. It arises when accessing an array using an index that lies outside the bounds of the array. When you access a location outside the program’s valid data boundaries, you end up with a memory access error (also known as a segmentation violation). When the memory location lies within the data bounds but outside of the array, you are looking at memory corruption when you write to such locations.

Prev3 of 4Next

Leave a Reply

Your email address will not be published. Required fields are marked *