static array length

Saaa empty at needmail.com
Thu Jan 31 03:41:17 PST 2008


A static string still has the .length info somewhere.
Thinking about it again, it won't work on static multidimentional arrays as 
changing one of the lengths would corrupt the way data is accessed.
I don't suggest that the actual internal size of the array would be changed, 
just the .length value.
It would make working with strings a lot simpler, or maybe I'm missing some 
nice way to do this:
char[100] c;
c=`1234`;
writefln(c); //even with c[0..4]=`1234`, this will crash


> Saaa:
>> Isn't it possible to make the .length variable for static arrays.
>> Its ofcourse an error to make it longer than the original size (extra
>> property?)
>> char[100] c;
>> c[0..4]='1234`;
>>
>> and maybe this:
>> c=`1234`; // implicit length adjustment.
>> Or would this break something?
>
> Thanks to your questions the thread regarding the string literals have 
> shown me that there are things I have to learn still regarding strings, 
> but you can answer most of your own questions when you know a bit what the 
> hidden array implementation is. Static arrays are nude chunks of data 
> (probably with some information for the GC and sometimes the memory 
> allocator of the OS too), probably allocated on the stack with alloca(), 
> while dynamic arrays are structs of <pointer, length> allocated on the 
> stack that point to a chunk of memory on the heap (I don't know if this is 
> true for string literals too). The length of the static array is a compile 
> time constant, and it's virtual at runtime, so you can't change it at 
> runtime. (Tuples too exists only at compile time. At runtime they are just 
> a scattered group of variables with a common way to index them).
> Often knowing how things are implemented and run on the level just below 
> the one you use is all you need to program effectively :-)
>
> Bye,
> bearophile 




More information about the Digitalmars-d-learn mailing list