miscellaneous array questions...

Adam D. Ruppe destructionator at gmail.com
Tue Jul 21 12:34:14 UTC 2020

On Monday, 20 July 2020 at 22:05:35 UTC, WhatMeWorry wrote:
> How does that pertain to an array?

C arrays work as pointers to the first element and D can use that 
style too.

> 2) "The total size of a static array cannot exceed 16Mb" What 
> limits this?

The others aren't wrong about stack size limits playing some 
role, but the primary reason is that it is a weird hack for 
@safe, believe it or not.

The idea is:

class A {
     ubyte[4_000_000_000] whole_system;

@safe void lol() {
     A a;
     a.whole_system[any_address] = whatever;

With the null `a`, the offset to the static array is just 0 + 
whatever and the @safe mechanism can't trace that.

So the arbitrary limit was put in place to make it more likely 
that such a situation will hit a protected page and segfault 
instead of carrying on. (most low addresses are not actually 
allocated by the OS... though there's no reason why they 
couldn't, it just usually doesn't, so that 16 MB limit makes the 
odds of something like this actually happening a lot lower)

I don't recall exactly when this was discussed but it came up in 
the earlier days of @safe, I'm pretty sure it worked before then.

More information about the Digitalmars-d-learn mailing list