8 Replies - 2725 Views - Last Post: 18 June 2011 - 10:55 PM Rate Topic: -----

#1 akhal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-June 11

Passing 2D array by reference

Posted 18 June 2011 - 12:31 PM

Hi

I have to pass a 2D array by reference to a constructor of a call where it manipulates this array. I have found out one way of doing so as:
class calc{
double (*my_a)[size];
calc(double (*arr)[size] /* or double [][size] */) : arr(my_a) {}
// Passing array in main function 
double arr[size][size];
calc(arr);


It works for me for small size, but I need sizes like 1000 or 2000; when I try that it gives me "stack overflow" compile error. I do not want to send this big array by value; only by pointer or reference, Can anybody help? Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Passing 2D array by reference

#2 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Passing 2D array by reference

Posted 18 June 2011 - 12:43 PM

it looks like the issue isn't passing the array by reference, it's that your object(of type 'calc') is too big. you probably need to allocate the array on the heap. however without seeing all the code it's impossible to tell
Was This Post Helpful? 0
  • +
  • -

#3 akhal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-June 11

Re: Passing 2D array by reference

Posted 18 June 2011 - 02:11 PM

Actually I am using Threading Building Block parallelism, so actually "calc" is the body object of looping which works like any other constructor; So if you dont have any idea of TBB, still i think its only matter of passing a 2d array by reference.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: Passing 2D array by reference

Posted 18 June 2011 - 02:52 PM

Quote

It works for me for small size, but I need sizes like 1000 or 2000; when I try that it gives me "stack overflow" compile error. I do not want to send this big array by value; only by pointer or reference, Can anybody help? Thanks

As stated in a previous post the problem of "stack overflow" is caused by trying to create the arrays on the stack, ie:
const int size = 3000;
double arr[size][size]; // This is your stack overflow.

You will need to use dynamic memory, to allocate an array of this size.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 akhal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-June 11

Re: Passing 2D array by reference

Posted 18 June 2011 - 03:36 PM

And yes let me see how to use dynamic array in my case? Please show me code snippet how to do that?
Was This Post Helpful? 0
  • +
  • -

#6 akhal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 15-June 11

Re: Passing 2D array by reference

Posted 18 June 2011 - 03:50 PM

More precisely, my concerned code actually is:

class calc
{
	double my_a[][size];
public:
	calc(double a[][size], const int rows) : my_a(a) {}
    void operator()(blocked_range<int>& r) const {
        for (int k = r.begin(); k != r.end(); ++k)
            for (int j = k+1; j != r.end(); ++j)
			//some calculation and writing in my_a[k][j];
               
    }
};
// now in main() function
//initialize array a[size][size]
  static affinity_partitioner ap;
  parallel_for(blocked_range<int>(0, size, 2), calc(a, size), ap);
//now use the manipulated and updated array a[size][size] with results


I now also pass size as second parameter simply for array to be passed correctly as I saw in one example on net. but I get compilation errors with array passing.
Was This Post Helpful? 0
  • +
  • -

#7 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Passing 2D array by reference

Posted 18 June 2011 - 03:52 PM

Use a container like std::vector or read about dynamic allocation.

Also, what exactly did you mean by passing a raw array by value? You do realize, that you can do that only if you wrap it in a class/struct? Otherwise you always pass a pointer (by value) to which the array is implicitly converted.

edit: Wow, how does that work?
 double my_a[][size];

I get:
error: storage size of 'my_a' isn't known


edit: Meh, right, g++ by default allows those non-standard zero-size arrays when they are member variables. Though simply add -pedantic to get:
 warning: ISO C++ forbids zero-size array 'my_a' [-pedantic]


First, you probably shouldn't be using zero-size array, and second, even if you wouldn't - just assigning a pointer to an array makes no sense. You have to manually (in one way or another) copy the data.
What you're doing is basically:
int a[2] = { 1, 2 };
int b[2] = a; // kaboom

This post has been edited by Xupicor: 18 June 2011 - 04:03 PM

Was This Post Helpful? 0
  • +
  • -

#8 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Passing 2D array by reference

Posted 18 June 2011 - 03:52 PM

First result on Google. You are using TBB, but you don't know anything about this? Maybe you should go back and study C++ basics. Seriously, TBB provides things like cache aligned allocators and what not to, and you don't even know about the basic constructs in standard C++ itself? This can't end well.
Was This Post Helpful? 0
  • +
  • -

#9 Salem_c  Icon User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: Passing 2D array by reference

Posted 18 June 2011 - 10:55 PM

I'm getting that deja vu feeling again...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1