yet another segfault - array out of bound is not caught by try catch

Nicholas Wilson iamthewilsonator at hotmail.com
Fri Sep 17 12:13:50 UTC 2021


On Friday, 17 September 2021 at 11:10:33 UTC, seany wrote:
> I have now this function, as a private member in a Class :
>             } catch (RangeError er) {

I can't remember if you can catch an index OOB error but try 
`catch (Throwable er)` will work if it is catchable at all and 
you can figure out what kind of Error you have by printing its 
name.

>  "Attempt to take address of value not located in memory" ? I 
> am not even calling / accessing a pointer. I am trying to 
> extract a value outside an array bound.

`Type[]` arrays in D are effectively struct {size_t length; Type* 
ptr; } under the hood. Your problem is the array has no elements 
which is why trying to extract a value outside an array bound is 
an irrecoverable error.

> with the bound checking operation in place, would the bound 
> error be triggered before the attempt to take unavailable 
> address error has a chance to trigger?

with a null array of zero length `arr`, `arr[0]` with bounds 
check enabled will fail the bounds check before it tries to 
dereference the pointer. if you try `arr.ptr[0]` to bypass the 
bounds checking (which is a very bad idea!) you will then try to 
load from an invalid memory address and crash.



More information about the Digitalmars-d-learn mailing list