new for 2D array

This is a discussion on new for 2D array within the C++ Programming forums, part of the General Programming Boards category; Lets say that I want a dynamic 2D array, I would do it in following way: Code: double (*koordinates)[3] = ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    new for 2D array

    Lets say that I want a dynamic 2D array, I would do it in following way:

    Code:
    double (*koordinates)[3] = new double [number_of_points][3]
    where number_of_points variable is inputed while program runs, and [3] is number of dimensions od space. But what I don't know how to do is, how do I make a [3] also dynamic, for example, if I want to choose a number of dimensions while program is running.
    I tried:
    Code:
    double (*koordinates)[dimension]= new double [number_of_points][dimension]
    
    or
    
    double (*koordinates)(*dimension)= new double [number_of_points][dimension]
    and it doesn't work.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Even better in C++:
    Code:
    std::vector<std::vector<double> > koordinates(dimension, std::vector<double>(dimension));

  3. #3
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Code:
    int **arry;
    size_t ARRY_X = 3;
    size_t ARRY_Y = 4;
        
    arry = new int*[ARRY_X];
    for(int i = 0; i < ARRY_X; i++)
       arry[i] = new int[ARRY_Y];
    Quote Originally Posted by Daved
    Even better in C++:
    Agreed, but it's worth it to know how to allocate multi-dimensional arrays.
    Last edited by SlyMaelstrom; 12-05-2006 at 02:49 PM.
    Sent from my iPad®

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I would leave your response there anyway since it actually answers the original question.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    13
    Thank you both.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,575
    On a side note...

    Do you really want to make the spacial dimension dynamic? Because if you are only working with 2 and 3 dimensions (generally that is the case), I would advise against it. You are complicating your life as far as deallocation is concerned. Count how many deletes you have to write.

    In fact, when working with multiple dimension dynamic arrays, the best option is to move to one dimensional arrays. Read http://www.parashift.com/c++-faq-lit...html#faq-16.16, taking into consideration that a multidimensional array is in fact an array of arrays, as Sly demonstrated.

    However, better yet is to not make the spacial dimensions dynamic. Use typedefs to simplify your array usage. Like this:

    Code:
    typedef double koord_2D[2];
    typedef double koord_3D[3];
    
    koord_3D* Planet = new koord_3D[4];
    koord_2D* Point = new koord_2D[4];
    You have now two types that map to 2D and 3D spacial objects. Planets is an array of 4 planets on a 3D coordinates system. Points is an array of 4 points on a Cartesian coordinate system.

    Any function can then use the typedef as a parameter and all will be much clearer.

    EDIT: Oh! and the deletes... you just need two. One for Planet and one for Point
    Last edited by Mario F.; 12-05-2006 at 04:03 PM.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM

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