miscellaneous array questions...

Johan j at j.nl
Tue Jul 21 15:32:57 UTC 2020

On Monday, 20 July 2020 at 22:05:35 UTC, WhatMeWorry wrote:
> 1) The D Language Reference says:
> "There are four kinds of arrays..." with the first example being
> "type*	Pointers to data"  and "int* p;  etc.
> At the risk of sounding overly nitpicky, isn't a pointer to an 
> integer simply a pointer to an integer?  How does that pertain 
> to an array?

I agree. "type*" being an array makes no sense from a D language 
point of view.

> 2) "The total size of a static array cannot exceed 16Mb" What 
> limits this? And with modern systems of 16GB and 32GB, isn't 
> 16Mb excessively small?   (an aside: shouldn't that be 16MB in 
> the reference instead of 16Mb? that is, Doesn't b = bits and B 
> = bytes)

This doesn't make sense either. Where did you find this in the 
documentation? It should be removed, as it is easily proven to 
work (`ubyte[170_000_000] s; void main(){s[160_000_000] = 1;}`).

> 3) Lastly, In the following code snippet, is arrayA and arrayB 
> both allocated on the stack? And how does their scopes and/or 
> lifetimes differ?
> ==== module1 =====
> int[100] arrayA;
> void foo() // changed from main to foo for clarity
> {
>     int[100] arrayB;
>     // ...
> }
> ==== module1 =====

"The stack" is not a D language thing, a better way of looking at 
it is that local storage is implemented by all D compilers by 
using the "stack" (on x86).

arrayA is not allocated on the stack, lifetime is whole duration 
of program, one array per thread.
arrayB is indeed allocated on the stack (local storage), lifetime 
is only from start to end of foo(), one array per call to foo (!).

Because arrayB is on the stack, you are limited by stack size 
which is set by the OS (but can be overridden). The array would 
be competing with all other things that are put on the stack, 
such as function call return addresses and temporary values, both 
of which you as coder cannot see. What maximum size of arrayB you 
can get away with heavily depends on the rest of your program 
(and the stack size allocated by the OS, which is somewhere in 
the 4MB, 8MB, 16MB range), thus best to avoid putting large 
arrays on the stack alltogether.
arrayA is allocated together with other global/TLS variables in a 
section for which I don't think there really is a size limit.


More information about the Digitalmars-d-learn mailing list