2 dimensional arrays as parameter

Discussion in 'C++' started by hyena, Apr 16, 2007.

  1. hyena

    hyena Guest

    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.
    hyena, Apr 16, 2007
    #1
    1. Advertising

  2. 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[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
    Victor Bazarov, Apr 16, 2007
    #2
    1. Advertising

  3. hyena

    hyena Guest

    "Victor Bazarov" <> wrote in message
    news:f001g7$brs$...
    > 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.
    hyena, Apr 16, 2007
    #3
  4. hyena skrev:
    > "Victor Bazarov" <> wrote in message
    > news:f001g7$brs$...
    >> 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 = 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
    Obnoxious User, Apr 16, 2007
    #4
  5. hyena

    hyena Guest

    "Obnoxious User" <OU@127.0.0.1> wrote in message
    news:46239264$0$13615$...
    > hyena skrev:
    >> "Victor Bazarov" <> wrote in message
    >> news:f001g7$brs$...
    >>> 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 = 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.
    hyena, Apr 16, 2007
    #5
  6. hyena

    Zeppe Guest

    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[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
    Zeppe, Apr 16, 2007
    #6
  7. hyena

    Marcus Kwok Guest

    hyena <> wrote:
    >
    > "Obnoxious User" <OU@127.0.0.1> wrote in message
    > news:46239264$0$13615$...
    >> hyena skrev:
    >>> "Victor Bazarov" <> wrote in message
    >>> news:f001g7$brs$...
    >>>> 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
    Marcus Kwok, Apr 18, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    422
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,913
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    638
    Vivek
    Aug 21, 2003
  4. Venkat
    Replies:
    4
    Views:
    962
    Venkat
    Dec 5, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    194
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page