7 Replies - 5873 Views - Last Post: 02 June 2009 - 03:29 AM Rate Topic: -----

#1 yalcin67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-June 09

How to return a multidimensional array with a pointer function in C++

Posted 01 June 2009 - 01:50 AM

Hello,

I would like to return a multidimensional array from a pointer function in Visual C++.But multidimensional array dimension's will be enormous.For example,width will be 500 and height will be 600.

Could you help me please?

This post has been edited by yalcin67: 01 June 2009 - 01:54 AM

Is This A Good Question/Topic? 0
  • +

Replies To: How to return a multidimensional array with a pointer function in C++

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to return a multidimensional array with a pointer function in C++

Posted 01 June 2009 - 02:48 AM

I'm not sure I understand your question.

Are you creating the array in the function of are you declaring it in main and then passing a pointer to that function into the function?

If you are just pointing to an array then it matters not how big it is since you won't be copying it around, just passing the directions for where it is in memory around.

I suspect we have a major failure to communicate here.

Could you explain in greater length and detail what it is you are trying to do. Ideally provide an example of your code trying to do this. It doesn't matter if it doesn't work. But if you give us some code and an explanation of what you think it does, or what it should do, you will maximise your chances of getting useful help.
Was This Post Helpful? 0
  • +
  • -

#3 joske  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 43
  • View blog
  • Posts: 297
  • Joined: 04-September 07

Re: How to return a multidimensional array with a pointer function in C++

Posted 01 June 2009 - 03:03 AM

here a simple example of a function returning a 2D array. Not sure if this is what you mean. You can change the width and height to 500 and 600, that's no problem.

/**
Creating a 2D array
*/

#include <cstdio>


// Returns a pointer to a newly created 2d array
// the array has size [height x width]
int** createArray(unsigned height, unsigned width)
{
  int** array = 0;
  array = new int*[height];

  for (int h = 0; h < height; h++)
  {
	array[h] = new int[width];

	for (int w = 0; w < width; w++)
	{
	  // fill in some initial values
	  // (filling in zeros would be more logic, but this is just for the example)
	  array[h][w] = w + width * h;
	}
  }

  return array;
}

int main()
{
  printf("Creating a 2D array\n");
  printf("\n");

  int height = 15;
  int width = 10;
  int** myArray = createArray(height, width);
  printf("Array sized [%i,%i] created.\n\n", height, width);

  // print contents of the array
  printf("Array contents: \n");
  for (int h = 0; h < height; h++)
  {
	for (int w = 0; w < width; w++)
	{
	  printf("%i,", myArray[h][w]);
	}
	printf("\n");
  }

  // important: clean up memory
  printf("\n");
  printf("Cleaning up memory...\n");
  for (int h = 0; h < height; h++)
  {
	delete [] myArray[h];
  }
  delete [] myArray;
  myArray = 0;
  printf("Ready.\n");

  return 0;
}


This post has been edited by joske: 01 June 2009 - 03:06 AM

Was This Post Helpful? 1
  • +
  • -

#4 yalcin67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-June 09

Re: How to return a multidimensional array with a pointer function in C++

Posted 01 June 2009 - 03:23 AM

Thanks for your help.In fact I have wanted to do this operation with another way.Its input parameter was one dimensional array,width and height output parameter was two dimensional array.And I defined two pointer result1 and result2 which was assigned multidimensional array.

I showed my code example below:

double  **function( array< double>^ data,int width,int height ) 

  {int i;
  //define returning variable that point multidimensional array
  double **R;
 // memory allocation for R
 R=new double *[height];
 for (i=0;i<height;i++)
 {
 R[i]=new double [width];
 }

  // ....


  return R;
 }


  int main( void ) {
  int M=2;
  int N=10;
  int  i,j;
  // define multimensional array 2x10

  array< array< double >^ >^ input = gcnew array< array< double >^ >(M);

for (j=0;j<input->Length;j++){
input[j]=gcnew array<double>(N);}

// define result1 and result 2 pointers and memory allocate  for these  variables

 double **result1;
 result1 = new double *[N];
 for(i=0;i<N;i++)
 {result1[i]=new double [M];}

 double **result2;
 result2 = new double *[N];
 for(i=0;i<N;i++)
 {result2[i]=new double [M];}

 //............


// send first row array of multidimensional array to function

 result1=function(input[0],M,N)

// send second row array of multidimensional array to function

 result2=function(input[1],M,N)

// delete  result1 and result2
for (i=0;i<N;i++)
{delete R[k];}
delete R;}

 return 0;
 }


