//
you're reading...
Uncategorized

Important Software Bugs and Errors in C++

We give code examples to 3 common software bugs and programming errors:
-Array Buffer Overflow
-Integer Overflow
-Floating Point Round-off Error

#include<iostream>
using namespace std;

int main(){

  cout << " Example of Array Buffer Overflow" << endl;
  int a[10] ={1,2,3,4,5,6,7,8,9,10};
  int b[10] ={11,12,13,14,15,16,17,18,19,20};
  for (int i =0; i <= 25; i++){
     cout << i << ": " << a[i]  << "  " << b[i] << endl;}
  int i = 12;
  b[i]= 9999;
  cout << "a[0] = "<< a[0] << endl;
 
  cout << " Example of Integer Overflow" << endl;
  int num = 1;
  unsigned int unum = 1;
  for ( int i=1 ; i <= 33 ; i++){
    num = num *2; unum = unum << 1;
    cout << i << ":  "<< num <<"   " << unum <<endl;
  }

  cout << endl <<  " Example of Roundoff" << endl;
  float f1 = 1000.43;
  float f2 = 1000.0;
  if ((f1-f2) != 0.43) {
    cout << "roundoff error" << endl;
    cout << ".43 != " <<  f1 - f2 << '\n';
  } 

} 

/*   Explanation:
1000.43 is actually represented as the following bitpattern 
-the “s” shows the position of the sign bit, 
-the “e“s show the positions of the exponent bits, 
-the “m“s show the positions of the mantissa bits:

    seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm
    01000100011110100001101110000101

The shifted mantissa is 1111101000.01101110000101 or 1000 + 7045/16384. 
The fractional part is 0.429992675781. 
*/

Advertisement

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: