www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - dynamic multidimensional arrays

reply Ald <Ald_member pathlink.com> writes:
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.
Sep 05 2005
next sibling parent reply =?ISO-8859-1?Q?Jari-Matti_M=E4kel=E4?= <jmjmak invalid_utu.fi> writes:
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;
Sep 05 2005
parent reply Ald <Ald_member pathlink.com> writes:
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?
Sep 05 2005
next sibling parent David L. Davis <SpottedTiger yahoo.com> writes:
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
Sep 05 2005
prev sibling next sibling parent John Reimer <terminal.node gmail.com> writes:
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
Sep 05 2005
prev sibling next sibling parent =?utf-8?B?RGF3aWQgQ2nEmcW8YXJraWV3aWN6?= <araelx gmail.com> writes:
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
Sep 06 2005
prev sibling parent reply Stefan <Stefan_member pathlink.com> writes:
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
Sep 06 2005
parent reply Stefan <Stefan_member pathlink.com> writes:
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
Sep 06 2005
parent "Regan Heath" <regan netwin.co.nz> writes:
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
Sep 06 2005
prev sibling parent Victor Nakoryakov <nail-mail mail.ru> writes:
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
Sep 06 2005