Any way to set len and ptr in dyn arrays?

James Dunne james.jdunne at gmail.com
Thu Aug 10 05:31:18 PDT 2006


nobody wrote:
> James Dunne wrote:
> 
>> nobody wrote:
>>
>>> James Dunne wrote:
>>>
>>>> This is very interesting and all, but why would you do such a thing?
>>>>
>>>> Is the .length property on the dynamic array just not cuttin' it for 
>>>> you?
>>>>
>>>
>>> No it is not. The whole raison d'etre for that array is to give me 
>>> access to the data in buf. From my reading of the docs it looks like 
>>> any case in which the length increases then this array can be copied 
>>> and might not point to the data in buf anymore. So now if I tried to 
>>> save changes to buf by passing it to the std.file.write method it 
>>> would look like nothing happened.
>>
>>
>> I'm still not following you.
>>
>> You want to access, on the byte level, the compiler-dependent 
>> implementation details of how dynamic arrays work.  I get that.  But why?
>>
>> Here:
>>
>>
>> // Sample program (with v0.163):
>> import    std.file;
>>
>> int main(char[][] args) {
>>     char[]    myFileContents = cast(char[]) std.file.read("hello.txt");
>>
>>     size_t    origLength = myFileContents.length;
>>     myFileContents.length = origLength + 20;
>>     myFileContents[origLength .. $] = "20 characters here!!";
>>
>>     std.file.write("hello2.txt", cast(void[]) myFileContents);
>>
>>     return 0;
>> }
>>
>>
>> Show me pseudo code of the larger problem you're trying to solve.  I 
>> think your assumptions on the way D's dynamic arrays work are getting 
>> in the way here.
>>
>>
> Sorry for the long delay in my reply. I was trying to work out a minimal 
> example of my problem and it got way too big. Instead I left it for this 
> morning hoping I might find a smaller way to illustrate what I meant. My 
> original example was crap (sorry for that!)so here is an example that is 
> close:
> 
> 
> int main(char[][] args)
> {
>   char[] buf = "parse words";
>   char[] ex = buf[0..1];
>   while(ex[$-1] != ' ')
>     ex.length = ex.length + 1;
>   dout.writeLine(uex.ex);
>   return 0;
> }
> 
> 
> Instead of writing into buf using the ex array this example instead 
> tries to read from buf via the ex array. However as I mentioned before 
> if you increase ex's length via .length then it no longer points into 
> buf. As a result the above example never returns.
> 
> The way forward is changing the length by some other means. This is what 
> my original example was poorly trying to illustrate. This version in 
> fact works just fine:
> 
> 
> struct sArrayKludge
> {
>   int len;
>   void* ptr;
> }
> union uArrayKludge
> {
>   char[] ex;
>   sArrayKludge meta;
> }
> 
> int main(char[][] args)
> {
>   char[] buf = "parse words";
>   uArrayKludge uex;
>   uex.meta.ptr = &buf[0];
>   uex.meta.len = 1;
>   while(uex.ex[$-1] != ' ')
>     uex.meta.len = uex.meta.len + 1;
>   dout.writeLine(uex.ex);
>   return 0;
> }
> 
> 
> Tom S, Frank Benoit and BCS helped me to see that slicing will probably 
> not have strange side effects so I should be using the following version 
> which also works fine:
> 
> 
> int main(char[][] args)
> {
>   char[] buf = "parse words";
>   char[] ex = buf[0..1];
>   while(ex[$-1] != ' ')
>     ex = buf[0..ex.length+1];
>   dout.writeLine(ex);
>   return 0;
> }
> 
> 
> So now with that said I am curious what incorrect assumptions you 
> thought I had about dynamic arrays? I always try to pay attention when 
> people say I might be missing something and even if what you were going 
> to say is not applicable to me it might help someone else.

Okay I think your incorrect assumption at first was that increasing the 
length field of a sliced array would extend that slice forward through 
the original array.

Ideally, you should never depend on compiler-dependent details of 
implementation unless that behavior is rigorously defined in the ABI. 
With D's basic set of primitive array operations and slicing, you can do 
just about anything you need to.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/MU/S d-pu s:+ a-->? C++++$ UL+++ P--- L+++ !E W-- N++ o? K? w--- O 
M--@ V? PS PE Y+ PGP- t+ 5 X+ !R tv-->!tv b- DI++(+) D++ G e++>e 
h>--->++ r+++ y+++
------END GEEK CODE BLOCK------

James Dunne



More information about the Digitalmars-d-learn mailing list