In this section:

Overview

This error is generated whenever an illegal value will be used to index an array that is being written. If this error can be detected during compilation, a compilation error will be issued instead of the normal runtime error.

CodeDescriptionEnabledReportedPlatform
WRITE_BAD_INDEXWriting array out of range(tick)RuntimeWindows/Unix


Problem

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

/*
 * File: writindx.c
 */
main()
{
	int i, a[10];

	for(i=1; i<=10; i++)
	a[i] = 0;
	return (0);
}

Other problems include writimd2.c and writimd3.c. A diagnosis similar to the one that follows applies to both.

Diagnosis at Runtime

[writindx.c:9] **WRITE_BAD_INDEX**
>>		a[i] = 0;
Writing array out of range: a[i]
Index used: 10
Valid range: 0 thru 9 (inclusive)
Stack trace where the error occurred:
			main() writindx.c, 9
**Memory corrupted. Program may crash!!**
  • Line 2: Source line at which the problem was detected.
  • Line 3: Description of the problem and the expression that is in error.
  • Line 4: Illegal index value used.
  • Line 5: Valid index range for this array.
  • Line 6: Stack trace showing the function call sequence leading to the error.
  • Line 8: Informational message indicating that a serious error has occurred which may cause the program to crash. 

Repair

This is normally a fatal error and is often introduced algorithmically. Other common sources include loops with incorrect initial or terminal conditions, as in this example, for which the corrected code is the following:

main()
{
	int i, a[10];
	for(i=; i<sizeof(a)/sizeof(a[0]); i++)
	a[i] = 0;
	return (0);
}



  • No labels