传递指针和分配内存会导致段错误

时间:2014-07-03 06:53:59

标签: c pointers segmentation-fault malloc

我正在编写一个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 = %p\n", data);
  return 0;


}

当我运行此代码时,它会在

处发生段错误
  

* data [i] [j] [k] =(float *)malloc(sizeof(float)* nr_channels);

但是,当我没有将指针传递给数据,标记或初始化为方法但在函数范围内声明它们时,这可以正常工作,但是我不能在这些数组之外使用这些数组。功能。

2 个答案:

答案 0 :(得分:2)

在C中,解除引用运算符(即*)的级别或优先级低于数组下标运算符(即[])。

因此,操作*data[i][j]相当于*(data[i][j])。在你的程序中,它导致了一个段错误,因为它将data[i][j]处的值解释为一个地址,并试图访问存储在该地址的数据,而它实际上是一个浮点数。

因此,在这种情况下,您必须使用括号,例如(*data)[i][j]

请参阅C/C++ Operator Predecence

答案 1 :(得分:0)

好的,所以我自己已经弄明白了(我猜是橡皮鸭调试)。 显然,* data [i] [j]和(* data)[i] [j]之间存在差异。第二个是我的意思,让我的代码正确运行!