forgetting -betterC means no runtime bounds checking?

tsbockman thomas.bockman at gmail.com
Thu Jan 5 14:09:17 UTC 2023


On Thursday, 5 January 2023 at 09:10:00 UTC, areYouSureAboutThat 
wrote:
> My question is: why is there no bounds checking occurring if I 
> forget to use -betterC?
>
> module test;
>
> extern(C) void main()

Note that whether bounds checking is performed depends on 
[compiler 
switches](https://dlang.org/dmd-windows.html#switch-boundscheck), 
and that there is a mode where `@safe` functions are checked, but 
`@system` functions (which your `main` appears to be) are not. 
This mode is the default for `-release` builds:
```
-boundscheck=[on|safeonly|off ]
Controls if bounds checking is enabled.
     on: Bounds checks are enabled for all code. This is the 
default.

     safeonly: Bounds checks are enabled only in @safe code. This 
is the default for -release builds.

     off: Bounds checks are disabled completely (even in @safe 
code). This option should be used with caution and as a last 
resort to improve performance. Confirm turning off @safe bounds 
checks is worthwhile by benchmarking.
```
So, if you want bounds checking, make  sure you either have 
`-boundscheck=on`, or use an `@safe` function together with 
`-boundscheck=safeonly`.


More information about the Digitalmars-d-learn mailing list