dynamic two dimensional arrays

This is a discussion on dynamic two dimensional arrays within the C++ Programming forums, part of the General Programming Boards category; I can successful declare a dynamic one dimensional array (size is unknown at compilation) but I can't get my program ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    dynamic two dimensional arrays

    I can successful declare a dynamic one dimensional array (size is unknown at compilation) but I can't get my program to compile when I try to make it two dimensional. Here is what worked:

    header.h
    Code:
    [snip]
    int *myLocations;
    main.cpp
    Code:
    [snip]
    routeData myRoute;
    myRoute.myLocations = new int[10];
    Here is what didn't work:

    header.h
    Code:
    int *myLocations(*);
    main.coo
    Code:
    routeData myRoute;
    myRoute.myLocations = new int[10][10];
    The 10s are just examples of what may come from what the user specified the array size to be.

    Or would it be best to just use a two dimensional vector, since those don't seem to need to have size constraints on them?
    Last edited by Beowolf; 10-30-2007 at 04:13 PM.
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    271
    To declare a two dimensional array, you declare
    Code:
    int **myLocations
    To allocate new memory for it, you have to
    Code:
    myRoute.myLocations = new int*[10]
    Then you iterate through each of the myLocations pointers and allocate memory for that, too.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Then you iterate through each of the myLocations pointers and allocate memory for that, too.
    Like this?

    Code:
    for(int i = 0; i < 10; i++)
        {
            *(myRoute.myLocations + 1) = new int[10];
        }
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Beowolf View Post
    Like this?

    Code:
    for(int i = 0; i < 10; i++)
        {
            *(myLocations + 1) = new int[10];
        }
    No:

    Code:
    for(int i = 0; i < 10; i++)
        {
            *(myLocations + i) = new int[10];
        }
    Or better:

    Code:
    for(int i = 0; i < 10; i++)
        {
           myLocations[i] = new int[10];
        }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Why not use vector<vector<int> >? You're already trying to get familiar with lists and sets, and a vector is a much better choice than a plain dynamic array.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Daved View Post
    Why not use vector<vector<int> >? You're already trying to get familiar with lists and sets, and a vector is a much better choice than a plain dynamic array.
    Also possible... But you'd still have to do some initialization:

    Code:
    // 10x10 array
    vector<vector<int> > array_2d(10);
    for(int i = 0; i < 10; i++)
    {
        array_2d[i].reserve(10);
    }

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> But you'd still have to do some initialization:
    Certainly, but I would use this:
    Code:
    vector<vector<int> > array_2d(10, vector<int>(10));
    Also note that if you did do it in a loop, you'd want resize rather than reserve.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Daved View Post
    >> But you'd still have to do some initialization:
    Certainly, but I would use this:
    Code:
    vector<vector<int> > array_2d(10, vector<int>(10));
    Also note that if you did do it in a loop, you'd want resize rather than reserve.
    Good catch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating and freeing dynamic arrays
    By circuitbreaker in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2008, 10:18 AM
  2. Replies: 16
    Last Post: 01-01-2008, 03:07 PM
  3. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 02:28 PM
  4. processing dynamic arrays
    By Mario F. in forum C++ Programming
    Replies: 9
    Last Post: 06-04-2006, 11:32 AM
  5. Dynamic (Numeric) Arrays
    By DavidB in forum C++ Programming
    Replies: 5
    Last Post: 05-03-2006, 07:34 PM

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