Multi-dimensional pointer arrays

Hello,

I am having some trouble using multidimensional arrays with pointers. I am able to create the array whatever size I want with this code from http://community.borland.com/article/0,1410,17224,00.html

float (*temp)[3];

temp = new float[2][3];
temp[0][0] = 2.345;
...

The only important differences are that I am not using floats, I am using a type of my own creation, and the user decides the size of the array in the program. I am new to creating arrays like this and don't know how to do a couple of things:

1. I know that if I create a multidimensional array the 'normal' way, then in function prototypes I have to explicitly give the sizes of all but the first dimension. What am I supposed to do if all the dimensions unknown until run-time?

2. Also on the idea of functions, could someone give me examples of function calls and bodies besides the prototype?

3. Is there anything else I should know about using this kind of array?

4. If I should go about it a different way altogether.

I am using Borland's command line C++ compiler.

Thanks,

Jesse Merriman
jesse_m14020@yahoo.com

Comments

  • [b][red]This message was edited by Darius at 2002-8-7 17:35:48[/red][/b][hr]
    [b][red]This message was edited by Darius at 2002-7-31 22:31:15[/red][/b][hr]
    : Hello,
    :
    : I am having some trouble using multidimensional arrays with pointers. I am able to create the array whatever size I want with this code from http://community.borland.com/article/0,1410,17224,00.html
    :
    : float (*temp)[3];
    :
    : temp = new float[2][3];
    : temp[0][0] = 2.345;
    : ...
    :
    :
    "The only important differences are that I am not using floats"

    Why? BLORG (*temp)[3];temp=new BLORG[2][3]; would work fine as long as BLORG has a default constructor.

    :, I am using a type of my own creation, and the user decides the size of the array in the program. I am new to creating arrays like this and don't know how to do a couple of things:
    :
    : 1. I know that if I create a multidimensional array the 'normal' way, then in function prototypes I have to explicitly give the sizes of all but the first dimension. What am I supposed to do if all the dimensions unknown until run-time?

    Pass the information at run-time. Then there a a variety of answers depending on which definition you use, but see later.

    :
    : 2. Also on the idea of functions, could someone give me examples of function calls and bodies besides the prototype?

    Huh? For this or in general?

    :
    : 3. Is there anything else I should know about using this kind of array?

    Probably lots, but again see later.

    :
    : 4. If I should go about it a different way altogether.
    :

    Good point. See later.

    : I am using Borland's command line C++ compiler.
    :
    That's my compiler. Borland C++ 5.6.

    : Thanks,

    Say thanks AFTER the problem is solved. I (personally) don't like it when people say 'Thanks in advanced'. I'm just a grouchy old bastard like that. Also, it's nice when they say thanks later, not because they say thanks, I could care less, but it lets you know that they (and therefore you and the thread) have reached closure.

    Ok, later is now.

    You didn't specify WHICH Borland compiler you are using, so I'm going to assume you are using 5.5 or better (or one close enough). That version supports STL and the new C++ Standard fine. In that case, use a std::vector. It'll save you a lot of head-aches in the long run. Here's a quick example:

    (assuming you're using namespace std;)
    [code]
    int r,c;
    cin>>r>>c;
    vector< vector > myMatrix(r,c);
    myMatrix[0][0]=5; //or whatever
    cout<<myMatrix.size()<<endl;
    cout<<myMatrix[1].size()<<endl;
    [/code]

    Just so you can see it here's a 3D array (note I don't know if these are the BEST way to declare these using STL, but they work. Hopefully someone else will come in a show a better way if this is not it).
    [code]
    int r,c,d;
    cin>>r>>c>>d;
    vector< vector< vector > > myMatrix(r,vector< vector >(c,d));
    myMatrix[0][0][0]=10;
    [/code]

    Here's some explanation of what this code is doing and some important notes.

    As you (hopefully!) can tell it's making a vector of vectors of vectors of int. The vector constructor takes up two parameters: size and a default value to initalize every element. In both examples we use the implicit int to vector conversion to save having to write out another vector (the first example written more strictly would be:

    vector< vector > myMatrix(r,vector(c));

    ). This should explain the second code example.

    Some things to note.
    a) NOTE THE > > >. Due to the way compilers work the following is erroneous:

    vector> myMatrix;

    The space before (ie vector< vector) is optional (I include it for balance, but that's me) however the space AFTER (the > >) is not. The compiler will misinterpret it as the >> operator. (Actually, Borland catches this and warns you (then seemingly proceeds to choke on a bunch of errors because of it)).

    b) uh, this should go without saying, but I'll say it anyways... you probably want to typedef this to something more manageable than...
    vector< vector< vector > > eg

    typedef vector< vector< vector > > matrix3D;

    still there are issues with that.

    c)There is no need for the sub-vectors to be all the same size. Every vector can be it's own size.

    d) All of this CAN be done with just using new [], BUT then you need to pass length information as well (one way or another). Since the vectors hold there own length it's much simpler this way (and safer).

    e) I'll look into nicer ways of initializing >2D arrays. Of course, you could always make a thin wrapper class that will simplify it, but that has a few problems in the most general cases, though I'd imagine typically it would be useful.

    f) Your class must meet certain requirements for certain uses of vector. For example, it must have a public default constructor if you intend on specifying the size of the vector at it's construction (and other things as well) See some (Standard Template Library) STL documentation to get all the requirements for certain activities.
    Basically they are all very reasonable and should be intuitive.

    Oh yah, I forgot some details.

    A vector is NOT an array, it is an object and therefore it is NOT passed by reference unless you specify it to be. Also, it is a non-trivial class, you DO NOT want to pass it by value (passing a 3D vector of user-defined classes by value would be painful). So, pass by reference if you want to be able to change the array, pass by const reference if you don't. You can pass by pointer as well, but typically reference is better all around for this type of thing.

    You can make a vector of pointers, but not the vector is NOT responsible for deleting them (though a vector of auto_ptr's may work).
    [red]

    Upon further research, I now know that a vector of auto_ptr's either simply won't work right out, or isn't a very good idea. I didn't really think it was when I wrote the preceding, but I didn't know then.

    [/red]
    Also read the STL documentation to see what all you can do with a vector (eg sort, transform, for_each). Note that not all (or maybe ANY) of these are methods of vector. The STL is NOT object-oriented it's generic. The algorithms are SEPARATE from the data structures typically (see ). As always read the documentation.

    "We can't do nothing and think someone else will make it right."
    -Kyoto Now, Bad Religion





  • Hi,

    Okay, I switched to using vectors, read all about them, and its working out great. I don't need anything else, for now anyway. Now I can say thanks and mean it :)

    Jesse Merriman
    jesse_m14020@yahoo.com
  • One thing I realized REAL quick once I learned about Vectors...is that I REALLY don't like to use dynamic arrays.

    In one of my classes (wasn't even a programming class) we had to write a simple program which calculated disk arm scheduling movement based on user input of #of requests, location of requests, starting position of the arm, and # of disk elements. Key requirement was that the size of the array could NOT be static...it HAD to be of the size of #of requests that user stated during run time.

    So many people had problems getting their dynamic set of arrays to work correctly...and ended up sacrificing 20% of their grades and just putting a static large number for their array size. People complained about how long it took them to write the code (some were up to 15-20 hrs!!). I told my teacher (who just happened to be my 2nd level C++/C# programming teacher) how much easier it was since we covered vectors and it only took me 1.5 hrs to write the code.

    My new motto: When a vector can be used...USE IT!
    : Hi,
    :
    : Okay, I switched to using vectors, read all about them, and its working out great. I don't need anything else, for now anyway. Now I can say thanks and mean it :)
    :
    : Jesse Merriman
    : jesse_m14020@yahoo.com
    :

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories