退出,代码=3221226356!!!!!在 VSCode 中

问题描述 投票:0回答:1

我正在尝试读取包含加速度数据(x、y、z 轴)的 .csv 文件。文件格式如下:

BBD66,0.000125,0,0.000875 BBD66,0.000125,0,0.000125 BBD66,0,-0.000125,-0.000625 BBD66,-0.00025,0.000125,0.000125 BBD66,0,0,0.000875 BBD66,0,0,0.000125 BBD66,0.000125,0,-0.000125 BBD66,0,0,0.00025 BBD66,-0.000125,0,0.000375

依此类推,直到第 8192 行。

我创建了一个包含 819 个随机数(从 0 到 8191)的数组“perm”,因此当我读取文件时,我只会保存与该“perm”数组上的数字对应的行。

代码如下(不包括生成数字的代码,因为这些行没有问题)

#define MAXCHAR 1024
#define N 8192
#define p 819

................................................ ................................

  /////////////////////////// READING SAMPLES FILE ///////////////////////////
  FILE *file_accel;

  // Allocate the array of pointers
  char **x_samples = (char **)malloc(p * sizeof(char *));
  char **y_samples = (char **)malloc(p * sizeof(char *));
  char **z_samples = (char **)malloc(p * sizeof(char *));

  // Allocate rows (max length 15 characters, including \0)
  for (int i = 0; i < p; ++i) {
    x_samples[i] = (char *)malloc(15 * sizeof(char));
    x_samples[i] = '\0';
    y_samples[i] = (char *)malloc(15 * sizeof(char));
    y_samples[i] = '\0';
    z_samples[i] = (char *)malloc(15 * sizeof(char));
    z_samples[i] = '\0';
  }

  file_accel = fopen("report_accelerometer_BBD66_80Hz_full.csv", "r");

  if (!file_accel) {
    printf("Can't open file\n");
    return 1;
  }

  // Here we have taken size of
  // array 1024 you can modify it
  char buffer[MAXCHAR];

  int row = 0;
  int column = 0;

  // compare each perm[i] with the corresponding readed line in the file
  for (int i = 0; i < p; i++) {
    while (fgets(buffer, MAXCHAR, file_accel)) {

      if (row != perm[i]) {
        row++;
        continue;
      } else {
        column = 0;

        // Splitting the data
        char *value = strtok(buffer, ",");
        if (i == 818) {
          printf("STOP RIGHT HERE TO SEE WHAT THE FUCK IS GOING ON\n\n");
        }

        printf("Data %d (row %d): ", i, perm[i]);

        while (value) {
          // Column 1
          if (column == 0) {
            value = strtok(NULL, ",");
            column++;
            continue;
          }

          // Column 2
          if (column == 1) {
            x_samples[i] = value;
            printf("%s ", x_samples[i]);
          }

          // Column 3
          if (column == 2) {
            y_samples[i] = value;
            printf("%s ", y_samples[i]);
          }

          // Column 4
          if (column == 3) {
            z_samples[i] = value;
            printf("%s ", z_samples[i]);
          }

          // printf("%s ", value);
          value = strtok(NULL, ",");
          column++;
        }

        printf("\n");
        row++;
      }
      break;
    }
  }

  // Close the file
  fclose(file_accel);
  /////////////////////////// READING SAMPLES FILE ///////////////////////////

  free(perm);

  // free memory
  for (int i = 0; i < p; i++) {
    free(z_samples[i]);
    free(y_samples[i]);
    free(x_samples[i]);
  }
  free(x_samples);
  free(y_samples);
  free(z_samples);

当我运行代码时,会在输出中弹出此内容

数据 712(第 6879 行):0 0 0

数据 713(第 6897 行):0.000125 0 0

数据 714(第 6908 行):0 0 -0.000125

数据 715(第 6924 行):0 0 -0.000375

数据 716(第 6933 行):0.0 [完成] 0.288 秒内退出,代码=3221226356

当我调试它时,它会打印出每一行,直到数据818,如下所示:

数据 814(第 8170 行):0 0 0.000125

数据 815(第 8176 行):-0.000125 -0.000125 -0.0015

数据 816(第 8180 行):0 -0.000125 0

数据 817(第 8183 行):0.000125 0.000125 -0.00025

停下来看看到底发生了什么

数据 818(第 8186 行):0 0 0.000125

但是当它到达这里的第一个空闲线时:

for (int i = 0; i < p; i++) {
    free(z_samples[i]);
    free(y_samples[i]);
    free(x_samples[i]);
}

经过几次Step Over后,弹出异常。未知信号

释放分配的内存时出现问题。但不知道是什么:(

c dynamic-memory-allocation
1个回答
0
投票

这没有任何意义:

x_samples[i] = (char *)malloc(15 * sizeof(char));
x_samples[i] = '\0';

这是一个厚颜无耻的错误,因为

'\0'
实际上是一个空指针常量,因此它会“在编译器的雷达下”通过。实际上,您丢弃了分配的内存并将其保留为泄漏,让指针变成空指针。

您可能打算将指向的 contents 设为 null:

x_samples[i][0] = '\0';

至于如何读取乱码Windows错误代码,十进制代码3221226356并没有告诉jack。转换为十六进制后,它变成 0xC0000374,Windows 错误代码往往以 0xC000 开头...具体来说,这是堆损坏,这有助于了解何时跟踪错误。

© www.soinside.com 2019 - 2024. All rights reserved.