我正在写一个C程序,但是我在指针和正确分配内存方面遇到了一点麻烦。下面是我的代码:
void read_file(char* file_name, float***** data, unsigned char**** flagged,
unsigned char**** initial_flagged)
{
int nr_stations;
int nr_times;
int nr_subbands;
int nr_subbands_in_file;
int nr_channels;
int nr_polarizations;
int i,j,k;
int err;
/*Unnecessary code omitted*/
//allocate memory for data and flags.
*data = (float****) malloc(sizeof(float***) * nr_times);
if(*data == NULL){
perror("Error allocating memory for data buffer:");
exit(1);
}
*flagged = (unsigned char***) malloc(sizeof(unsigned char **) * nr_times);
if(*flagged == NULL){
perror("Error allocating memory for flag buffer:");
exit(1);
}
*initial_flagged = (unsigned char***) malloc(sizeof(unsigned char **) * nr_times);
if(*initial_flagged == NULL){
perror("Error allocating memory for flag buffer:");
exit(1);
}
for(i=0;i<nr_times;i++){
*data[i] = (float ***) malloc(sizeof(float**) * nr_subbands);
if(*data[i] == NULL){
perror("Error allocating memory for data on time");
exit(1);
}
*flagged[i] = (unsigned char **) malloc(sizeof(unsigned char *) * nr_subbands);
if(*flagged[i] == NULL){
perror("Error allocating memory for flags on time");
exit(1);
}
*initial_flagged[i] = (unsigned char **) malloc(sizeof(unsigned char *)
* nr_subbands);
if(*initial_flagged[i] == NULL){
perror("Error allocating memory for initial_flags on time");
exit(1);
}
for(j=0;j<nr_subbands;j++){
*data[i][j] = (float **) malloc(sizeof(float*) * nr_polarizations);
if(*data[i][j] == NULL){
perror("Error allocating memory for data on subband at time");
exit(1);
}
for(k=0;k<nr_polarizations;k++){
*data[i][j][k] = (float *) malloc(sizeof(float) * nr_channels);
if(*data[i][j][k] == NULL){
perror("Error allocating memory for data on on polarization on subband at time ");
exit(1);
}
memset(*data[i][j][k], 0, sizeof(float) * nr_channels);
}
*flagged[i][j] = (unsigned char*) malloc(sizeof(unsigned char) * nr_channels);
if(*flagged[i][j] == NULL){
perror("Error allocating memory for flags on subband at time");
exit(1);
}
*initial_flagged[i][j] = (unsigned char*) malloc(sizeof(unsigned char) *
nr_channels);
if(*initial_flagged[i][j] == NULL){
perror("Error allocating memory for initial flags on subband at time");
exit(1);
}
memset(*flagged[i][j], 0, sizeof(unsigned char) * nr_channels);
memset(*initial_flagged[i][j], 0, sizeof(unsigned char) * nr_channels);
}
}
int main(int argc, char** argv){
float**** data = NULL;
unsigned char*** flagged = NULL;
unsigned char*** initial_flagged = NULL;
if(argc != 2){
printf("Usage: flagger <filename>n");
exit(1);
}
read_file(argv[1], &data, &flagged, &initial_flagged);
printf("data = %pn", data);
return 0;
}
当我运行这段代码时,它在
处出现分段错误*数据[我][j] [k] =(*)浮动malloc (sizeof(浮动)* nr_channels);
然而,当我不传递指针到数据,标记或initial_flag的方法,但在函数范围内声明它们,这工作得很好,但我不能在函数外使用这些数组。
在C语言中,解引用操作符(即*)的级别或优先级低于数组下标操作符(即[])。
因此,操作*data[i][j]
等价于*(data[i][j])
。在你的程序中,它导致了一个段故障,因为它解释了data[i][j]
的值作为一个地址,并试图访问存储在这个地址的数据,而它实际上是一个浮点数。
因此,在这种情况下,您必须使用括号,如(*data)[i][j]
。
参见C/c++操作符前序
好吧,我自己弄明白了(我猜是橡皮鸭调试)。显然,*data[i][j]和(*data)[i][j]是有区别的。第二个就是我的意思,它使我的代码正确运行!