Creating multidimensional array at class

Hi,
My problem is that ,
I have two class,
first class is generating only generating parameters(3 parameters M,N,Tap_Length).
second class is genarating 3 dimensional array from these paremeters
like PDB[M][N][Tap_Length].
I defined first class at the second class to pass the values(member initilization at constructor).
For 1 dimension, there is no problem and code below do this.only do
PDB[TAP_LENGTH].But I cant for 3 dimension.How can I do?
I tried to declare my problem clearly on the code below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <cmath>
#include <iostream>
#include <limits>
using namespace std;
///first Class definition
class Dim
{
public:
Dim(int, int, int);

void setPdb();//Set the Power Delay Profiles
int  get_tap_length();//returns the tap length
void setTdL_Scenario(int );//Set the TdL_Scenario
void setAntennaNum(int,int);// Set the Antenna numbers.
int getNN();//returns the NN length
int getMM();//returns the MM value
void show();
//Data Members
private:
int Pdb[];
int tap_length;
int TdL_Scenario;//input to the system
int NN; // Number of receive antennas(input to the system)
int MM; // Number of transmit antennas(input to the system)

};

//Constructor of Dim//
Dim::Dim(int m,int n, int scenario)

{

    setTdL_Scenario(scenario);
	setPdb();
	setAntennaNum(m, n);

}
//End of Constructor

//Member function//setTdL_Scenario
void Dim::setTdL_Scenario( int scenario)
{
    TdL_Scenario = scenario;
}
//End of Member function//setTdL_Scenario

//Set Power Delay Profiles
void Dim::setPdb()
{
    switch(TdL_Scenario)
    {
        case 1:
            Pdb[0] = 1;Pdb[1]=3;
            tap_length =2;
            break;
        case 2 :
            Pdb[0] =3; Pdb[1] = 6;Pdb[2] = 2;
            tap_length = 3;
            break;

        default :
		cout<< "İnvalid TDL_SCENARİO";

	}
}
//End of Member function//Set Power Delay Profiles


//Member function//setAntennaNum
void Dim::setAntennaNum(int m,int n)
{
    NN = n;
    MM = m;

}
//End of Member function//setAntennaNum

int Dim::get_tap_length()
{
        return (tap_length);
}
int Dim::getNN()
{
        return (NN);
}
int Dim::getMM()
{
        return (MM);
}


//New class(Second class definition)//
////////////
//My problem is that,
//I am trying to declare multiple dimensional array at the class Generateq
//(class below),with paremeters of the Class Dim such as MM,NN,Tap_Length
//like PDB[M][N][TAP_LENGTH]
////////////I am using composition such as defining Dim class at the
                            //Generateq class
class Generateq

{
    public:
        Generateq(Dim &);

    private:
    const int TAP_LENGTH ;//These values are my dimensional paremeters.
    const int N;//////////I get these values by member initilazer at the
    const int M;///////// constructor

    double *PDB;// this is not what I want(one dimensional)
    //double PDB[TAP_LENGTH][N][M];
    //At this point Iam trying to declare multipledim array
    //This sample is wrong

    Dim dimension;


};

Generateq::Generateq(Dim &dimdim)
        :dimension(dimdim),
        TAP_LENGTH(dimdim.get_tap_length()),
        N(dimdim.getNN()),//I am getting values
        M(dimdim.getMM())//from previous class Dim
{
cout<<"Constructor";
PDB = new double[TAP_LENGTH];//Create 1 dimension array.
}

///Main Code///
int main()
{
Dim a(2,2,1);/// Dim class definition
Generateq generate(a);//Generateq ,class definition


  std::cout <<"   Press ENTER to continue...";
  std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );

return(0);
}

In Dim, where do you define?

 
int Pdb[];


it seems only declared but no memory is allocated.

Inside Generateq (if now the array should be doubles) you need to declare

 
double **PDB;


then allocate PDB:

 
double PDB = new double*[N]; // if N is rows 

then for each row in PDB allocate M number of doubles

1
2
3
4
for (int i = 0; i < N; ++i )
{
   PDB[i] = new double[M];
}



now you can access each element with PDB[i][j]

(although I personally would use vector of vectors instead or some other template depending on the type of the data)

hth

Thanks.
Your code works properly
I have not seen anywhere this kind of using "new" with "*".At books or internet.Can you please tell me of logic of this using?
1
2
3
4
5
6
double **PDB;
double PDB = new double*[N]; // if N is rows 
for (int i = 0; i < N; ++i )
{
   PDB[i] = new double[M];
}

I have questions

Why did not work this code instead of your code?,
and is there diffrence?
1
2
3
double PDB[N][M];
double PDB  = new double[N][M];

2)For 3 dimensional case is it proper way of
defining(Lets say dimensions N,M,A) ?
double ***PDB;
but how can I do remaning parts?.




Last edited on
If you didn't want to deal with new/delete you could use a vector of vectors.

1
2
3
4
vector<vector<double> > 2dArray;
2dArray.resize(N);
for (int i = 0; i < N; ++i)
 2dArray[i].resize(M);


The new* is saying create me a list of pointers to the objects. So you want it to create you an array of double pointers N sized.

The new doesn't take 2 arguements of size, only 1. So you code doesn't work because new doesn't support a method call of that nature.

2) for a 3D Array, you can do multiple vectors embedded.

e.g
vector<vector<vector<double> > > 3dArray;

or

1
2
3
4
5
6
7
8
double ***3DArray;

3DArray = new double**[Array1];
for(int i = 0; i < Array1; ++i) {
 3DArray[i] = new double*[Array2];
 for (int j = 0; j < Array2; ++j)
  3DArray[i][j] = new double[Array3];
}


However, as Ander's has failed to point out. When using the dynamic method by allocating with the new keyword you MUST de-allocate the memory yourself. This involves doing the opposite you've done with the new calling delete.

When using the embedded vectors, this is not required and the memory is freed automatically.
Oh!!
Thank you very much Zaita for these useful info.
Topic archived. No new replies allowed.