By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,917 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,917 IT Pros & Developers. It's quick & easy.

2 dimensional arrays as parameter

P: n/a
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[i][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.
Apr 16 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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?
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[i][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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 16 '07 #2

P: n/a

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f0**********@news.datemas.de...
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.
>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).
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.
Apr 16 '07 #3

P: n/a
hyena skrev:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f0**********@news.datemas.de...
>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?
>
>>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).
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[i] = 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];
}

--
OU
Apr 16 '07 #4

P: n/a

"Obnoxious User" <OU@127.0.0.1wrote in message
news:46***********************@alt.teranews.com...
hyena skrev:
>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f0**********@news.datemas.de...
>>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.
>>
>>>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).
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[i] = 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];
}

--
OU
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.
Apr 16 '07 #5

P: n/a
hyena wrote:
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[i][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

Apr 16 '07 #6

P: n/a
hyena <as@hut.atwrote:
>
"Obnoxious User" <OU@127.0.0.1wrote in message
news:46***********************@alt.teranews.com...
>hyena skrev:
>>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f0**********@news.datemas.de...
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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 18 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.