Jump to content


Best method for passing multi dimentional arrays


12 replies to this topic

#1 moomin

    Member

  • Members
  • PipPip
  • 44 posts

Posted 22 July 2003 - 08: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?

#2 UnknownStranger

    Valued Member

  • Members
  • PipPipPip
  • 139 posts

Posted 22 July 2003 - 09:03 AM

Well, I like the ** approach, and I don't see any problems using it...
M.E.
-----
"Human stupidity is something you can rely on." -- M.A.
"I didn't design life." -- J.G.
"It's almost finished..." -- EHD

#3 moomin

    Member

  • Members
  • PipPip
  • 44 posts

Posted 22 July 2003 - 09:16 AM

I was just curious if there was a neat way of using references, but doesn't look like it :{

#4 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 22 July 2003 - 09: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
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#5 baldurk

    Senior Member

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 22 July 2003 - 05:20 PM

what if the dimensions aren't the same?
baldurk
He who knows not and knows that he knows not is ignorant. Teach him.
He who knows not and knows not that he knows not is a fool. Shun him.

#6 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 22 July 2003 - 11:39 PM

hm? what do you mean?
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#7 fringe

    Member

  • Members
  • PipPip
  • 44 posts

Posted 23 July 2003 - 01:27 PM

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

#8 baldurk

    Senior Member

  • Members
  • PipPipPipPip
  • 1057 posts

Posted 23 July 2003 - 05:37 PM

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.
baldurk
He who knows not and knows that he knows not is ignorant. Teach him.
He who knows not and knows not that he knows not is a fool. Shun him.

#9 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 23 July 2003 - 09: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
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#10 DrunkenCoder

    Member

  • Members
  • PipPip
  • 97 posts

Posted 30 July 2003 - 06:12 AM

for jagged arrays use a vector of vectors and for uniform arrays write a wrapper

#11 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 30 July 2003 - 01:18 PM

Wrapper in Code Snippets
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....

#12 donBerto

    Senior Member

  • Members
  • PipPipPipPip
  • 369 posts

Posted 31 July 2003 - 08: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:
Imagine.

#13 davepermen

    Senior Member

  • Members
  • PipPipPipPip
  • 1306 posts

Posted 03 August 2003 - 08:21 PM

hehe, i try my best:D
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users