Thread: Dynamically Allocated Multidimensional Array

  1. #16
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Quote Originally Posted by AndrewHunter View Post
    Umm...do you not click on any links? Multidimensional Arrays-C++ Click the thing in red, it has answered all these questions
    Yes, in fact I do click the links and had already read the page. The only time it mentioned push_back() was here:

    You can also do some interesting things with dynamic multi-dimensional arrays with vectors. For example, if you only allocate the first dimension, then use the .push_back() to add records to the 2nd dimension it's no longer a grid, but an array with a dynamically sized 2nd dimension (much like a street of buildings each with a different amount of floors). This functionality can be achieved using pointers, but is much harder to do.
    Pardon my ignorance, but I can't deduce from here how to use push_back() with a 2D vector, and it gives no examples in the code. I also read from this link that:

    A vector is, essentially, a resizable array; the vector class allows random access via the [] operator, but adding an element anywhere but to the end of a vector causes some overhead as all of the elements are shuffled around to fit them correctly into memory.
    which leads me to believe that I should use push_back() instead of using [], or at least that it would be a lot easier. Are you saying that I can't or shouldn't use push_back()??? If not, please give me an example of how it can be used with a 2D array.

  2. #17
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Quote Originally Posted by LyTning94 View Post
    Pardon my ignorance, but I can't deduce from here how to use push_back() with a 2D vector, and it gives no examples in the code.
    What the article means is that you can have a vector of differently sized vectors, which you cannot do with a regular multidimensional array.

    For example:
    Code:
    std::vector< std::vector<int> > array2D;
    
    array2D.resize(100);
    // array2D is a std::vector< std::vector<int> > that now contains
    // 100 std::vector<int>s (each intialised with the default constructor)
    	
    array2D[0].push_back(1);
    array2D[0].push_back(2);
    array2D[0].push_back(3);
    array2D[0].push_back(4);	
    // array2D[0] is now an std::vector<int> that contains 4 elements
    
    array2D[1].push_back(2);
    array2D[1].push_back(2);
    // array2D[1] is now an std::vector<int> that contains 2 elements
    
    array2D[2].resize(20);
    // array2D[2] is an std::vector<int> that contains 20 elements
    // (each intialised with the default constructor)
    
    array2D[3].resize(30);
    // array2D[3] is an std::vector<int> that contains 30 elements
    // (each intialised with the default constructor)
    
    array2D[x].resize(y);
    // array2D[x] is an std::vector<int> that contains y elelments
    This means you can save on memory, if you don't intend to have a "grid", because you can have various differently sized vectors depending on your needs.

    which leads me to believe that I should use push_back() instead of using [], or at least that it would be a lot easier. Are you saying that I can't or shouldn't use push_back()??? If not, please give me an example of how it can be used with a 2D array.
    Using [] to access random elements is done in constant time, so you shouldn't have any additional overhead when simply accessing elements using [] with a vector. I think what the article means is that there may be additional overhead if you insert a new element in the middle of the vector, because the vector keeps each element in contiguous memory (which means if you insert a new element in the middle, it has to move some or all of the existing elements around in memory). push_back(...) places the element at the end of the memory block, so it shouldn't have to move everything around normally. I may be wrong about that though, so hopefully a more knowledgeable coder can confirm this for you.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Ushakal
    This means you can save on memory, if you don't intend to have a "grid", because you can have various differently sized vectors depending on your needs.
    On the other hand, if you do intend to have a grid, this wastes memory since each vector keeps track of its own size (and capacity). There is a way around this, as is hinted by the article's statement that "you can optimize this by combining the 2 dimensions into a single dimension".

    Quote Originally Posted by Ushakal
    there may be additional overhead if you insert a new element in the middle of the vector, because the vector keeps each element in contiguous memory (which means if you insert a new element in the middle, it has to move some or all of the existing elements around in memory). push_back(...) places the element at the end of the memory block, so it shouldn't have to move everything around normally. I may be wrong about that though, so hopefully a more knowledgeable coder can confirm this for you.
    You're right. However, this linear time insertion in the middle is a limitation of arrays in general, not just std::vector.
    Last edited by laserlight; 08-08-2011 at 09:57 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Mar 2011
    Posts
    61
    Thanks for the help and the explanation. I think I understand everything that I need to know.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just to provide you with an alternative if you want an easier way of creating an N-dimensional array:

    Code:
    #include "boost/multi_array.hpp"
    
    int main ()
    {
      // Create a 3D array that is 3 x 4 x 2
      boost::multi_array<double, 3> A(boost::extents[3][4][2]);
    }
    That's it. You have your array. No push_backs to use.
    Naturally, this isn't a dynamic array; just a static array. Just like std::array.
    Requires boost (boost.org).

    You can see the full example here: The Boost Multidimensional Array Library (Boost.MultiArray) - Boost 1.47.0
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamically Allocated Array Troubles
    By grogel in forum C Programming
    Replies: 7
    Last Post: 06-28-2011, 09:03 PM
  2. Dynamically Allocated Array
    By chris4 in forum C Programming
    Replies: 9
    Last Post: 05-06-2011, 10:01 AM
  3. Initializing a 2D array dynamically allocated
    By newboyc in forum C Programming
    Replies: 5
    Last Post: 02-01-2011, 01:08 PM
  4. Dynamically allocated array
    By dre in forum C Programming
    Replies: 17
    Last Post: 08-13-2009, 06:57 PM
  5. Dynamically Allocated Array
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 08:40 AM