C array bounds checking

Run Settings
LanguageC
Language Version
Run Command
#include <stdio.h> #define BOUNDCHECK 1 #if BOUNDCHECK #define DeclareArray(Type,Name,Size) \ Type Name[Size] = {}; \ int Name##Length_ = Size; #define SetElement(Name,Element,Value) \ if((Element) < Name##Length_ && (Element) >= 0){\ Name[Element] = Value; \ }else{ \ printf("WARNING\n"); \ } #define GetElement(Name,Element) \ (((Element) < Name##Length_ && (Element) >= 0)? \ Name[Element]:printf("WARNING\n")) #else #define DeclareArray(Type,Name,Size) \ Type Name[Size] = {}; #define SetElement(Name,Element,Value) \ { \ Name[Element] = Value; \ } #define GetElement(Name,Element) \ (Name[Element]) #endif int main() { DeclareArray(int,SomeArray,5); SetElement(SomeArray,4,7); SetElement(SomeArray,5,2); // Waring GetElement(SomeArray,5); // Waring int Element = GetElement(SomeArray,4); printf("Element = %i\n", Element ); printf("Element = %i\n", GetElement(SomeArray,5) ); // Warning return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines