On Sun, 09 Oct 2011 12:24:24 +0100, Graham Cole wrote: > I understand from the documentation that the "-release" compiler switch > turns off "array bounds checking for system and trusted functions". > > Is it correct that the following code should seg fault when compiled > with "-release" ? > > string[string] h; > h["abc"] = "def"; > string s = h["aaa"]; > > I.e. retrieving from an associative array for a non existent key. > > I would have thought that an exception should be generated in this case > when compiled with "-release" (as it is when compiled without). > > This code behaves the same when compiled by both D1 and D2. > > Should I report this as a bug ?
Howdy Mr. Cole, The "-release" flag disables runtime checks of data ranges and bounds of things like arrays and pointers. These checks take a little bit of time, but they help you catch an error as early as possible. Without the "-release" flag, the checks are removed, so there are two possibilities when you index an array index. A. The memory address is out of bounds for the program as a whole, and results in a segfault. B. The memory address is in range for the the program as a whole, but has no been initialized or refers to data in a completely different variable. Errors of the second type are especially sinister and very hard to debug. Consider the following program: void main() { int[] ages = [28, 23, 40]; assert(ages[0] == 28); ages[3] = 54; assert(ages[3] == 54); } $ dmd -release bounds.d $ ./bounds # No segfault because the address is within the address space # for the program allowed by the OS. $ dmd bounds.d $ ./bounds core.exception.RangeError@bounds(6): Range violation ---------------- ./bounds(onRangeError+0x28) [0x805f908] ./bounds(_d_array_bounds+0x16) [0x805d516] ./bounds() [0x805ae8e] ./bounds(_Dmain+0x6c) [0x805ae40] ./bounds(_D2rt6dmain24mainUiPPaZi7runMainMFZv+0x1a) [0x805d96e] ./bounds(_D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv+0x20) [0x805d608] ./bounds(_D2rt6dmain24mainUiPPaZi6runAllMFZv+0x32) [0x805d9b2] ./bounds(_D2rt6dmain24mainUiPPaZi7tryExecMFMDFZvZv+0x20) [0x805d608] ./bounds(main+0x94) [0x805d5b4] /lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xe7) [0xb7681e37] ./bounds() [0x805ad21] ---------------- - Vijay