2 dimensional arrays as parameter

H

hyena

Hi,

I have a problem regarding passin 2 dimensional array into a function.
I have a[][] to pass into function f(), f is called many times and the size
of a[][] will change for each call.
I am not interested in vector implementation in std library since speed is
my major concern.

the follwoing code does not work of couse, it show the idea what I want. The
compiler comlained about lacking at least the 2nd dimnesion length.

void f(int a[][]){
for(i)for(j){
...a[j]
}
}

main(){
int nrow=5,ncol=4;
int a[nrow][ncol];
....//fill in a
f(a);

......
nrow = ..,ncol =...
f(a);
}

in such case, what would be the solution, thanks.
 
V

Victor Bazarov

hyena said:
I have a problem regarding passin 2 dimensional array into a
function. I have a[][] to pass into function f(), f is called many times
and
the size of a[][] will change for each call.

Both of them?
I am not interested in vector implementation in std library since
speed is my major concern.

Ah... Premature optimization, assumptions... Sure. You must know
better.
the follwoing code does not work of couse, it show the idea what I
want. The compiler comlained about lacking at least the 2nd dimnesion
length.

Well, one way around it is to pass an array of pointers, and not
a two-dimensional array of values. The other way around it is to
make the function a template and pass the array by a reference and
let the compiler figure out the sizes (if they are known at compile
time). Yet another way around it is to pass a one-dimensional array
and do all indexing yourself (instead of relying on the compiler to
figure out all offsets from the second dimension).
void f(int a[][]){
for(i)for(j){
...a[j]
}
}

main(){
int nrow=5,ncol=4;
int a[nrow][ncol];


That's not C++.
....//fill in a
f(a);

.....
nrow = ..,ncol =...
f(a);
}

in such case, what would be the solution, thanks.

I don't know "the" solution. But there are several you can try.

V
 
H

hyena

Victor Bazarov said:
hyena said:
I have a problem regarding passin 2 dimensional array into a
function. I have a[][] to pass into function f(), f is called many times
and
the size of a[][] will change for each call.

Both of them?

yes, both of them, pity.
Ah... Premature optimization, assumptions... Sure. You must know
better.

well, ....I had tried another piece of code in std realization and found it
is not very efficient compare to array version,from that comes my
impression, along with some disscussions I read.
Well, one way around it is to pass an array of pointers, and not
a two-dimensional array of values. The other way around it is to
make the function a template and pass the array by a reference and
let the compiler figure out the sizes (if they are known at compile
time). Yet another way around it is to pass a one-dimensional array
and do all indexing yourself (instead of relying on the compiler to
figure out all offsets from the second dimension).
snip

I was trying to make the code simple and readable, if possible. I have tried
pass a array of pointers, still I need to give info at least about the
length of the array pointed by the pointer.

eg. f(int (*a)[]) , the compiler complains no array size info again.

and I am not familiar with c++ template concept, :). Seems "the" solution to
me may well be the last you suggested.

Thansk for your input.
 
O

Obnoxious User

hyena skrev:
Victor Bazarov said:
hyena said:
I have a problem regarding passin 2 dimensional array into a
function. I have a[][] to pass into function f(), f is called many times
and
the size of a[][] will change for each call.
Both of them?

yes, both of them, pity.
Ah... Premature optimization, assumptions... Sure. You must know
better.

well, ....I had tried another piece of code in std realization and found it
is not very efficient compare to array version,from that comes my
impression, along with some disscussions I read.

Do you mean std::vector said:
snip

I was trying to make the code simple and readable, if possible. I have tried
pass a array of pointers, still I need to give info at least about the
length of the array pointed by the pointer.

It's the sub dimensions the compiler needs length specified for in order
to calculate offset jumps when indexing. For the first dimension all it
needs to know comes from sizeof(type). It's a memory layout issue.
eg. f(int (*a)[]) , the compiler complains no array size info again.

and I am not familiar with c++ template concept, :). Seems "the" solution to
me may well be the last you suggested.

You could create your own wrapper:

class two_dim_array {
int ** d_array;
two_dim_array() {
int x = 3, y = 8;
d_array = new int*[x];
for(unsigned i = 0; i < x; ++i) {
d_array = new int[y];
}
}
int * operator[](unsigned index) {
return d_array[index];
}
// and more like resize() and ~two_dim_array
};

Another solution is what Victor gave you:

int xs = 3, ys = 5;

int * array = new int[xs*ys];

int index(unsigned x, unsigned y) {
return array[x * ys + y];
}
 
H

hyena

