13

I'm new to C/C++ and I've been cracking my head but still got no idea how to make an "structure" like this

alt text

It's supposed to be a 3D dynamic array using pointers.

I started like this, but got stuck there

  int x=5,y=4,z=3;
  int ***sec=new int **[x];

It would be enough to know how to make it for a static size of y and z;

Please, I'd appreciate that you help me.

Thanks in advance.

justin
  • 104,054
  • 14
  • 179
  • 226
Marco Aviles
  • 5,516
  • 6
  • 33
  • 47

7 Answers7

23

To create dynamically 3D array of integers, it's better you understand 1D and 2D array first.

1D array: You can do this very easily by

const int MAX_SIZE=128;
int *arr1D = new int[MAX_SIZE];

Here, we are creating an int-pointer which will point to a chunk of memory where integers can be stored.

2D array: You may use the solution of above 1D array to create a 2D array. First, create a pointer which should point to a memory block where only other integer pointers are held which ultimately point to actual data. Since our first pointer points to an array of pointers so this will be called as pointer-to-pointer (double pointer).

const int HEIGHT=20;
const int WIDTH=20;

int **arr2D = new int*[WIDTH];  //create an array of int pointers (int*), that will point to 
                                //data as described in 1D array.
for(int i = 0;i < WIDTH; i++){
      arr2D[i] = new int[HEIGHT]; 
}

3D Array: This is what you want to do. Here you may try both the scheme used in above two cases. Apply the same logic as 2D array. Diagram in question explains all. The first array will be pointer-to-pointer-to-pointer (int*** - since it points to double pointers). The solution is as below:

const int X=20;
const int Y=20;
const int z=20;

int ***arr3D = new int**[X];
for(int i =0; i<X; i++){
   arr3D[i] = new int*[Y];
   for(int j =0; j<Y; j++){
       arr3D[i][j] = new int[Z];
       for(int k = 0; k<Z;k++){
          arr3D[i][j][k] = 0;
       }
   }
}
colombien
  • 149
  • 6
Manish Shukla
  • 562
  • 6
  • 18
  • Also you can use this method for N-Dimension arrays with N-1 for loops. – Amir Zadeh Oct 11 '10 at 08:43
  • @Manish Shukla In the 3D Array code which of X, Y , and Z represents Width, height and depth ?? – Mariya Apr 16 '14 at 16:40
  • 1
    @Mariya - Don't think 3Dimentional array as a box which has width/height/depth. The way we constructed 3D array is first we had one simple 1D array, every element of this array points to another 1D array and again every element of which points to again 1D array which will hold real values. If you can visualize this then you will have ans to your question, if you want to take this as width/height/depth. If you still need help, please leave a comment. – Manish Shukla Apr 21 '14 at 12:24
  • @ManishShukla I think of it this way because I'm trying to implement a dynamic programming matrix similar to Needlman wunsch algorithm – Mariya Apr 22 '14 at 13:21
  • Don't forget to `delete[]` the nested arrays backwards when you stop using them. – Wouterr Apr 05 '20 at 11:41
11
// one-liner
typedef std::vector<std::vector<std::vector<int> > > ThreeDimensions;
// expanded
typedef std::vector<int> OneDimension;
typedef std::vector<OneDimension> TwoDimensions;
typedef std::vector<TwoDimension> ThreeDimensions;

(this is tagged c++, after all)

EDIT in response to Joe's question

hello again Joe =) sure. here's the example:

#include <vector>
#include <iostream>

