Friday 25 March 2011

Dynamic n-dimension array

This post is a note when I'm working with a 3-d array. Here's the code:

//================= declare the array
float*** datacol_ = new float**[100];
int nrc = 100;
int siz = 100;

//================= populate with value
for(int i = 0; i < 100; i++){
datacol_[i] = new float*[nrc];
for( int icomp = 0; icomp < nrc; icomp++ ){
datacol_[i][icomp] = new float[siz];
for( int idx = 0; idx < siz; idx++){
datacol_[i][icomp][idx] = trcin_.get(idx, icomp);
}
}
}

//================= delete the array
for(int i = 0; i < totnr_; i++){
for(int j = 0; j < trcin_.nrComponents(); j++){
delete [] datacol_[i][j];
}
delete [] datacol_[i];
}
delete [] datacol_;

No comments:

Post a Comment