multidimentional array

Discussion in 'C++' started by Bangalore, Sep 27, 2005.

  1. Bangalore

    Bangalore Guest

    Hi all,
    Plz clarify me, on the implementation of two or three dimensional
    array using overloaded [ ] operator.

    Thanks, in advance
    Bangalore, Sep 27, 2005
    1. Advertisements

  2. "Bangalore" <-spam.invalid> wrote
    in message news:4338e514$0$45264$...

    > Hi all,
    > Plz clarify me, on the implementation of two or three dimensional
    > array using overloaded [ ] operator.
    > Thanks, in advance
    > Bangalore

    Do you mean an actual array? Or an array-like Class?

    Re. multidimensional built-in arrays, I made copious notes on
    those a few years ago. For reference, I've included those notes
    at the end of this message.

    As for an array-like class, I think the best way to impliement
    that might be using an actual multidimensional built-in array,
    with added safety features (esp. bounds checking on indexes).
    This would actually be easier and cleaner than using vectors,
    I think.

    I don't know if I'd recommend using an overloaded [] operator,
    though. Trying to get MyArray[7][37][6] to work right would be
    challenging to impliment. (Would almost force you to write
    your array class recursively, so that a 3d MyArray is a 1d MyArray
    of 2d MyArrays, etc.) Sounds messy.

    The application operator() might work better for you. That way
    you'd use just ONE operator to access an element of your array,
    regardless of the number of dimensions:

    dValue = MyArray(13,7,5,3); // (example for 4d Array)

    Also consider templatizing you class for different types, since
    it's basically just a container. You might also pass the
    number of dimensions as a template parameter, and pass the
    dimensions as arguments to a constructor. Something like this:

    MyArray<double, 3> Fred (13, 7, 7); // 3d array of doubles, 13x7x7

    Postscript: What follows are some very lengthy detailed
    notes on mulitdimensionally arrays I made some years ago,
    when I was first learning C. Anyone who would be bored
    by this please stop reading here. You've been warned.

    Wednesday July 4, 2001:
    I've been thinking-about and experimenting-with arrays today. I've
    learned that the nomenclature:
    int array[5][7][11];
    means: "an array of 5 elements, each of which is an array of 7 elements,
    each of which is an array of 11 elements, each of which is an integer".

    That is pretty much backwards from how I had thought that arrays work!

    In memory, a [3][4] array is stored in a contiguous block, in this
    [0][0] [0][1] [0][2] [0][3]
    [1][0] [1][1] [1][2] [1][3]
    [2][0] [2][1] [2][2] [2][3]
    Another way to look at multi-dimensional arrays is that they are
    multi-level n-ary trees. A [l][m][n] array is a tree with one trunk,
    l limbs, m branches per limb, and n twigs per branch. So, element
    array[3][5][7] is (from left to right): limb 3, branch 5, twig 7;
    or (from right to left): twig 7 of branch 5 of limb 3 of the array.

    Behind the scenes, I believe element address for an array such as:

    int Array[Size1][Size2][Size3];

    is implimented by pointers, like so:

    Array[x][y][z] =
    *(&(Array[0][0][0]) + x*Size2*Size3 + y*Size3 + z);

    All sizes except for the first are needed to calculate the RAM address of each element.

    And still deeper behind the scenes, at the assembly level, we need to know the size of
    the actual elements (this is, sizeof(int)), so that we can translate this C statement:

    IntPtr += 4; /* step forward over 4 "int" elements */

    Into the equivalent pseudo-machine-language statement:

    BytePtr += 16 /* step forward over 16 bytes */

    In other words,

    MachLangArray[x][y][z] =
    + x*Size2*Size3
    + y*Size3
    + z


    This is why, when passing n-dimensional arrays to functions, all but the first size
    number must be passed:

    int MyArray[3][16][6][82];
    /* ... */
    QuidBlitz = GurzBlunkt(Fidgit, MyArray[][16][6][82]);

    The first number is not needed, because the RAM address of any one element is independent
    of the number of items in the top-level grouping. (This is because we don't need to pass
    over the end boundary of the top-level grouping, but we DO pass over end-boundaries
    for all the other grouping levels, so we need to know how long they all are in order to
    calculate the RAM addresses in bytes.)

    Robbie Hatley
    Tustin, CA, USA
    email: lonewolfintj at pacbell dot net
    web: home dot pacbell dot net slant earnur slant
    Robbie Hatley, Sep 27, 2005
    1. Advertisements

  3. upashu2

    upashu2 Guest

    if u mean [j] and [j][k], you can't overload it, you can only
    define for your custom class.
    p[j] means that it is defined something like this - int **p;
    and p[j] = *( *(p+i)+j)
    and memory is allocated like
    p = new int *[ i];
    for(int k=0; k<i;i++) *(p+i) = new int[j];

    Instead of overloading [ ] [ ] or [ ] [ ] [ ] , overload ' () '
    operator, and access the element as (i,j) or (i,j,k).
    upashu2, Sep 27, 2005
  4. E. Robert Tisdale, Sep 27, 2005
  5. "Bangalore" <-spam.invalid> schrieb
    im Newsbeitrag
    > Hi all,
    > Plz clarify me, on the implementation of two or three dimensional
    > array using overloaded [ ] operator.

    If you really want this, you can make a class:

    class Mat3x3 : public std:vector<std::vector<std::vector<double> > >

    and then handle the allocation of each row somehow. Bad idea. Use the
    () operator.
    Gernot Frisch, Sep 28, 2005
  6. Marcus Kwok

    Marcus Kwok Guest

    Marcus Kwok, Oct 5, 2005
  7. Axter

    Axter Guest

    Bangalore wrote:
    > Hi all,
    > Plz clarify me, on the implementation of two or three dimensional
    > array using overloaded [ ] operator.
    > Thanks, in advance
    > Bangalore

    You can use a vector of vector, like the following:

    vector<vector<int> > My2D_Array;
    vector<vector<vector<int> > > My3D_Array;

    Or you can use a simple class like the following:

    The above class can be access via [][] index, and does not require a
    helper class.
    For more information, check out the following links:

    I recommend against using the operator() method that is suggested in
    the C++ FAQ.
    It's not consistent with normal array indexing.
    IMHO, using a method that gives consistent syntax is preferable, and
    easier to read.
    Axter, Oct 6, 2005
  8. Marcus Kwok

    Marcus Kwok Guest

    Marcus Kwok, Oct 6, 2005
    1. Advertisements

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. willem
    Feb 15, 2005
  2. Replies:
    Victor Bazarov
    Jan 24, 2005
  3. vertigo
    Mathias Panzenboeck
    Dec 10, 2006
  4. cedric
    Michael Mair
    Dec 30, 2004
  5. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    David RF
    Sep 3, 2009
  6. Tom
    Dec 20, 2004
  7. Tuan  Bui
    it_says_BALLS_on_your forehead
    Jul 29, 2005
  8. Sébastien Cottalorda

    IPC::Shareable Problem with multidimentional hash

    Sébastien Cottalorda, Sep 9, 2005, in forum: Perl Misc
    Sébastien Cottalorda
    Sep 13, 2005