Thread: dynamically allocating a 2dim array

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    106

    dynamically allocating a 2dim array

    is this possible? if so, why doesnt the statement

    Code:
    char *data=new char[w][h];
    work? any help would be *greatly* appreciated. thanks!

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    It's possible. You need to allocate space for the width first, then for each row (0 to w-1) allocate space for the length;

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    any hints as to how i could go about doing this?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >any hints as to how i could go about doing this?
    Code:
    char **array = new char*[w];
    for ( int i = 0; i < w; i++ )
      array[i] = new char[h];
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    106
    perfect! thanks a bunch!

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Originally posted by Prelude
    >any hints as to how i could go about doing this?
    Code:
    char **array = new char*[w];
    for ( int i = 0; i < w; i++ )
      array[i] = new char[h];
    Awww, that's not a hint, that's an answer!! A hint would be:
    Originally posted by jlou
    You need to allocate space for the width first, then for each row (0 to w-1) allocate space for the length;

  7. #7
    Registered User impactBlue's Avatar
    Join Date
    Mar 2004
    Posts
    5
    cool, i was just going to post a question about this very same thing but it's already here. my question, though, is how would go about deleting the allocated 2d array?

    is this the correct way to delete that dynamic 2d array?:

    Code:
    for(int i = 0; i < w; i++)
    {
       delete [] array[i];
    }
    
    delete [] array;

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Here's another hint:

    Yes.

  9. #9
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    how about using STL vector to create a 2d dynamic array ?

    how to create it and how to assign data or delete data from it ?

  10. #10
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    >>how to create it and how to assign data or delete data from it ?

    http://cppreference.com/cppvector.html

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  11. #11
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    i mean creating a 2d dynamic array using STL vector. can i do something like

    Code:
    vector<vector<int> >array;
    if the above code make a 2d dynamic array, how i insert data into it ?

  12. #12
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Yes you can do that. Here's an example that show how to add stuff to a 2d vector:
    Code:
    #include <vector>
    
    int main()
    {
        int rows = 5;
        int cols = 10;
    
        // Start out full of 0's.
        std::vector<std::vector<int> > array0(rows, std::vector<int>(cols));
    
        // Start out full of 1's.
        std::vector<std::vector<int> > array1(rows, std::vector<int>(cols, 1));
    
        // Start out with empty rows.
        std::vector<std::vector<int> > array2(rows);
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
                array2&#091;i&#093;.push_back(j);
        }
    
        // Start out completely empty.
        std::vector<std::vector<int> > array;
        for (int i = 0; i < rows; i++)
        {
            std::vector<int> row;
            for (int j = 0; j < cols; j++)
                row.push_back(j);
            array.push_back(row);
        }
    }
    Last edited by jlou; 03-09-2004 at 02:19 PM.

  13. #13
    Registered User
    Join Date
    Oct 2003
    Posts
    55
    if i want to insert data in a specific location, then how ?
    for example, if i need to insert data in location array[2][3], how am i suppose to do it ?

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    the vector class allows you to use the [] operator just as you would with standard arrays.

    Using the insert methods of the vector class spares you the need to shift elements within the vector, if needed, but requires you to pass several parameters, one of which is an iterator to the address where you want to insert the material.

    To, go one step further, there are matrix classes built on the vector classes that you could consider using. There, the underlying vector < vector < T> > stuff is all done for you. A search of the internet will probably dredge up several examples, eventually.

  15. #15
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Just like in standard arrays, you can use:

    array[2][3] = 78;

    But remember that just like in a standard array you are not inserting data. Instead you are changing the data at that location. In both vectors and standard arrays, if you use [] there is already a value at the position you are accessing. The reason this is important is that if you start out with a completely empty array (e.g. std::vector<std::vector<int> > array;), then assigning a value to [2][3] is undefined behavior that could easily crash your program. You have to make sure the size of each vector is what you want it to be before using the operator[].

    All four of those examples above leave the arrays in a state where you can use operator[].

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Allocating a 2D Array Differently
    By pianorain in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2005, 02:01 AM
  2. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  3. delete dynamically allocated char array
    By xddxogm3 in forum C++ Programming
    Replies: 7
    Last Post: 11-23-2003, 04:57 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Replies: 4
    Last Post: 09-12-2001, 02:05 PM