Thread overview | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
September 05, 2005 dynamic multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Hello. I just started to learn the language. Now, I am trying to use a dynamic two-dimensional array. The compiler accepted the following line without a problem: picture.length = (width), (height); However, running the binary resulst in Win32 exception error. I have read the manual and the forum for an hour or so, but found no answer. |
September 05, 2005 Re: dynamic multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ald | Ald wrote:
> Hello. I just started to learn the language.
> Now, I am trying to use a dynamic two-dimensional array. The compiler accepted
> the following line without a problem:
>
> picture.length = (width), (height);
>
> However, running the binary resulst in Win32 exception error.
> I have read the manual and the forum for an hour or so, but found no answer.
>
>
You can use a simple foreach-loop:
int[][] table
table.length = height;
foreach(int[] row; table) row.length = width;
|
September 05, 2005 Re: dynamic multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jari-Matti Mäkelä | >You can use a simple foreach-loop:
>
>int[][] table
>
>table.length = height;
>foreach(int[] row; table) row.length = width;
Thank you, it works now.
...
Do you expect people to thank for advice or do you consider it to be unnecessary
and redundant messages on the board?
|
September 05, 2005 Re: Is 'thanks' necessary or not? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ald | In article <dfigu9$2lj1$1@digitaldaemon.com>, Ald says... > >Do you expect people to thank for advice or do you consider it to be unnecessary and redundant messages on the board? > Ald, My thoughts are, "If the question you asked was important enough to post for others to read, and then someone took the time to reply back to you with some very helpful information...wouldn't posting a thanks be the right thing to do?" And as far as I know, this forum doesn't have a rule to whether it's necessary or not to post a thanks, it's lefted up to each person to decide. But just because we're on the web, it doesn't mean we can forget our manners. Afterall, this is an International Community, plus "these posts may be the only way other people can discover who you are...so be mindful." :) David L. ------------------------------------------------------------------- "Dare to reach for the Stars...Dare to Dream, Build, and Achieve!" ------------------------------------------------------------------- MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html |
September 06, 2005 Re: dynamic multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ald | Ald wrote:
>>You can use a simple foreach-loop:
>>
>>int[][] table
>>
>>table.length = height;
>>foreach(int[] row; table) row.length = width;
>
>
> Thank you, it works now.
>
> ...
> Do you expect people to thank for advice or do you consider it to be unnecessary
> and redundant messages on the board?
>
>
It's good practice to respond with a thanks or otherwise because it let's the other person know that their suggestion was useful in solving your problem.
Feedback is almost always a good idea.
-JJR
|
September 06, 2005 Re: dynamic multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ald | Ald wrote: > Hello. I just started to learn the language. > Now, I am trying to use a dynamic two-dimensional array. The compiler accepted > the following line without a problem: > > picture.length = (width), (height); > > However, running the binary resulst in Win32 exception error. > I have read the manual and the forum for an hour or so, but found no answer. > > In this statement you do following: "assign to picture's length result of comma expression". result of comma expression is result of rightmost expression among expressions separated by comma. So in other words you wrote equivalent to: picture.length = height; Exception is consequence of value of `height`, I think it was negative. For me following snipet compiles and runs as expected: module core; void main() { uint width = 5; uint height = 15; int[][] a; a.length = (width), (height); } -- Victor (aka nail) Nakoryakov nail-mail<at>mail<dot>ru Krasnoznamensk, Moscow, Russia |
September 06, 2005 Re: dynamic multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ald | On Tue, 06 Sep 2005 00:30:34 +0200, Ald <Ald_member@pathlink.com> wrote: >> You can use a simple foreach-loop: >> >> int[][] table >> >> table.length = height; >> foreach(int[] row; table) row.length = width; > > Thank you, it works now. > > ... > Do you expect people to thank for advice or do you consider it to be unnecessary > and redundant messages on the board? Not a big deal, but you probably want to know that there is `D.learn' list where probably-simple-to-answer questions should be send. -- Dawid Ciężarkiewicz |
September 06, 2005 Re: dynamic multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ald | In article <dfigu9$2lj1$1@digitaldaemon.com>, Ald says... > >>You can use a simple foreach-loop: >> >>int[][] table >> >>table.length = height; >>foreach(int[] row; table) row.length = width; > >Thank you, it works now. > Strange, it doesn't work for me (DMD 0.129, Win XP). # void main () # { # size_t height = 4; # size_t width = 3; # int[][] table; # # table.length = height; # foreach(int[] row; table) { # row.length = width; # } # table[0][0] = 0; # } throws ArrayBoundsError. What works for me is: # void main () # { # size_t height = 4; # size_t width = 3; # int[][] table; # # foreach(size_t idx, int[] row; table) { # table[idx].length = width; # } # table[0][0] = 0; # } Kind regards, Stefan |
September 06, 2005 Re: dynamic multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan | In article <dfl79t$1u38$1@digitaldaemon.com>, Stefan says... > >In article <dfigu9$2lj1$1@digitaldaemon.com>, Ald says... >> >>>You can use a simple foreach-loop: >>> >>>int[][] table >>> >>>table.length = height; >>>foreach(int[] row; table) row.length = width; >> >>Thank you, it works now. >> > >Strange, it doesn't work for me (DMD 0.129, Win XP). > ># void main () ># { ># size_t height = 4; ># size_t width = 3; ># int[][] table; ># ># table.length = height; ># foreach(int[] row; table) { ># row.length = width; ># } ># table[0][0] = 0; ># } > >throws ArrayBoundsError. > >What works for me is: > ># void main () ># { ># size_t height = 4; ># size_t width = 3; ># int[][] table; ># ># foreach(size_t idx, int[] row; table) { ># table[idx].length = width; ># } ># table[0][0] = 0; ># } I think it should be (note the "inout)": # void main () # { # size_t height = 4; # size_t width = 3; # int[][] table; # # table.length = height; # foreach(inout int[] row; table) { # row.length = width; # } # table[0][0] = 0; # } Am I right? Regards, Stefan |
September 06, 2005 Re: dynamic multidimensional arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan | On Tue, 6 Sep 2005 23:15:49 +0000 (UTC), Stefan <Stefan_member@pathlink.com> wrote:
> In article <dfl79t$1u38$1@digitaldaemon.com>, Stefan says...
>>
>> In article <dfigu9$2lj1$1@digitaldaemon.com>, Ald says...
>>>
>>>> You can use a simple foreach-loop:
>>>>
>>>> int[][] table
>>>>
>>>> table.length = height;
>>>> foreach(int[] row; table) row.length = width;
>>>
>>> Thank you, it works now.
>>>
>>
>> Strange, it doesn't work for me (DMD 0.129, Win XP).
>>
>> # void main ()
>> # {
>> # size_t height = 4;
>> # size_t width = 3;
>> # int[][] table;
>> #
>> # table.length = height;
>> # foreach(int[] row; table) {
>> # row.length = width;
>> # }
>> # table[0][0] = 0;
>> # }
>>
>> throws ArrayBoundsError.
>>
>> What works for me is:
>>
>> # void main ()
>> # {
>> # size_t height = 4;
>> # size_t width = 3;
>> # int[][] table;
>> #
>> # foreach(size_t idx, int[] row; table) {
>> # table[idx].length = width;
>> # }
>> # table[0][0] = 0;
>> # }
>
> I think it should be (note the "inout)":
>
> # void main ()
> # {
> # size_t height = 4;
> # size_t width = 3;
> # int[][] table;
> #
> # table.length = height;
> # foreach(inout int[] row; table) {
> # row.length = width;
> # }
> # table[0][0] = 0;
> # }
>
> Am I right?
I think so.
Regan
|
Copyright © 1999-2021 by the D Language Foundation