Segmentation Fault (Core Dumped)

Hi fellow programmers,

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.



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

int readCube(FILE * inp, FILE * inp2, int i_num_elem,int i_num_bands, int i_num_cols,int i_num_rows, int i_num_elem2,int i_num_bands2, int i_num_cols2,int i_num_rows2)
{

short *** ps_buf = new short** [i_num_bands];	//allocates enough space for elements
		
int i_num_read = fread(ps_buf, sizeof(short), i_num_elem, inp);
  if(i_num_read == 0)
  {
    fprintf(stderr, "Error: Input file1 can not be read\n");
    return 1;
  }
 if(i_num_read != i_num_elem )
  {
    fprintf(stderr, "Error: Input file1 does not have the number of rows, columns and bands specified\n");
    return 1;
  }


 int c1 = 0;
 int c2 = 0;
 int c3 = 0;
 float var1 = 0;
 float var2 = 0;
 float var3 = 0;
 float elementsC1 = 0;
 float elementsC2 = 0;
 float elementsC3 = 0;
 float meanClass1[i_num_bands];
 float meanClass2[i_num_bands];
 float meanClass3[i_num_bands];
 float varianceClass1[i_num_bands];
 float varianceClass2[i_num_bands];
 float varianceClass3[i_num_bands];

for( int k = 0; k < i_num_bands; k++)
  for( int j = 0; j < i_num_rows; j++)
	for(int i = 0; i < i_num_cols; i++)
{
	 fread(&(ps_buf), sizeof(short), 1, inp);
	 if(ps_buf[i][j][k] == 1)
		{
	    	c1++;
		elementsC1 = elementsC1 + ps_buf[i][j][k];
		}
	else if (ps_buf[i][j][k] == 2)
		{
			c2 = ++c2;
			elementsC2 = elementsC2 + ps_buf[i][j][k];
		}
		else
		{
			c3 = ++c3;
			elementsC3 = elementsC3 + ps_buf[i][j][k];
		}

		//for mean when each band is complete
		if (i == i_num_cols && j == i_num_rows)
		{
			meanClass1[k] = mean(c1, elementsC1);
			meanClass2[k] = mean(c2, elementsC2);
			meanClass3[k] = mean(c3, elementsC3);


			//for variance
			if(ps_buf[i][j][k] == 1)
			{
				for( int j = 0; j < i_num_rows; j++)
					for(int i = 0; i < i_num_cols; i++)
						var1 = var1 + (ps_buf[i][j][k] - meanClass1[k]);
			}
			else if (ps_buf[i][j][k] == 2)
			{
				 for( int j = 0; j < i_num_rows; j++)
					for(int i = 0; i < i_num_cols; i++)
						var2 = var2 + (ps_buf[i][j][k] - meanClass2[k]);
			}
			else
			{
				for( int j = 0; j < i_num_rows; j++)
					for(int i = 0; i < i_num_cols; i++)
						var3 = var3 + (ps_buf[i][j][k] - meanClass3[k]);
		}
	  

varianceClass1[k] = var1;
varianceClass2[k] = var2;
varianceClass3[k] = var3;

}	

	 
}

		 


delete []ps_buf;
}


While trying to debug, my code runs until after the fread:

1
2
3
4
5
if(ps_buf[i][j][k] == 1)
{
  c1++;
  elementsC1 = elementsC1 + ps_buf[i][j][k];
}


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.

Thanks
 
short *** ps_buf = new short** [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 = new short** [i_num_bands];
for( int k = 0; k < i_num_bands; k++) {
  ps_buf[k] = new short*[i_num_rows];
  for( int j = 0; j < i_num_rows; j++)
    ps_buf[k][j] = new short[i_num_cols];
}


Note that you also need to reverse [i][j][k] to [k][j][i] in your code.
Well that worked perfectly, thanks alot I really appreciate it. My code is finally working!!!!!!
Topic archived. No new replies allowed.