Jump to content
  • Advertisement
Sign in to follow this  
Protocol_0

Returning A 2D Array

This topic is 4888 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 ->   [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
//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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!