dynamic storage allocation for array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amare21
    New Member
    • May 2010
    • 2

    #1

    dynamic storage allocation for array

    I am getting confused about making a 2 dimensional array of pointers allocate storage space dynamically in C++. Actually I just want the second dimension of the array to be dynamic in size. For e.g. a simpler version of what I want to do would be:
    Instead of,
    Code:
    int p[10];
    I would do
    Code:
    int *p;
    int x=10; // OR available thru user input
    
    p = new int [x];
    but what I have currently is:
    Code:
    int *p[2][5]; // array of 2x5 pointers
    and want to convert to allow the user to enter the size of second dimension (which is [5] right now). Maybe the declaration will look like:
    Code:
    (int *) (* p[2]);
    Not sure how to go around allocating space for the second dimension of the array?
    Last edited by Niheel; May 27 '10, 03:19 AM. Reason: unnecessary spacing, code tags
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    No you can`t do that.
    Read this.

    ......you may be shocked to find that arrays are really all one dimensional.

    Comment

    • amare21
      New Member
      • May 2010
      • 2

      #3
      dynamic storage allocation for array

      Thanks for the link. After trying a few things, ended up using a "map" instead:


      typedef std::pair<UINT8 , UINT16> tABC;
      static std::map<tABC, int*> xyz;

      Comment

      Working...