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

Recommended Answers

All 11 Replies

I'm not sure what code is inside your class calc and what isn't, and what my_a is, so I won't comment on your code. If you have a 2-D array and you need a function that takes a 2-D array and that function manipulates array elements, here's an example...

#include <iostream>
#include <cstring>
#include <ctime>
#include <cstdlib>
using namespace std;


void foo(int arr[][10], const int num_rows)
{
    arr[5][2]++;
}


int main()
{
     int a[20][10];
     srand(time(NULL));
     a[5][2] = rand() % 10;
     cout << a[5][2] << endl;
     foo(a, 20);
     cout << a[5][2] << endl;
     cin.get();
     return 0;
}

"calc" object is actually body object of threading building block's parallel_for. So Its not different than any other class as far as passing array for manipulation is concerned. I tried as you suggested but at constructor I got this error:"cannot specify explicit inializer for arrays" and at manipulating this array inside that body object gives error:"l-values specifies const object".

it gives me "stack overflow" compile error.

I assume you mean run-time error?

I tried as you suggested but at constructor I got this error:"cannot specify explicit inializer for arrays"

You'll have to post some code. Impossible to comment on what you tried till I see it.

Okay, my concerned code 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 think now I followed as you suggested in earlier example, but I get compilation errors with array passing.

If you're getting a stack overflow error it means that your array is too big to be allocated on the stack.
It has nothing to do with passing it to the function. Try using a dynamically allocated array instead.
A std::vector<std::vector<double> > is a good alternative too.

Also, this...

class calc
{
    double my_a[][size];

    //...
};

...may be legal, but it doesn't do what you think it does.

#include <iostream>

struct Foo { int array[][100]; };

int main() { std::cout << sizeof(Foo) << std::endl; }

I've never been real clear on what this type of initializer does (see red). I never use it. Perhaps I should learn. So I can be of little to no help regarding what you did wrong and how to do it right.

calc(double a[][size], const int rows) : my_a(a)

I can possibly suggest a fix though. I guess you're trying to do a deep copy of the passed array to your object's array?

calc(double a[][size], const int rows)
{
    memcpy(&(my_a[0][0]), &(a[0][0]), rows * size * sizeof(double));

    // the other code
}

I imagine that'll solve any compile errors. Stack overflow errors? Not sure on that one.

Dozier: oh thank you but thats not what I actually want, I only want a reference there to modify this array so that I dont have to return this array back, and my original array is updated by that class object. Thats why I ask passing array by reference!!

I only want a reference there to modify this array so that I dont have to return this array back

Well, VernonDozier's first post in this thread shows you exactly how to do that.
You don't have to use any special syntax. This is the default behaviour for arrays.

Also, let me remind you, if you're getting a stack overflow error, the problem is
the declaration of the array. It has nothing to do with passing it around to functions.

What is the size of the array that you are using? Are you sure you never try to read past the end of the array? If the array is very large I would suggest putting it into the free store and not into the heap by dynamically allocating the array.

I'm not clear what the OP is trying to do, so I won't add more than what I already posted, but I am curious about the comments regarding the array size. If one is passing an array that is to be mutated and NOT deep copied anywhere, what difference does the array size make? You're passing the address of the array and that's it, or at least that's it for the example I posted. In that example, I can make the array as big as I want and pass it and nothing should break. Correct?

To the OP, re-iterating that I'm not clear on what you are trying to do except that you want to mutate an array in the class, I'm wondering if possibly you don't want an array as a class member at all, but rather a pointer to an array.

double** my_a;

rather than

double my_a[][size];

Then in the constructor, assign my_a to POINT TO the array passed in the constructor. Just throwing it out there.

I made the comment about the size because I'm not sure where in the code the actual error is happening at. I also think the OP might want to just have a pointer. Not sure if it is better to have pointer to a 2d array like

double my_a***;

or a pointer for a 2d array as you posted Vernon

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.