Thread: Dynamic 2D arrays question

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    31

    Dynamic 2D arrays question

    Code:
    typedef struct {
        int rows;
        int cols;
        int ** element;
    } matrix;
    Is there any difference between using this to get to the int element is pointing at

    matrix * resultMatrix = new matrix;

    resultMatrix->element[i][j]

    and this?

    *(*((resultMatrix->element)+i))+j)

    Forgive me if this seems basic, cos I just wanna make sure

    Pier.

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    No, I dont see difference, and as far as I know, the compiler translate this sentence:
    arr[i] to *(arr+i)

    So, I think, same thing with 2d arrays.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    No, there is no real difference, except the notation. The variable element is the start-address of the array. It is allowed to access the array by adding a value to the start-address.

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    There is no problem in accessing your example in that manner, however, I do have to point out that what you are working with is not a dynamic 2D array. A 2D array in C and C++ is guaranteed to occupy adjacent space in memory and is not an array of pointers to arrays. A more precise method of working with a "dynamic multidimensional array" can be simulated by dynamically allocating a single-dimension of size numrows*numcolumns, and accessing it via the element row * numcolumns + column. This is what C an C++ do internally when you create and work with a multidimensional array on the stack, etc. Otherwise, your example is just providing a (paritally) syntacticly similar way of working with data as though it were a multidimensional array, but implementation and functionality are completely different. You can no longer use standard pointer arithmetic on the array as a whole, not to mention the fact that you're fragmenting your heap.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    Originally posted by Polymorphic OOP
    etc. Otherwise, your example is just providing a (paritally) syntacticly similar way of working with data as though it were a multidimensional array, but implementation and functionality are completely different. You can no longer use standard pointer arithmetic on the array as a whole, not to mention the fact that you're fragmenting your heap.
    Do you mean this..
    Code:
    (resultMatrix->element) = new int*[resultMatrix->rows];  /*dynamic allocation of memory*/
    for (i=0; i<(resultMatrix->rows); i++) 
      *((resultMatrix->element)+i) = new int[resultMatrix->cols];/*end of memory allocation*/
    Last edited by MadStrum!; 02-08-2003 at 04:38 AM.

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    First, that's C++, not C. This is a C board. Second, no, that's not what I meant. The example you gave is what you're doing right now.

    This is what I mean (since you're using C++, I'll post it in C++)

    typedef struct {
    int rows;
    int cols;
    int* element; // Not a pointer to a pointer
    } matrix;

    resultMatrix->element = new int[resultMatrix->rows * resultMatrix->columns]; /*dynamic allocation*/

    There, no looping or anything. This is what's done internally when you make a multidimensional array anyways. In your example you're just making an array of pointers to arrays which is actually very different.

    In order to access the above matrix you'd do

    resultMatrix->element[ row * resultMatrix->columns + column ]

    (you'd put that in a function usually to make things simpler when accessing the matrix)

    Since you're using C++, you can just overload operator() to take 2 parameters -- a row and a column so that you can just simply do

    resultMatrix( row, column )

    to access it
    Last edited by Polymorphic OOP; 02-08-2003 at 04:51 AM.

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    31
    Ok I get what you mean....
    But I'm stuck with this way of implementing the structure because it was given to me in my assignment.
    So I guess my method will work anyway?
    Thanks for sharing the info

    Pier.

  8. #8
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Yup, it should still work, it'll just be less efficient and won't be as much like a true "2D Array".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d dynamic arrays
    By zbest in forum C++ Programming
    Replies: 5
    Last Post: 12-06-2008, 12:53 PM
  2. Passing 2D dynamic arrays
    By s_siouris in forum C Programming
    Replies: 5
    Last Post: 11-12-2008, 08:08 AM
  3. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM