SSJX.CO.UK
Browser Games | Cybiko | Amiga | Applications | Windows Games | Java Area | Test Area | Guides | Site Map

Out Of Bounds - C

Introduction

This is part of a series testing each of the languages I use to see how good they are at catching simple array out of bounds errors.

These tests check for the following:

The Code

The following was tested using Tiny C Compiler (TCC) 0.9.27 :

#include <stdio.h>

void main()
{
	const int pa=3;
	int pb=4;
	int array[3]={1,2,3};
		
	// #1. Simple so should be an easy catch - CT: FAILED
	array[3]=97;
	
	// #2. This is a constant so should also be an easy catch - CT: FAILED
	array[pa]=98;

	// #3. Variable index but does start outside - CT: FAILED
	array[pb]=99;

	// #4. Looping through array - CT: FAILED
	for(int i=0;i<4;i++)
	{
		printf("%d\n",array[i]);
	}
}

Test 1 - Default Settings

The compiler did not pick up any of the errors.

Running the above, it crashed with a Windows error. With #3 commented out, the program runs and displays 1,2,3,98.

Test 2 - Checks Enabled

To enable bounds checking, the program must be compiled with the '-b' switch. With this done, when you run the program you get an 'outside region' error message.

Summary

I did not expect any of the errors to be caught so was not surprised by the results. I recompiled some of my programs which seemed to work fine but with the '-b' switch and it did highlight errors which I had missed. Note that other C compilers may pick up these error but I do not have them installed...

11/01/20