Multidimentional Array Dynamic Allocation

This is a discussion on Multidimentional Array Dynamic Allocation within the C++ Programming forums, part of the General Programming Boards category; Is it possible to dynamicaly allocate space for a multidimentional array. Something like: Code: int* var = new int* [2][2]; ...

  1. #1
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483

    Multidimentional Array Dynamic Allocation

    Is it possible to dynamicaly allocate space for a multidimentional array.

    Something like:

    Code:
    int* var = new int* [2][2];
    or something like that?
    My Website
    010000110010101100101011
    Add Color To Your Code!

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    not quite, here's an example
    Code:
    	int ** ptr = new int * [2];
    	for (int i = 0; i < 3; i++)
    		ptr[i] = new int [2];

  3. #3
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Thanks, that was really helpful, I suspected i would have to have pointer point to a pointer point to a memory address, but I though maybe there was an easier way :-\


    Thanks a lot.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by mrafcho001
    Thanks, that was really helpful, I suspected i would have to have pointer point to a pointer point to a memory address, but I though maybe there was an easier way :-\
    http://www.parashift.com/c++-faq-lit...html#faq-16.16
    Or a vector of vectors?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    > but I though maybe there was an easier way
    There is, if all your minor dimensions are constant.

    For example
    int (*var)[2] = new int[6][2];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Array Resizing
    By dld333 in forum C++ Programming
    Replies: 13
    Last Post: 11-04-2005, 12:13 AM
  2. need help with dynamic array syntax
    By soldyne in forum C Programming
    Replies: 3
    Last Post: 10-11-2005, 02:59 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM
  5. dynamic allocation question
    By vale in forum C++ Programming
    Replies: 1
    Last Post: 08-26-2001, 05:23 PM

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