digitalmars.D.learn - forgetting -betterC means no runtime bounds checking?
- areYouSureAboutThat (16/16) Jan 05 2023 I was playing around with betterC, when I discovered, that if i
- areYouSureAboutThat (14/14) Jan 05 2023 On Thursday, 5 January 2023 at 09:10:00 UTC, areYouSureAboutThat
- Mathias LANG (3/6) Jan 05 2023 This is bounds checking happening.
- Richard (Rikki) Andrew Cattermole (5/11) Jan 05 2023 Looks like an exception to me, which is what the default for bounds
- Tejas (27/43) Jan 05 2023 Works on run.dlang.io for me:
- tsbockman (23/27) Jan 05 2023 Note that whether bounds checking is performed depends on
I was playing around with betterC, when I discovered, that if i accidently forget to provide -betterC to the compiler, it will still compile this, but, there will be no runtime bounds checking occuring. My question is: why is there no bounds checking occurring if I forget to use -betterC? module test; extern(C) void main() { import core.stdc.stdio : printf; int[5] arr = [ 0, 1, 2, 3, 4]; for (int i = 0; i < 10; i++) // oops! { printf("%d\n", arr[i]); } }
Jan 05 2023
On Thursday, 5 January 2023 at 09:10:00 UTC, areYouSureAboutThat wrote:btw. the output (when you forget to use -betterC): 0 1 2 3 4 src/rt/dwarfeh.d:330: uncaught exception reached top of stack This might happen if you're missing a top level catch in your fiber or signal handler core.exception.ArrayIndexError test.d(25): index [5] exceeds array of length 5 Aborted (core dumped)
Jan 05 2023
On Thursday, 5 January 2023 at 09:17:28 UTC, areYouSureAboutThat wrote:core.exception.ArrayIndexError test.d(25): index [5] exceeds array of length 5 Aborted (core dumped)This is bounds checking happening.
Jan 05 2023
On 05/01/2023 10:17 PM, areYouSureAboutThat wrote:src/rt/dwarfeh.d:330: uncaught exception reached top of stack This might happen if you're missing a top level catch in your fiber or signal handler core.exception.ArrayIndexError test.d(25): index [5] exceeds array of length 5 Aborted (core dumped)Looks like an exception to me, which is what the default for bounds check on error will do. Because you are not using the druntime entry point, its not being caught and made to look nice to you.
Jan 05 2023
On Thursday, 5 January 2023 at 09:10:00 UTC, areYouSureAboutThat wrote:I was playing around with betterC, when I discovered, that if i accidently forget to provide -betterC to the compiler, it will still compile this, but, there will be no runtime bounds checking occuring. My question is: why is there no bounds checking occurring if I forget to use -betterC? module test; extern(C) void main() { import core.stdc.stdio : printf; int[5] arr = [ 0, 1, 2, 3, 4]; for (int i = 0; i < 10; i++) // oops! { printf("%d\n", arr[i]); } }Works on run.dlang.io for me: ```sh 0 1 2 3 4 dmd_runJFfSJW: onlineapp.d:12: Assertion 'array index out of bounds' failed. Error: program killed by signal 6 ``` On my personal ldc 1.27.1: ```sh (base) [tejasgarhewal fedora ~]$ ldc2 -betterC -run ./D/oob.d 0 1 2 3 4 oob-06265c: ./D/oob.d:9: Assertion 'array overflow' failed. Error: /tmp/oob-06265c failed with status: -2 message: Aborted (core dumped) Error: program received signal 2 (Interrupt) (base) [tejasgarhewal fedora ~]$ ```
Jan 05 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`.
Jan 05 2023