array trouble

Jan Hanselaer jan.hanselaer at gmail.com
Tue May 1 01:04:26 PDT 2007


Hi all,

When looking in to the documentation of arrays I found something that 
appeared rather "buggy" to me.
In the documentation you can read this (it's an example to show how changing 
the size of one array could affect the contents of another, namely when the 
one is a slice of the other, her in the example b is a slice of a):

char[] a = new char[20];
char[] b = a[0..10];
char[] c = a[10..20];

b.length = 15; // always resized in place because it is sliced
                    // from a[] which has enough memory for 15 chars

Now I have 2 questions:
1) How come that when I want to print al chars from b I get an error 
(Error: 4invalid UTF-8 sequence)
Normaly I think that the new elements 10..15 from b should get the default 
char, and then there would be no error.

2) Like they say in the documentation, also array a should be affected but 
that doesn't seem to be true because I can still print the 20 original 
chars.

Now to make it a little bit stranger, when I try all this with ints instead 
of chars it works like they say in the documentation. The new elements from 
b get default int 0 and when I print the 3 arrays, I see they are all 
effected by the change of length in b.

Can anyone explain all this? Am I just overlooking something?
My test code for char and int is in the attachment.

Thanks in advance
Jan 


begin 666 array_chars.d
M:6UP;W)T('-T9"YS=&1I;SL-"@T*<')I=F%T92 Z#0H)8VAA<EM=(&$[#0H)
M8VAA<EM=(&([#0H)8VAA<EM=(&,[#0H-"G9O:60@<')I;G1A<G)A>7,H*0T*
M>PT*"7=R:71E9FQN*")<;D%R<F%Y(&$@.B @(BD[#0H)9F]R96%C:"AC:#L@
M82D-"@D)=W)I=&5F*"(E<R B+&-H*3L-"@EW<FET969L;B at B7&Y!<G)A>2!B
M(#H@("(I.PT*"69O<F5A8V at H8V@[(&(I#0H)"7=R:71E9B at B)7,@(BQC:"D[
M#0H)=W)I=&5F;&XH(EQN07)R87D at 8R Z(" B*3L-"@EF;W)E86-H*&-H.R!C
M*0T*"0EW<FET968H(B5S("(L8V at I.PT*?0T*#0II;G0@;6%I;B at I#0I[#0H)
M82 ](")A8F-D969G:&EJ:VQM;F]P<7)S='5V(CL-"@EB(#T at 85LP+BXQ,%T[
M#0H)8R ](&%;,3 N+C(P73L)#0H)<')I;G1A<G)A>7,H*3L-"@T*"6(N;&5N
M9W1H(#T@,34[#0H)<')I;G1A<G)A>7,H*3L-"@T*(" @<F5T=7)N(# [#0I]
`
end

begin 666 array_ints.d
M:6UP;W)T('-T9"YS=&1I;SL-"@T*<')I=F%T92 Z#0H):6YT6UT at 83L-"@EI
M;G1;72!B.PT*"6EN=%M=(&,[#0H-"G9O:60@<')I;G1A<G)A>7,H*0T*>PT*
M"7=R:71E9FQN*")<;D%R<F%Y(&$@.B @(BD[#0H)9F]R96%C:"AC:#L at 82D-
M"@D)=W)I=&5F*"(E9" B+&-H*3L-"@EW<FET969L;B at B7&Y!<G)A>2!B(#H@
M("(I.PT*"69O<F5A8V at H8V@[(&(I#0H)"7=R:71E9B at B)60@(BQC:"D[#0H)
M=W)I=&5F;&XH(EQN07)R87D at 8R Z(" B*3L-"@EF;W)E86-H*&-H.R!C*0T*
M"0EW<FET968H(B5D("(L8V at I.PT*?0T*#0II;G0@;6%I;B at I#0I[#0H@("!A
M(#T at 6R Q+#(L,RPT+#4L-BPW+#@L.2PQ,"PQ,2PQ,BPQ,RPQ-"PQ-2PQ-BPQ
M-RPQ."PQ.2PR,"!=.PT*"6(@/2!A6S N+C$P73L-"@EC(#T at 85LQ,"XN,C!=
M.PD-"@EP<FEN=&%R<F%Y<R at I.PT*#0H)8BYL96YG=&@@/2 Q-3L-"@EP<FEN
==&%R<F%Y<R at I.PT*#0H)<F5T=7)N(# [#0I]#0H`
`
end



More information about the Digitalmars-d-learn mailing list