Returning A 2D Array

Started by
6 comments, last by Protocol_0 18 years, 9 months ago
I am having problems returning a 2D array. Can anybody supply me with any help? The data type is bool. I thought that I would just do it like this:

bool *testFunc();
{
	bool test[10][10]={true, true, true, true, true, true, true, true, true, true,
                         true, false, false, false, false, false, false, false, false, true,
				         true, false, false, false, false, false, false, false, false, true,
                         true, false, false, false, false, false, false, false, false, true,
                         true, false, false, false, false, false, false, false, false, true,
                         true, false, false, false, false, false, false, false, false, true,
                         true, false, false, false, false, false, false, false, false, true,
                         true, false, false, false, false, false, false, false, false, true,
                         true, false, false, false, false, false, false, false, false, true,
                         true, true, true, true, true, true, true, true, true, true};


	return test;
}



But that doesn't work. I have been trying for hours but can't get it right. I'm going to take a guess, but is this how 2d arrays work? If this is [10][10]?

                                         Memory Address
                            0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xF            

0x00000001  pointer to ->   [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]       
0x00000002  pointer to ->   [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
0x00000003  pointer to ->   [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
0x00000004  pointer to ->   [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
0x00000005  pointer to ->   [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
0x00000006  pointer to ->   [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
0x00000007  pointer to ->   [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
0x00000008  pointer to ->   [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
0x00000009  pointer to ->   [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
0x0000000F  pointer to ->   [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
"Hard work pays off for the future; laziness pays off now"
Advertisement
test will fall out of scope in your example, so you wouldn't want to return it.

You'd need to dynamically create it with 'new.'

but if you are returning a pointer to an array you might want to return the dimensions as well.

in that case you could possibly either consider returning the information through the parameters (in/out) or returning a struct.

Returning through the parameters would be better in this case, as you woudln't want to call the function with out acquiring the pointer.

Example.

a function like so:
int * Myfunc();

could be called like

Myfunc();

and nothing is grabbing the pointer which would result in memory leak.
Hmm, I'm not sure I quite understand. How would I use "new" to dynamically create a 2D array? I know how to create a 1D, but not 2D.
"Hard work pays off for the future; laziness pays off now"
if you make the array static like this:

[source lang = "cpp]bool **testFunc();{	static bool test[10][10]={ ... };	return test;}


also you have to return a pointer to a pointer

or new

[source lang = "cpp]bool **testFunc();{        const int SIZE = 10;	bool *test = new bool*[SIZE ];        for( int i = 0 ; i < SIZE  ; ++i )            test = new bool[ SIZE ];        // fill with data	return test;}


this is ugly and you have to release the pointers using delete[]

the methid you use really depends on what you are doing with the data
//Heere is an Example

bool Array[2][2];
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
Array[j[k]=false;
bool **GetArray=Array;
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
cout<<GetArray[j][k]<<endl;

Or
bool Array[2][2];
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
Array[j[k]=false;
bool **GetArray=new bool*[2];
for(int t=0;t<2;t++)
GetArray[t]=new bool[2];
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
GetArray[j][k]=Array[j][k];
for(int t=0;t<2;t++)
delete[] GetArray[t];
delete[] GetArray;

PS i did not test it but it is something like that hope that helps.
Yaaay. I figured out another way to neatly load my 2D arrays. Thanks you all for your valued input.

rip-off: That's a good idea for your first example too. That's what I would have done if I didn't think of my new way of loading it.

In my class, the .h, I just simple defined the 2D bool array as static, and defined it through the .cpp file.

e.g
class maps  {public:	maps();	static bool    map1[10][10];	virtual ~maps();}; bool maps::map1[10][10]={true, true, true, true, true, true, true, true, true, true,                         true, false, false, false, false, false, false, false, false, true,				         true, false, false, false, false, false, false, false, false, true,                         true, false, false, false, false, false, false, false, false, true,                         true, false, false, false, false, false, false, false, false, true,                         true, false, false, false, false, false, false, false, false, true,                         true, false, false, false, false, false, false, false, false, true,                         true, false, false, false, false, false, false, false, false, true,                         true, false, false, false, false, false, false, false, false, true,                         true, true, true, true, true, true, true, true, true, true};

Thanks again.
"Hard work pays off for the future; laziness pays off now"
Probably not a direct answer to your question, but instead of creating a explicit 2d array, you can create a 1d array and use it as 2d. so you will have something like this

bool *test = new test [width*height];

and you can acesse a item by caculating the offset. Such an array can be return just as a pointer.

Yeah, I actually thought of doing something like that. But I'm the type of person who likes my code to look fancy. I have never used 2D arrays before, so I thought that it would be a good time to learn for the type of game I'm making.
"Hard work pays off for the future; laziness pays off now"

This topic is closed to new replies.

Advertisement