What's left for 1.0?

Walter Bright newshound at digitalmars.com
Sat Nov 18 14:43:52 PST 2006


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