int main(int argc, char* const argv[]) {

    /* one-liner */
    typedef std::vector<std::vector<std::vector<int> > >ThreeDimensions;
    /* expanded */
    typedef std::vector<int>OneDimension;
    typedef std::vector<OneDimension>TwoDimensions;
    typedef std::vector<TwoDimensions>ThreeDimensions;

    /*
       create 3 * 10 * 25 array filled with '12'
     */
    const size_t NElements1(25);
    const size_t NElements2(10);
    const size_t NElements3(3);
    const int InitialValueForAllEntries(12);

    ThreeDimensions three_dim(NElements3, TwoDimensions(NElements2, OneDimension(NElements1, InitialValueForAllEntries)));

    /* the easiest way to assign a value is to use the subscript operator */
    three_dim[0][0][0] = 11;
    /* now read the value: */
    std::cout << "It should be 11: " << three_dim[0][0][0] << "\n";
    /* every other value should be 12: */
    std::cout << "It should be 12: " << three_dim[0][1][0] << "\n";

    /* get a reference to a 2d vector: */
    TwoDimensions& two_dim(three_dim[1]);

    /* assignment */
    two_dim[2][4] = -1;
    /* read it: */
    std::cout << "It should be -1: " << two_dim[2][4] << "\n";

    /* get a reference to a 1d vector: */
    OneDimension& one_dim(two_dim[2]);

    /* read it (this is two_dim[2][4], aka three_dim[1][2][4]): */
    std::cout << "It should be -1: " << one_dim[4] << "\n";
    /* you can also use at(size_t): */
    std::cout << "It should be 12: " << one_dim.at(5) << "\n";

    return 0;
}
justin
  • 104,054
  • 14
  • 179
  • 226
1

You can try:

for(int i=0;i<x;i++) {
  sec[i] = new int *[y];
  for(int j=0;j<y;j++) {
    sec[i][j] = new int [z];
  }
}

And once you are done using this memory you can deallocate it as:

for(int i=0;i<x;i++) {
  for(int j=0;j<y;j++) {
    delete [] sec[i][j];
  }
  delete [] sec[i];
}
delete [] sec;
codaddict
  • 445,704
  • 82
  • 492
  • 529
1

Comprehensive answers.

If you are really writing this in C++ (not rough C) I think you should take another look at this complicated data structure. IMO redesign while keeping in mind what you are trying to do would be better.

Keynslug
  • 2,676
  • 1
  • 19
  • 20
1

What you're trying to do is not idiomatic in C++. Of course, you can use a int***pointer for this, but this is strongly discouraged. In C++ we have better ways to get there.

vector<vector<vector<int> > > foo (5,vector<vector<int> >(4, vector<int>(3)));

This will result in something with the memory layout similar to what you asked for. It supports dynamic resizing and inner vectors to have different sizes just like in your picture. In addition, you don't have to worry about manual allocation / deletion of any of it. Also, the vectors know their size so you don't have to remember it somewhere.

But if you just want a "rectangular" 3D array where all the elements are consecutivly stored in the same memory block, you could use a boost::multiarray.

sellibitze
  • 27,611
  • 3
  • 75
  • 95
0

OK let us take your beginnings

int ***sec = new int**[x]; 

sec is now an array of int**s of length x, so now I am just going to focus on making the zeroeth element be what you want

sec[0] = new int*[y];

Now sec[0] points to array of int*s of length y, now just need to get the last bit of the tree done, so

sec[0][0] = new int[z];

And finally to get it to the form in your diagram

sec[0][0][z-1] = 0;

This does seem a little like a homework question, make sure you actually understand the answer and why it works.

Aurojit Panda
  • 909
  • 6
  • 11
  • `sec` is not an array. It's a pointer that points to the first element of an array. – sellibitze Oct 11 '10 at 09:33
  • That is rather semantic. To be fair in cases like int x[10]; x is really a pointer to the first element of an array, and an array both. There really isn't a difference between being a pointer to the first element of an array and being an array. – Aurojit Panda Oct 11 '10 at 17:50
-1

If it's the actual arrays you'r having problems with look here: Declaring a pointer to multidimensional array and allocating the array

Not sure exactly what you want but you might want to read up on about linked lists.

Community
  • 1
  • 1
dutt
  • 7,909
  • 11
  • 52
  • 85