PDA

View Full Version : Best method for passing multi dimentional arrays


moomin
07-22-2003, 01:59 AM
I've seen a few different ways some with more advantages than others, but there isn't really a good way of doing it with references (Unless you specify one of the column max length values)


The only acceptable method so far is using a pointer-to-pointer method


i.e.

GridNodes myNodes = new GFridNodes[maxSize][maxSize];

DoSomething(myNodes,maxSize);



void DoSomething function (GridNodes **nodeArray, int maxSize)
{
int i,j;

for (i=0;i<maxSize;i++)
{
for(j=0;j<maxSize;j++)
{

GridNodes[i][j] = callSomFunction();
}
}
}





anyone any better ideas?

UnknownStranger
07-22-2003, 02:03 AM
Well, I like the ** approach, and I don't see any problems using it...

moomin
07-22-2003, 02:16 AM
I was just curious if there was a neat way of using references, but doesn't look like it :{

davepermen
07-22-2003, 02:43 AM
i never use multidimensional arrays.. i always access them manually as such..

good thing: if you don't care about width/height/depth of them, you can simply iterate linearly through them, performing your task.. best to optimize later.. :D

baldurk
07-22-2003, 10:20 AM
what if the dimensions aren't the same?

davepermen
07-22-2003, 04:39 PM
hm? what do you mean?

fringe
07-23-2003, 06:27 AM
Hey Guys,

If you are using c++ you could write an object thatyou could pass round by reference. This could have as many dimensions as you wish and there the object can check for overflows andwrong dimensionality etc.

fringe

baldurk
07-23-2003, 10:37 AM
davepermen: his code assumes it's a "square" array, what if the dimensions were 4 and 6?

fringe: you could do that in C, but sometimes it is a bit of a waste.

davepermen
07-23-2003, 02:39 PM
ahh okay, you where not talking about me:D

hm, fringe.. the per access bound checking is a terrible idea imho.. it is just sooooo slow:D

DrunkenCoder
07-29-2003, 11:12 PM
for jagged arrays use a vector of vectors and for uniform arrays write a wrapper

davepermen
07-30-2003, 06:18 AM
Wrapper in Code Snippets (http://www.devmaster.net/forums/index.php?act=ST&f=35&t=423)

donBerto
07-31-2003, 01:57 PM
...or you can just use everybody's favorite mechanism here on devmaster: a TEMPLATED container!

edit: i see davepermen already beat me to it.

:yes:

davepermen
08-03-2003, 01:21 PM
hehe, i try my best:D