A computer-readable non-transitory storage medium configured with data and with instructions that when executed by at least one processor causes the processor(s) to perform a process for compile-time bounds checking of user-defined types, the process comprising the following steps performed during compilation of a program from a source code through an intermediate language code to an executable code: identifying in the source code a user-defined class for safely accessing memory, namely, a user-defined class having as an annotated member a memory-accessing code which is annotated with a user-defined-bounds check annotation and also having as another annotated member a bound-providing member which is annotated to indicate that it provides bound information for generating a bounds check on the memory-accessing code; the user-defined-bounds check annotation comprising an annotation indicating to the compiler that the code accesses a memory-mapped buffer or other explicitly allocated memory; inserting into the intermediate language code a bounds-check representation of the user-defined-bounds check annotation, the inserting being performed during compilation of the corresponding annotated source code and the inserting being accomplished using at least one of parse trees, abstract syntax trees, attributes and generalized dope vectors to represent the user-defined-bounds check annotation; applying, to the intermediate language code and/or the executable code, an optimization which reduces duplicative bounds checking that would otherwise occur in the executable code; wherein applying the optimization comprises determining that all accesses inside a loop to a variable of the user-defined class are within allowable memory address bounds of the variable, and, in response to this, removing multiple bound checks that would otherwise occur as a result of executing the loop, wherein the optimization is accomplished by analytically determining that a memory access which is subject to bounds checking cannot assume a value during execution that would result in a memory access outside allowed bounds. identifying in the source code a user-defined class for safely accessing memory, namely, a user-defined class having as an annotated member a memory-accessing code which is annotated with a user-defined-bounds check annotation and also having as another annotated member a bound-providing member which is annotated to indicate that it provides bound information for generating a bounds check on the memory-accessing code; the user-defined-bounds check annotation comprising an annotation indicating to the compiler that the code accesses a memory-mapped buffer or other explicitly allocated memory; inserting into the intermediate language code a bounds-check representation of the user-defined-bounds check annotation, the inserting being performed during compilation of the corresponding annotated source code and the inserting being accomplished using at least one of parse trees, abstract syntax trees, attributes and generalized dope vectors to represent the user-defined-bounds check annotation; applying, to the intermediate language code and/or the executable code, an optimization which reduces duplicative bounds checking that would otherwise occur in the executable code; wherein applying the optimization comprises determining that all accesses inside a loop to a variable of the user-defined class are within allowable memory address bounds of the variable, and, in response to this, removing multiple bound checks that would otherwise occur as a result of executing the loop, wherein the optimization is accomplished by analytically determining that a memory access which is subject to bounds checking cannot assume a value during execution that would result in a memory access outside allowed bounds.