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 ~]$
```
  • forgetting -bett... areYouSureAboutThat via Digitalmars-d-learn
    • Re: forgett... areYouSureAboutThat via Digitalmars-d-learn
      • Re: for... Richard (Rikki) Andrew Cattermole via Digitalmars-d-learn
      • Re: for... Mathias LANG via Digitalmars-d-learn
    • Re: forgett... Tejas via Digitalmars-d-learn
    • Re: forgett... tsbockman via Digitalmars-d-learn

Reply via email to