Testing and Debugging
Boundary Check
An index check prevents the program from reading outside an array.
Boundary Check
boundary_check.c
#include <stdio.h>
int main(void) {
int index = ;
int values[3] = {10, 20, 30};
int selected = -1;
if (index >= 0 && index < 3) {
selected = values[index];
}
printf("selected=%d\n", selected);
return 0;
}
#include <stdio.h>
int main(void) {
int index = ;
int values[3] = {10, 20, 30};
int selected = -1;
if (index >= 0 && index < 3) {
selected = values[index];
}
printf("selected=%d\n", selected);
return 0;
}
#include <stdio.h>
int main(void) {
int index = ;
int values[3] = {10, 20, 30};
int selected = -1;
if (index >= 0 && index < 3) {
selected = values[index];
}
printf("selected=%d\n", selected);
return 0;
}
range guard
The guard checks both lower and upper bounds before indexing.
fallback value
The fallback path gives a predictable value for out-of-range input.