November 29, 2004 Re: dynamic array of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | In article <opsh8wwchn5a2sq9@digitalmars.com>, Regan Heath says... > >On Mon, 29 Nov 2004 19:45:07 +0000 (UTC), <alexander.panek@brainsware.org> wrote: >> In article <cofq5q$18dv$1@digitaldaemon.com>, Chris Sauls says... >>> >>> In article <cofnsm$1510$1@digitaldaemon.com>, >>> alexander.panek@brainsware.org >>> says... >>>> >>>> Hello, >>>> >>>> I got a problem with an array of structs in a class: when I declare >>>> the array >>>> inside the class as static, everything works fine. But as soon as I >>>> try to >>>> handle that with a dynamical array it tells me "Error: ArrayBoundsError >>>> <file(line)>". Is there no way to use dynamic arrays of structs? or >>>> did I just >>>> misused that? >>>> >>>> struct foo { >>>> char[] x; >>>> } >>>> >>>> class bar { >>>> private foo [] y; >>>> >>>> this() { foo[0].x = "hello world"; } // <--- there i get the error >>>> } >>>> >>> >>> The problem here is a simple but common one. You are attempting to >>> access the >>> first value in a set with /zero/ items. Here are a couple of correct >>> examples: >>> >>> this() { >>> foo ~= "hello world"; >>> } >>> >>> >>> this() { >>> foo.length = 1; >>> foo[0] = "hello world"; >>> } >>> >>> Even dynamic arrays have to be told their length before using them. >>> I'm fond of >>> the first method (concatenation) myself, but there are times when the >>> second >>> method is best. Experimentation will pay off here. >>> >>> -- Chris Sauls >>> >>> >> >> Well. I tried those and neither the '~='-operater helped, nor is the >> length-property available for an array of structs. Shall I use an array >> of >> pointers instead of dynamic arrays or just a pointer for that? Maybe that >> helps..gonna try. >> >> And before I forget: >> >> <code> >>>> struct foo { >>>> char[] x; >>>> } >>>> >>>> class bar { >>>> private foo [] y; >>>> >>>> this() { y[0].x = "hello world"; } // <--- there i get the error >>>> } >> </code> >> >> better? :P > >Both 'x' and 'y' are dynamic arrays. >Both require you to explicitly extend the length of them, or, use the >append operator '~'. > >Example: > >import std.stdio; > >struct foo { > char[] x; >} > >version(EXTEND) { > class bar { > private foo[] y; > this() { > foo f; > writef("EXTEND\n"); > f.x = "hello world"; > y ~= f; > } > } >} >else { > class bar { > private foo[] y; > this() { > foo f; > writef("APPEND\n"); > f.x = "hello world"; > y.length = y.length + 1; > y[y.length-1] = f; > } > } >} > >void main() >{ > bar b = new bar(); > writef(b.y[0].x,"\n"); >} > >compile with: > "dmd dynamic.d" for the append version > "dmd dynamic.d -version=EXTEND" for the extend version > >Also note that: > y[y.length-1] = f; > >copies the 'f' foo structure into the array, which isn't a problem unless the struct is large in which case it is slower than using a class (which is a reference type thus all you copy is the reference to the class, not the actual class itself). > >If you want speed and to use a struct then consider a struct pointer i.e. > >struct foo { > char[] x; >} > >class bar { > private foo*[] y; > this() { > foo *f = new foo(); > f.x = "hello world"; > y ~= f; > } >} > >Regan > >-- >Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/ I thank you very much! First really helpful message! Works now! :) Alex |
November 29, 2004 Re: dynamic array of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to alexander.panek | On Mon, 29 Nov 2004 17:56:38 +0000 (UTC), alexander.panek@brainsware.org wrote: > struct foo { > char[] x; > } > > class bar { > private foo [] y; > > this() { foo[0].x = "hello world"; } // <--- there i get the error > } This worked for me ... <code> import std.stdio; struct foo { char[] x; } class bar { private foo [] y; this() { y.length = 1; y[0].x ~= "hello world"; } } void main() { bar B = new bar; writefln("%s\n", B.y[0].x); } </code> -- Derek Melbourne, Australia 30/11/2004 9:11:42 AM |
November 29, 2004 Re: dynamic array of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to alexander.panek | In article <cofu83$1ee2$1@digitaldaemon.com>, alexander.panek@brainsware.org says... > >Well. I tried those and neither the '~='-operater helped, nor is the length-property available for an array of structs. Shall I use an array of pointers instead of dynamic arrays or just a pointer for that? Maybe that helps..gonna try. > >Thanks, Alex > Both the concat. operator and length prop. will work Ok like so: >import std.stream; >import std.string; > >struct foo { > char[] x; >} > >class bar { > private foo[] y; > this() > { > y ~= new foo[1]; > y[0].x = "hello world"; > } > this(int x) > { > y.length = x; > foreach(int idx, inout foo f; y) { > f.x = "hello world (" ~ std.string.toString(idx) ~ ")"; > } > } >} > >void main() >{ > { > bar B = new bar; > stdout.writefln(B.y[0].x); > } > { > bar B = new bar(100); > foreach(foo f; B.y) { > stdout.writefln(f.x); > } > } >} - Dave |
November 30, 2004 Re: dynamic array of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | Derek Parnell wrote:
> class bar {
> private foo [] y;
>
> this() {
> y.length = 1;
> y[0].x ~= "hello world"; }
> }
Wouldn't
y[0].x = "hello world";
work just as well, since x is empty?
|
November 30, 2004 Re: dynamic array of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Russ Lewis | On Mon, 29 Nov 2004 17:26:35 -0700, Russ Lewis wrote: > Derek Parnell wrote: >> class bar { >> private foo [] y; >> >> this() >> { >> y.length = 1; >> >> y[0].x ~= "hello world"; >> } >> } > > Wouldn't > y[0].x = "hello world"; > work just as well, since x is empty? Yep, sure would. I guess I was just being a bit paranoid wanting to ensure that a copy of the string was assigned rather than just a reference. But a simple '=' does that in this context anyway. -- Derek Melbourne, Australia 30/11/2004 2:13:15 PM |
November 30, 2004 Re: dynamic array of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to alexander.panek | alexander.panek@brainsware.org wrote: <snip> [ArrayBoundsError] > struct foo { > char[] x; > } > > class bar { > private foo [] y; > > this() { foo[0].x = "hello world"; } // <--- there i get the error > } <snip> Unless I'm missing something, it can only be a bug that this compiles. What does DMD think it means? (I haven't tested it in 0.107 yet.) Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
November 30, 2004 Re: dynamic array of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | On Tue, 30 Nov 2004 10:28:39 +0000, Stewart Gordon <smjg_1998@yahoo.com> wrote: > alexander.panek@brainsware.org wrote: > <snip> > [ArrayBoundsError] >> struct foo { >> char[] x; >> } >> class bar { >> private foo [] y; >> this() { foo[0].x = "hello world"; } // <--- there i get the error >> } > <snip> > > Unless I'm missing something, it can only be a bug that this compiles. What does DMD think it means? > > (I haven't tested it in 0.107 yet.) > > Stewart. > 1.07 gives: bugtest.d(8): no [] operator overload for type foo -- "Unhappy Microsoft customers have a funny way of becoming Linux, Salesforce.com and Oracle customers." - www.microsoft-watch.com: "The Year in Review: Microsoft Opens Up" "Clearly, if I were one of that Predator hunting team, I would file a complaint with my local member asking why such an unsafe environment should be allowed to continue, tradition or not." - http://www.sydneyanglicans.net/culture/watching/alien_vs_predator/ |
November 30, 2004 Re: dynamic array of structs? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | Derek Parnell wrote:
> On Mon, 29 Nov 2004 17:26:35 -0700, Russ Lewis wrote:
>>Wouldn't
>> y[0].x = "hello world";
>>work just as well, since x is empty?
>
> Yep, sure would. I guess I was just being a bit paranoid wanting to ensure
> that a copy of the string was assigned rather than just a reference. But a
> simple '=' does that in this context anyway.
Well, if you really want to make sure that you are assiging a copy, then
y[0].x = "hello world".dup;
should work.
|
Copyright © 1999-2021 by the D Language Foundation