Search

ParaSoft

HOME
PRODUCTS
SUPPORT
ABOUT
WHAT'S NEW
EVENTS


Insure++

Quick facts

Add-on Modules:
   -INUSE
   -TCA

Comparisons

Technical Papers

Support & Manuals

FAQs

Recent Reviews

User Testimonials

Press Releases


Insure tool to debug c++





Insure++ Reference - READ_BAD_INDEX



READ_BAD_INDEX

Reading array out of range

This error is generated whenever an illegal value will be used to index an array. It is a particularly common error that can be very difficult to detect, especially if the out-of-range elements happen to have zero values.

If this error can be detected during compilation, an error will be issued instead of the normal runtime error.

Problem

This code attempts to access an illegal array element due to an incorrect loop range.

	1:	/*
	2:	 * File: readindx.c
	3:	 */
	4:	int a[10];
	5:	int junk;
	6:	main()
	7:	{
	8:		int i, tot=0;
	9:
	10:		for(i=1; i<=10; i++)
	11:			tot += a[i]; bug
	12:		return (0);
	13:	}

Diagnosis (at runtime)

	[readindx.c:10] **READ_BAD_INDEX**
1.	>> 		tot += a[i];
	
2.		Reading array out of range: a[i]
	
3.		Index used: 10

4.		Valid range: 0 thru 9 (inclusive)

		Stack trace where the error occurred:
5.			main() readindx.c, 11

  1. Source line at which the problem was detected.
  2. Description of the problem and the expression that is in error.
  3. Illegal index value used.
  4. Valid index range for this array.
  5. Stack trace showing the function call sequence leading to the error.

Repair

Typical sources of this error include loops with incorrect initial or terminal conditions, as in this example, for which the corrected code is:

	main()
        {
              int i, tot=0, a[10];

              for(i=0; i<sizeof(a)/sizeof(a[0]); i++)
                  tot += a[i];
	      return (0);
	}

< PARM_WILD > READ_DANGLING
Tools to debug c++ and java
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft