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

标签 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);

但是,当我不将指向数据、标记或 initial_flagged 的​​指针传递给方法,而是在函数范围内声明它们时,这工作正常,但我不能在函数外部使用这些数组。

最佳答案

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

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

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

参见 C/C++ Operator Predecence

关于c - 传递指针和分配内存导致段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24546887/

相关文章:

opengl - rviz 段错误(核心已转储)

c - 结构和段错误

c - 获取具有十六进制值的 char 并将该值作为整数返回

c - 将同一个常数移动另一个会产生两个不同的答案?

C区分指向Char的指针和指向Char数组的指针

c++ - 将一个数组的引用分配给另一个数组 - "warning: target of assignment not really an lvalue"

c++ - 我需要在 Windows 上同步对 HANDLE 的访问吗?

android - 在android apk中的不同 Activity 中加载.so文件时的内存共享

c++ - 如何将方法指针传递给结构(在 C/C++ 中)?

c - 为什么我的递归程序会出现段错误?