Just needed some guidance with a small problem that I'm having. It's really a simple project that I am doing but keep on getting the same error after running my program (Segmentation Fault (Core Dumped)).
Essentially, I am writing a program to calculate the mean and variance of a 3D cube (rows, columns, and bands). The code should do this for each band with respect to the rows and columns, which is basically one "image".
Each image has three classes which read either 1,2,or 3. This function below reads the cube, stores it into a buffer and then should find the mean and variance.
I've tried all sorts of variations to solve this problem and I am really at a loss. If anyone has any solution to this problem, it would be very appreciated.
short *** ps_buf = newshort** [i_num_bands]; //allocates enough space for elements This doesn't allocate storage for any elements. It only allocates an array or "short**" pointers, each pointing to something undefined. Accessing this location causes a segfault.
You need to allocate each of these pointers to an array of "short*", and each of those an array of "short".
1 2 3 4 5 6
short *** ps_buf = newshort** [i_num_bands];
for( int k = 0; k < i_num_bands; k++) {
ps_buf[k] = newshort*[i_num_rows];
for( int j = 0; j < i_num_rows; j++)
ps_buf[k][j] = newshort[i_num_cols];
}
Note that you also need to reverse [i][j][k] to [k][j][i] in your code.