What's left for 1.0? (static data)

Bill Baxter dnewsgroup at billbaxter.com
Sun Nov 19 16:11:23 PST 2006


Thanks for the reply.

So in short, are you saying that
    static const char[] c = [1,2,3,4,5,6];
really does not cause any heap allocations?  If so then that's exactly 
what I want.

However, if that is the case then this should work:

static const ubyte[]  icon1 = [1,2,3,4,5];
static const ubyte[]  icon2 = [1,2,3,4,5];
static const ubyte[]  icon3 = [1,2,3,4,5];
static const ubyte*[] all_icons = [&icon1[0], &icon2[0], &icon3[0] ];

But it gives errors like:
staticdata.d(7): Error: non-constant expression cast(ubyte*)(icon1)

This, however, works:
static const ubyte[5]  icon1 = [1,2,3,4,5];
static const ubyte[5]  icon2 = [1,2,3,4,5];
static const ubyte[5]  icon3 = [1,2,3,4,5];
static const ubyte*[] all_icons = [&icon1[0], &icon2[0], &icon3[0] ];


The error messages are the main thing that led me to believe that the 
former must be doing dynamic heap allocations.

So is this whole issue really just a bug with deducing what's const and 
what's not?

--bb

Walter Bright wrote:
> Bill Baxter wrote:
>> Walter Bright wrote:
>>> const char[] c = [ 1,2,3,4,5,6,7, 255 ];
>>>
>>> works fine, though it's a dynamic array.
>>
>> Yep it's great that D has built-in dynamic arrays, but the point is 
>> that the syntax for dynamic arrays is getting in the way of static 
>> arrays, making something that's simple in C become hard in D.  If you 
>> want a static array you have no choice right now but to count up the 
>> elements, or deliberately use the wrong length to trigger compiler 
>> errors that will tell you the right length.
> 
> If you want an array put into the static data segment,
>     static const char[] c = [ 1,2,3,4,5,6,7, 255 ];
> will do it.
> 
>>> So can D:
>>> char *list[] = [ "eggs","bacon","milk","break" ];
>>  > char[] list[] = [ "eggs","bacon","milk","break" ];
>>
>> Nope, D cannot:
>>    dchar.d(12): Error: cannot implicitly convert expression ("bacon") 
>> of type char[5] to char[4]
> 
> The following D program:
> ------------------
> char *list[] = [ "eggs","bacon","milk","break" ];
> char[] list2[] = [ "eggs","bacon","milk","break" ];
> ------------------
> compiles without error.
> 
> 
>> But, then you're making everything dynamic when it should be static. 
>> Correct me if I'm wrong, but in the C/C++ version of this above, 
>> basically everything is static.  The strings will be embedded into the 
>> exe, and the array will just consist of pointers directly to those 
>> strings in the data segment.  But in the D version you'll have that 
>> same data in the data segment, and then you'll also make dynamic 
>> copies of all the data onto the heap at runtime.  I don't want two 
>> copies of all my static data, especially if one of those copies 
>> requires runtime heap allocations.
> 
> In the above D program, everything is put into the static data segment. 
> Here's an excerpt from the object file:
> 
> _DATA   segment
>         db      065h,067h,067h,073h,000h,062h,061h,063h
>         db      06fh,06eh,000h,06dh,069h,06ch,06bh,000h
>         db      062h,072h,065h,061h,06bh,000h,000h,000h
>         dd      offset FLAT:_DATA
>         dd      offset FLAT:_DATA[5]
>         dd      offset FLAT:_DATA[0Bh]
>         dd      offset FLAT:_DATA[010h]
> _D4test4listAPa:
>         db      004h,000h,000h,000h
>         dd      offset FLAT:_DATA[018h]
>         db      065h,067h,067h,073h,000h,062h,061h,063h
>         db      06fh,06eh,000h,06dh,069h,06ch,06bh,000h
>         db      062h,072h,065h,061h,06bh,000h,000h,000h
>         db      004h,000h,000h,000h
>         dd      offset FLAT:_D4test4listAPa[8]
>         db      005h,000h,000h,000h
>         dd      offset FLAT:_D4test4listAPa[0Dh]
>         db      004h,000h,000h,000h
>         dd      offset FLAT:_D4test4listAPa[013h]
>         db      005h,000h,000h,000h
>         dd      offset FLAT:_D4test4listAPa[018h]
> _D4test5list2AAa:
>         db      004h,000h,000h,000h
>         dd      offset FLAT:_D4test4listAPa[020h]



More information about the Digitalmars-d mailing list