Thread: 2D-Arrays Re-sized

  1. #1
    Registered User
    Join Date
    Jun 2003

    2D-Arrays Re-sized

    Here's the scenario:

    In a class, I have a two dimensional array, let's call it: 2DArray.
    To declare it, I use:
    int 2DArray[20][20];

    Now, naturally, it is an array of integers, holding 441 (21 x 21) values.

    But, this "20" I use is pretty open to change. Sometimes I might feel like throwing in a 50, sometimes a 2, it depends on my current mood. Now, the problem is, I use this number "20" many many times throughout my class AND main program, so, going through each time and changing the "20"s that apply can get rather tiresome.

    What I was hoping was to find a way that I could make a variable within my class, to hold the dimensions of my 2DArray (it will always be symmetrical, I.e. 1x1 or 5x5 or 50x50), and then declare a 2D Array based on that variable? That way I'll also be able to retrieve the value from my main app and use the number there if calculations are needed.


  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Umm... Actually, it only holds 400 (20x20) values.

    At any rate, to solve your problem, I recommend one of the following:
    1) Use dynamic memory allocation; just be sure to clean up properly when you resize, which would be a pain to deal with.
    2) Use a constant for your values: 'const int ARRAY_SIZE = 20;' somewhere in your code, and then use ARRAY_SIZE where you had used your literal value (only requiring one change and then a recompile).
    3) Use STL and a vector inside a vector: i.e. 'vector<vector<int> > m2DArray'.

    The best is the last suggestion. The first is the most dangerous if you are not very familiar/comfortable with dynamic memory allocation.


  3. #3
    Registered User
    Join Date
    Jun 2003
    Bah, you're right, [20] = [0 to 19], not [0 to 20]

    And as for those three options...I have no clue about anything that Options 1 or 3 suggest But I'm liking the simplicity (and distance from messing with memory) that number 3 has, but I have no clue what STL is, or how I could use vectors for it...but I really don't want to go messing with memory allocation.

    I'll give #3 a shot and I'll most likely be back here in the near future with a couple of questions Thanks


  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Good call staying away from #1. As for #3, try this thread for some pointers to STL info:

    Vectors are actually quite nice. You can treat them just like arrays, and they are resizable (with their own internal memory management). The one thing I should warn you of is the inefficiency of resizing an array/vector in such a manner, though perhaps this is not much of an issue. Just make sure that all of your vectors get resized.

    A fourth idea (which is likely overkill) is to find a library with a good matrix class.

    Good luck.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    For simplicity, at the expense of some memory, you could do

    const int MAX_SIZE = 30;
    typedef int twoDArray[MAX_SIZE][MAX_SIZE];

    Sure it wastes some memory if all you want is a 5x5, but so long as the max isn't too big, and you don't have too many of them (like hundreds), then it's a pretty easy way to deal with the problem.

    void foo ( twoDArray bar, size_t current_size );
    Pass the array, and whatever it's current size is (up to the max), and everything takes care of itself.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 2d arrays
    By thamiz in forum C Programming
    Replies: 25
    Last Post: 05-25-2008, 05:06 AM
  2. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  3. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  4. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 09:05 PM
  5. how can i pass 2d arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2001, 07:33 AM