Two dimentional array

This is a discussion on Two dimentional array within the C++ Programming forums, part of the General Programming Boards category; Is it possible to make a two dimetional dynamic array? It probably is, but I cant get it to work ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Two dimentional array

    Is it possible to make a two dimetional dynamic array?
    It probably is, but I cant get it to work

    Thanks in advance

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    example:
    Code:
    int i;
    int sizeX = 10, sizeY = 20;
    int **my2DArray;
    my2DArray = malloc(sizeX * sizeof(*my2DArray));
    // OR: my2DArray = malloc(sizeX * sizeof(*int));
    for (i = 0; i < sizeX; i++)
    {
        my2DArray[i] = malloc(sizeY * sizeof(**my2DArray));
        // OR: my2DArray[i] = malloc(sizeY * sizeof(int));
    }

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Is it possible to make a two dimetional dynamic array?
    Code:
    vector<vector<int> > V(rows, cols);

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    that should be:
    Code:
    vector<vector<int> > V(rows, vector<int>(cols));
    I wouldn't use malloc in C++, and I wouldn't use new either because a container like vector is much better than dynamic allocation.

    For one thing, whether you use malloc or new, KONI's example doesn't include the deletion code. The vector example is all you need for creation and cleanup.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Considering this is C++ new and delete are more used, but vectors are the way to go, really.

    Code:
    int **Allocate_2D_Array( int x, int y )
    {
        int **Array = new int *[y];
    
        for ( int i=0; i<y; i++ )
            Array[i] = new int [x];
    
        return Array;
    }
    
    
    
    void Dealocate_2d_array ( int **Array, int y )
    {
        for ( int i=0; i<y; i++ )
            delete []Array[i];
    
        delete []Array;
    }
    
    
    
    int main( void )
    {
        int x, y;
    
        // Get your user defined array dimensions
        cout<< "Enter X: ";
        cin >> x;
    
        cout<< "Enter Y: ";
        cin >> y;
    
        // Allocate memory
        int **p_2d_array = Allocate_2D_Array( x, y );
    
        // Fill array with variables
        for ( int j=0; j<y; j++ )
            for ( int i=0; i<x; i++ )
                p_2d_array[j][i] = i+j;
    
        // print out array
        for ( int j=0; j<y; j++ )
        {
            for ( int i=0; i<x; i++ )
                cout<< p_2d_array[j][i] << " ";
    
            cout<< '\n';
        }
    
        // delete allocated memory
        Dealocate_2d_array( p_2d_array, y );
    
        return 0;
    }
    Should work OK if you need to use new/delete. I prefer to make functions to allocate and deallocate the memory cause it makes main() ... nicer.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Unless you are forced to do so because you are a student, I cannot think of any reason why you would pick that version over a vector. You are really just reinventing what a vector does for you, except your own version won't work as well.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by Daved View Post
    that should be:
    Code:
    vector<vector<int> > V(rows, vector<int>(cols));
    |
    Are you sure Daved? I used to not think what I posted worked, but then I saw someone post that example, tried it, and changed my mind. Maybe it's not conformant to the standard though.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    I can't say that I'm sure. At first I thought it was wrong because using that form on a one dimensional vector would create a vector of rows ints initialized to cols. However, that cannot happen here since it is declared as a 2-D vector. So what it will try to do is create rows vector<int> objects constructed with cols.

    My guess is that it will work if the single int constructor for vector<T> is not explicit, and it will fail if it is. I'm not sure it either way is required by the standard, but I'll check if I have a chance.

    BTW, I do get an error in VC++ 7.1 indicating that an int cannot be converted to a const vector<int>& because, "Constructor for class 'std::vector<_Ty>' is declared 'explicit'".
    Last edited by Daved; 04-16-2007 at 05:13 PM.

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >BTW, I do get an error in VC++ 7.1 indicating that an int cannot be converted to a const vector<int>&
    Interesting. It compiles with Dev-C++ (g++).

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    My copy of the standard indicates it should be explicit (23.2.4.1), so I suspect gcc is wrong in this case.

    Interestingly, in this this link Stroustrup indicates that this exact example was the catalyst for the rule in C++ allowing constructors to be declared explicit: http://www.research.att.com/~bs/dne_errata.html (scroll to pg80 for the second printing).

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Thanks to all of you for your inputs. I think this forum is the one where most people get the most replies to there question.

    I need the two dimentional array for holding the color of a computer generated picture so that I can pass it to my saveFileFunction (Work in progres).

    Thanks again

  12. #12
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    >> You are really just reinventing what a vector does for you

    Technically it's the other way around isn't it

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,705
    You are really just doing what a vector reinvents for you ?

    I'm not very good at logical inverses

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  2. Have problems with copying my array!
    By AvaGodess in forum C Programming
    Replies: 11
    Last Post: 09-25-2008, 12:56 AM
  3. passing 3 dimentional array into function
    By 182blink in forum C Programming
    Replies: 3
    Last Post: 10-28-2006, 05:20 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21