5 Replies - 1863 Views - Last Post: 22 September 2012 - 09:47 AM Rate Topic: -----

#1 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

How can I return a dynamically allocated multi-dimensional array?

Posted 22 September 2012 - 08:50 AM

Ok, so here's a simple question, I hope at least. I have a problem, I have a multi-Dimensional array of integers, int array[20][20]. However, I need it so the user can define different sizes. Thats the easy part. I have a function like so:
int *arrayAddress

int* setArray(int x, int y)
{
   int tempArray[x][y];
   for(int xIndex = 0;  xIndex < x;  xIndex++)
   {
      for(int xIndex = 0;  xIndex < x;  xIndex++)
      {
          /*just some sample code so I can play around with the semantics, obviously would be changed so the program serves a purpose*/
          tempArray[xIndex][yIndex] = xIndex * yIndex;
      }
   }

   return tempArray;
}

void main()
{
   arrayAddress = setArray(20, 20);
   /*NOW, HOW WOULD I GO ABOUT USING THE ARRAY, SO I CAN DO cout << arrayAddress[10][2] OR SOMETHING?
   ONE MORE ISSUE IS THAT I NEED TO DELETE THE ARRAY, SO THAT A NEW ONE CAN BE ALLOCATED.  HOW WOULD 
   I DO THIS?*/
}



The last comment speaks for itself. How would I go about using the element in the array [10][2], and how would i go about deleting the array so I can allocate a new one? I know, this is probably a simple question, but I don't know how I would do it. Also, I don't want to use vectors, as they tend to be hard to work with. Thanks in advance!

Is This A Good Question/Topic? 0
  • +

Replies To: How can I return a dynamically allocated multi-dimensional array?

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4096
  • View blog
  • Posts: 12,674
  • Joined: 25-December 09

Re: How can I return a dynamically allocated multi-dimensional array?

Posted 22 September 2012 - 09:09 AM

Well since you mention vector, I'm going to assume you are writing a C++ program. So in the following snippet:
int* setArray(int x, int y)
{
   int tempArray[x][y];


You should get a compile error. array sizes in a C++ program must be compile time constants. Variable Length Arrays are not allowed in C++, if you want this functionality use std::vector. Now if C++ did allow VLA you wouldn't be able to return this array from your function, it is a local variable and it is deleted when you return from the function. To do this you need to use new/delete, but I recommend you learn vectors.

Quote

ONE MORE ISSUE IS THAT I NEED TO DELETE THE ARRAY, SO THAT A NEW ONE CAN BE ALLOCATED.

Basically you don't resize static arrays in C++.

For all the functionality you have described a vector is the best choice in C++. Let the compiler do the messy memory allocations/deallocations for you. I really recommend you learn to properly use vectors, they will save you quite a few headaches.

Also in C++ main must be defined to return an int, and you should return an int from this function:

int main()
{

   return 0;
}



Jim
Was This Post Helpful? 1
  • +
  • -

#3 zehawk  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 213
  • Joined: 23-February 12

Re: How can I return a dynamically allocated multi-dimensional array?

Posted 22 September 2012 - 09:20 AM

Hmm, well, I basically just wrote that up real fast to test it, so yeah, main was supposed to have an int. However, you are wrong. Writing that in gcc doesn't give me an error, in fact, it doesn't even give me a warning. I guess I'll have to use vectors, I was hoping not too as vectors can be somewhat confusing. I'll get on it right away. However, if anyone else can tell me how to solve it using arrays, i'd be one really happy person.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2999
  • View blog
  • Posts: 10,380
  • Joined: 08-August 08

Re: How can I return a dynamically allocated multi-dimensional array?

Posted 22 September 2012 - 09:29 AM

Turn up your warnings. If that doesn't work, get another compiler.
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is online

  • member icon


Reputation: 4096
  • View blog
  • Posts: 12,674
  • Joined: 25-December 09

Re: How can I return a dynamically allocated multi-dimensional array?

Posted 22 September 2012 - 09:34 AM

Quote

However, you are wrong. Writing that in gcc doesn't give me an error, in fact, it doesn't even give me a warning.

No, that is because you are allowing g++ to use extensions to the language, which I don't recommend. Plus you probably need to increase the number of warnings g++ is generating. What compile switches are you using? In my opinion you should be using at least -Wall -Wextra -pedantic -ansi and also use the -g to include debugging information.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: How can I return a dynamically allocated multi-dimensional array?

Posted 22 September 2012 - 09:47 AM

View Postzehawk, on 22 September 2012 - 05:20 PM, said:

Hmm, well, I basically just wrote that up real fast to test it, so yeah, main was supposed to have an int. However, you are wrong. Writing that in gcc doesn't give me an error, in fact, it doesn't even give me a warning. I guess I'll have to use vectors, I was hoping not too as vectors can be somewhat confusing. I'll get on it right away. However, if anyone else can tell me how to solve it using arrays, i'd be one really happy person.

vectors shouldn't be confusing if you understand the concept of an array; Everything that an array can do has a direct equivalent with a vector (vectors are capable of replacing arrays for pretty much everything that you could ever want to do with a "raw" array)

The only real differences in usage is are minor syntactical ones; the way of thinking is otherwise identical. (vector has additional features which aren't possible using raw arrays, but you are not required to use any of them)

e.g. array versus vector

declare a 1D array of int:
int fred[10]; vs vector<int> fred(10);

declare a 2D-array (array-of-arrays) of int:
int fred[10][10]; vs vector< vector<int> > fred(10, 10);

1D array index access:
fred[0]; vs fred.at(0); or fred[0];

2D array index access:
fred[0][0]; vs fred.at(0).at(0); or fred[0][0];

1D array sorting:
std::sort( fred, fred+10 ); vs std::sort( fred.begin(), fred.end() );

Passing arrays as function arguments "by-reference"
void func( int fred[], int size ); vs void func( vector<int>& fred );


Whatever you wish to do with an array is generally always as easy using a vector, and often easier (such as returning from a function, copying, resizing the array and getting its size)

This post has been edited by Bench: 22 September 2012 - 09:48 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1