Why can't you allocate a 2D array like this?

This is a discussion on Why can't you allocate a 2D array like this? within the C++ Programming forums, part of the General Programming Boards category; I've seen how to dynamically allocate a two-dimensional array row by row. But why couldn't you just simply allocate the ...

  1. #1

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76

    Why can't you allocate a 2D array like this?

    I've seen how to dynamically allocate a two-dimensional array row by row. But why couldn't you just simply allocate the memory and cast it into a 2D array, like this:

    Code:
    // I want a dynamically allocated int[2][2], so allocate enough memory:
    int* buffer = new int [2 * 2];
    
    // And do the cast:
    int (*p_array) [2] [2] = (int (*) [2] [2]) buffer;
    
    // ...
    
    delete [] buffer;
    // Or, delete [] p_array; ?
    I guess it's not technically an array of an array, but the result will achieve that effect. Also, this would be handy:

    Code:
    int (&the_array) [2] [2] = *p_array;
    Last edited by rudyman; 07-22-2008 at 12:01 PM.

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    It's just not how the syntax for the language is.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why not just use a vector<vector<int> > instead, or some other class that acts like a matrix?

  4. #4

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    I know there are better alternatives, but the code I used compiles, and it seems the memory gets deleted properly, too. I don't understand what's so bad about it (other than you can't delete individual rows).
    Last edited by rudyman; 07-22-2008 at 12:00 PM.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You can combine a 2d array into a single allocation, there's nothing wrong with it. I know Bubba has posted examples here before, you may get some tips if you can find them in a search.

  6. #6

    Join Date
    Apr 2008
    Location
    USA
    Posts
    76
    I find this a much easier solution than allocating row by row. A helper function could do most of the work:

    Code:
    template <typename T, size_t x, size_t y>
    int (*Allocate2DArray ()) [x] [y]
    {
        return new T [x * y];
    }

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Just use Boost's multi_array class, which does the allocation (as a single block), and provides easy accessors, and lets you choose row-major or column-major, and supports even more dimensions.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    Code:
    int (*array)[2] = new int[2][2];
    Should be what you want, if you really want an array with two subscripts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cannot print out my 2d array correctly! please help
    By dalearyous in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2006, 02:07 AM
  2. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 08:51 AM
  3. Copying from one 2d array to another....with a twist
    By Zildjian in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2004, 07:39 PM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM

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