Obnoxious User said:
hyena skrev:
Victor Bazarov said:
hyena wrote:
I have a problem regarding passin 2 dimensional array into a
function. I have a[][] to pass into function f(), f is called many
times and
the size of a[][] will change for each call.
Both of them?

yes, both of them, pity.
I am not interested in vector implementation in std library since
speed is my major concern.
Ah... Premature optimization, assumptions... Sure. You must know
better.

well, ....I had tried another piece of code in std realization and found
it is not very efficient compare to array version,from that comes my
impression, along with some disscussions I read.

Do you mean std::vector< std::vector< int > > or something else?

I remeber it was std::vector< std::vector< double > > or float type. with
many loops(no random access, just going through the entire array) and
passing around to other functions, my impression is that this constainer
version runs much slower then the simple array version. But I did not
seriously try to compare them and quickly turn back to normal array
solutions.
snip

I was trying to make the code simple and readable, if possible. I have
tried pass a array of pointers, still I need to give info at least about
the length of the array pointed by the pointer.

It's the sub dimensions the compiler needs length specified for in order
to calculate offset jumps when indexing. For the first dimension all it
needs to know comes from sizeof(type). It's a memory layout issue.

yeap.
eg. f(int (*a)[]) , the compiler complains no array size info again.

and I am not familiar with c++ template concept, :). Seems "the" solution
to me may well be the last you suggested.

You could create your own wrapper:

class two_dim_array {
int ** d_array;
two_dim_array() {
int x = 3, y = 8;
d_array = new int*[x];
for(unsigned i = 0; i < x; ++i) {
d_array = new int[y];
}
}
int * operator[](unsigned index) {
return d_array[index];
}
// and more like resize() and ~two_dim_array
};

Another solution is what Victor gave you:

int xs = 3, ys = 5;

int * array = new int[xs*ys];

int index(unsigned x, unsigned y) {
return array[x * ys + y];
}


Both looks quite suitable to my problem. I think I will follow what Victor
has suggested and just copy ur code here:).

thanks for the answers and the codes.
 
Z

Zeppe

hyena said:
Hi,

I have a problem regarding passin 2 dimensional array into a function.
I have a[][] to pass into function f(), f is called many times and the size
of a[][] will change for each call.
I am not interested in vector implementation in std library since speed is
my major concern.

I think the valarray is performing enough :)
the follwoing code does not work of couse, it show the idea what I want. The
compiler comlained about lacking at least the 2nd dimnesion length.

void f(int a[][]){
for(i)for(j){
...a[j]
}
}

main(){
int nrow=5,ncol=4;
int a[nrow][ncol];


ok, given that this is an error, I'm wondering how you are actually
allocating this memory. Do you really need allocation in the stack?
Otherwise, you can go for a int** if you want to keep the double
dimensions, or a int* in which you access the elements as n*i+j, n being
the number of columns in your matrix.
....//fill in a
f(a);

.....
nrow = ..,ncol =...

oh, well, what is this? you need to reallocate a as well.
f(a);
}

in such case, what would be the solution, thanks.


another possibility is to use some library such matrix ublas included in
boost, the matrix definition is clean and very optimized. You can also
choose the storage model in order to maximize the performances in the
vectorial operations.

Of course, the objects have to be given to the functions by reference.

Remember anyway that if you need the performances the worst solution is
to start optimizing without knowing well where the things have to be
optimized. For example, probably the vector has got a little bit of
overhead for some operations (but for the elements access and so on it's
usually not the case), but a matrix allocation is as expensive as
thousands of vector element accesses.

Regards,

Zeppe
 
M

Marcus Kwok

hyena said:
Obnoxious User said:
hyena skrev:
hyena wrote:
I have a problem regarding passin 2 dimensional array into a
function. I have a[][] to pass into function f(), f is called many
times and
the size of a[][] will change for each call.
Both of them?

yes, both of them, pity.

I am not interested in vector implementation in std library since
speed is my major concern.
Ah... Premature optimization, assumptions... Sure. You must know
better.

well, ....I had tried another piece of code in std realization and found
it is not very efficient compare to array version,from that comes my
impression, along with some disscussions I read.

Do you mean std::vector< std::vector< int > > or something else?

I remeber it was std::vector< std::vector< double > > or float type. with
many loops(no random access, just going through the entire array) and
passing around to other functions, my impression is that this constainer
version runs much slower then the simple array version. But I did not
seriously try to compare them and quickly turn back to normal array
solutions.

In your vector functions, if you were not passing the vectors by
reference, then it was probably copying the vectors many more times than
it needed to. In many cases, vectors and arrays have equal performance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top