I built this program succesfully in Visual Studio 2008.When I debug this code,the program computed result1 pinter variable but during computing result2 in the function here:

R=new double *[height];
for (i=0;i<height;i++)
 {
 R[i]=new double [width];
 }


Visual Studio give this error:

An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in stdeneme.exe Additional information: External component has thrown an exception.
Unfortunately I can't understand this error. How can I overcome of this problem?Could you help me please?

This post has been edited by yalcin67: 01 June 2009 - 03:24 AM

Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,686
  • Joined: 16-October 07

Re: How to return a multidimensional array with a pointer function in C++

Posted 01 June 2009 - 04:20 AM

View Postyalcin67, on 1 Jun, 2009 - 04:23 AM, said:

array< array< double >^ >^ input = gcnew array< array< double >^ >(M);



Only in managed C++. And then, why even bother with C++. That is some seriously ugly syntax.

For real C++, arrays are a pain because they usually don't know how big they are. joske offered the acceptable way to dynamically create a 2D array, which results in a "jagged" array. Another way is to create a one dimensional array and do the two D yourself, which is my preference.

However, you're in the wonderful world of C++. You can self contain and manage everything in a class. Get the benefits of a simple array with multidimensional access. Hell, add some templating for whatever type you wish to use.

Here's a simple example. I'd normally include a friend to ostream, and some clear and fill functionality to such a class, but that's an exercise for the reader.

#include <iostream>
using namespace std;

template<class T>
class Array2D {
protected:
	T *data;
public:
	const unsigned rows, cols, size;
	Array2D(unsigned r, unsigned c) : rows®, cols©, size(r*c) {
		data = new T[size];
	}
	~Array2D() { delete data; }
	void setValue(unsigned row, unsigned col, T value) { 
		data[(row*cols)+col] = value;
	}
	T getValue(unsigned row, unsigned col) const {
		return data[(row*cols)+col];
	}
};

void load(Array2D<int> &a) {
	for(int row=0; row<a.rows; row++) {
		for(int col=0; col<a.cols; col++) {
			a.setValue(row, col, (row+1)*(col+1));
		}
	}
}

void print(Array2D<int> &a) {
	for(int row=0; row<a.rows; row++) {
		for(int col=0; col<a.cols; col++) {
			cout << a.getValue(row, col) << '\t';
		}
		cout << endl;
	}
}


int main() {
	Array2D<int> array(3,5);
	load(array);
	print(array);
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#6 yalcin67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-June 09

Re: How to return a multidimensional array with a pointer function in C++

Posted 01 June 2009 - 07:18 AM

Thank for your help baavgai.I tried your sample code. In your code here:

View Postbaavgai, on 1 Jun, 2009 - 03:20 AM, said:

Array2D(unsigned r, unsigned c) : rows, cols, size(r*c) {
data = new T[size];
}


Visual Studio 2008 gave error I couldn't understand why Visual Studio gave error.

Do you have a comment about this?

Best regards..
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to return a multidimensional array with a pointer function in C++

Posted 02 June 2009 - 02:52 AM

What error did VS give you?
Was This Post Helpful? 0
  • +
  • -

#8 yalcin67  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 01-June 09

Re: How to return a multidimensional array with a pointer function in C++

Posted 02 June 2009 - 03:29 AM

Thanks for your interest .I solved the error .If I change baavgai's code like this:



	Array2D(unsigned r, unsigned c) : rows(r), cols(c), size(r*c) {
		data = new T[size];
	}


VS doesn't give error
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1