Dear all.
I am trying to define a triple pointer.
The piece of code is
int XYSTRIPS=96;
Double_t *pedestal_value = new Double_t[4];
for(int j=0;j<4;j++) *pedestal_value[j] = new Double_t [96];
for(int j=0;j<4;j++){for(int i=0;i<XYSTRIPS;i++) pedestal_value[j][i]=new Double_t[4];}
When I compile the code (.L xxxx.C++), no error is given, but when I run it (yyy()), I get a segmentation violation error.
could you please help me?
Thanks a lot,
bye,
Francesca
Hi.
Thanks a lot.
I already used the dynamicArray in the past and it works fine.
I was anyhow wondering if it is possible using the structure
int XYSTRIPS=96;
Double_t *pedestal_value = new Double_t[4];
for(int j=0;j<4;j++) *pedestal_value[j] = new Double_t [96];
for(int j=0;j<4;j++){for(int i=0;i<XYSTRIPS;i++) pedestal_value[j][i]=new Double_t[4];}
and, if yes, how.
Thanks a lot!
Regards,
Fra
double *** p = new double **[10];
for (int i = 0; i < 10; ++i) {
p[i] = new double *[20];
for (int j = 0; j < 20; ++j)
p[i][j] = new double[30];
}
But that’s ugly, you can do something like this (probably, will not work with CINT):
#include <iostream>
#include <vector>
class Array3D {
private:
typedef std::vector<double>::size_type size_type;
class Slice {
double *sliceData;
const unsigned nCols;
public:
Slice(double *data, unsigned cols) : sliceData(data), nCols(cols) {}
double * operator [] (size_type i) {return sliceData + i * nCols;}
const double * operator[] (size_type i) const {return sliceData + i * nCols;}
};
public:
Array3D(unsigned depth, unsigned rows, unsigned cols)
: data(rows * cols * depth), nRows(rows), nCols(cols)
{}
Slice operator[] (size_type i) {return Slice(&data[i * nCols * nRows], nCols);}
const Slice operator [] (size_type i) const {return Slice(&data[i * nCols * nRows], nCols);}
private:
mutable std::vector<double> data;
const unsigned nRows;
const unsigned nCols;
};
int main()
{
Array3D arr(5, 3, 4);//Like double arr[5][3][4]
for (unsigned i = 0; i < 5; ++i) {
for (unsigned j = 0; j < 3; ++j) {
for (unsigned k = 0; k < 4; ++k)
arr[i][j][k] = i * 1000 + j * 100 + k * 10;
}
}
std::cout<<arr[4][2][3]<<std::endl;
}
You can easily make this class a template, to work with other types.