realloc + memcpy 二维浮点数组导致分段错误

Realloc + memcpy 2D float array results in segmentation fault

我做了一个结构体(SomeMisc),它有一个浮点数组,所以我可以用一些值填充它,然后尝试将它的浮点数组 memcpy 到另一个结构的浮点数组,并打印出结果看看是否成功了。

另一个结构 (ArrayPairs) 应该包含两个数组数组。所以当我想对“一对”做一些改变时,low[i] 属于 high[i],反之亦然。

所以我制作了 2 个 SomeMisc 对象,用数字填充它们的数组,然后尝试制作一个函数,首先用 realloc 扩展 ArrayPairs 对象的低数组和高数组,然后我尝试 malloc space 到新行,然后最后从作为函数参数给出的 2 个 SomeMisc 成员数组中 memcpy 内容。

但它一直导致分段错误 and/or 未定义的行为,我不明白为什么。

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <time.h>
#include <stdlib.h>

typedef struct some{
  int32_t len;
  float* arr;
} SomeMisc;

typedef struct arrPrs{
  int32_t amountOfRows;
  int32_t amountOfColumns;
  float** low;
  float** high;
} ArrayPairs;

void initializeArrayPairArray(ArrayPairs* AP, int32_t length, int32_t width){

  AP->amountOfRows = length;
  AP->amountOfColumns = width;

  AP->low = (float**)malloc(length * sizeof(float*));
  AP->high = (float**)malloc(length * sizeof(float*));

  for(int i=0; i<length; i++){
    AP->low[i] = (float*)malloc(width * sizeof(float));
    AP->high[i] = (float*)malloc(width * sizeof(float));
    for(int j=0; j<width; j++){
      AP->low[i][j] = 32;
      AP->high[i][j] = 44;
    }
  }
}

void addArrayPair(ArrayPairs* AP, float* low, float* high){
  AP->amountOfRows++;

  AP->low = (float**)realloc(AP->low, AP->amountOfRows * sizeof(float*));
  AP->high = (float**)realloc(AP->high, AP->amountOfRows * sizeof(float*));

  AP->low[AP->amountOfRows] = (float*)malloc(AP->amountOfColumns * sizeof(float));
  AP->high[AP->amountOfRows] = (float*)malloc(AP->amountOfColumns * sizeof(float));

  memcpy(AP->low[AP->amountOfRows], low, AP->amountOfColumns * sizeof(float));
  memcpy(AP->high[AP->amountOfRows], high, AP->amountOfColumns * sizeof(float));

  printf("TESTING PRINT: %.2f\n", AP->high[10][5]);
}

int main () {
  int32_t nrOfCols = 8;
  int32_t nrOfRows = 10;

  ArrayPairs arr;
  initializeArrayPairArray(&arr, nrOfRows, nrOfCols);

  int32_t mArrLength = 2;
  SomeMisc* mArr = (SomeMisc*)malloc(mArrLength*sizeof(SomeMisc));
  for(int i=0; i<mArrLength; i++){
    mArr[i].arr = (float*)malloc(nrOfCols*sizeof(float));
    for(int j=0; j<nrOfCols; j++){
      mArr[i].arr[j] = (i+1)*j;
    }
  }

  addArrayPair(&arr, mArr[0].arr, mArr[1].arr);

  printf("LOW:\tHIGH:\n");
  for(int i=9; i<arr.amountOfRows; i++){
    printf("INDEX: %d\n",i);
    for(int j=0; j<arr.amountOfColumns; j++){
      printf("%.2f\t%.2f\n",arr.low[i][j],arr.high[i][j]);
    }
    printf("\n");
  }

  return(0);
}

我遵循了这个答案:2d array realloc Segmentation Fault Error

但是我在addArrayPair的参数列表中已经有了ArrayPairs* AP,并且在调用函数时&带有对象arr

我也尝试按照该答案中的建议取消引用,但这也不起作用:

void addArrayPair(ArrayPairs* AP, float* low, float* high){
  (*AP).amountOfRows++;

  (*AP).low = (float**)realloc((*AP).low, AP->amountOfRows * sizeof(float*));
  (*AP).high = (float**)realloc((*AP).high, AP->amountOfRows * sizeof(float*));

  (*AP).low[AP->amountOfRows] = (float*)malloc((*AP).amountOfColumns * sizeof(float));
  (*AP).high[AP->amountOfRows] = (float*)malloc((*AP).amountOfColumns * sizeof(float));

  memcpy((*AP).low[(*AP).amountOfRows], low, (*AP).amountOfColumns * sizeof(float));
  memcpy((*AP).high[(*AP).amountOfRows], high, (*AP).amountOfColumns * sizeof(float));
}

你增加 AP->amountOfRows 太早了。这意味着当您执行 AP->low[AP->amountOfRows] 时,您将使用 out-of-bounds 索引,并且具有 未定义的行为

改为(重新)分配 AP->amountOfRows + 1 个元素,并在所有分配和复制完成后增加 AP->amountOfRows

void addArrayPair(ArrayPairs* AP, float* low, float* high){
  AP->low = realloc(AP->low, (AP->amountOfRows + 1) * sizeof(float*));
  AP->high = realloc(AP->high, (AP->amountOfRows + 1) * sizeof(float*));

  AP->low[AP->amountOfRows] = malloc(AP->amountOfColumns * sizeof(float));
  AP->high[AP->amountOfRows] = malloc(AP->amountOfColumns * sizeof(float));

  memcpy(AP->low[AP->amountOfRows], low, AP->amountOfColumns * sizeof(float));
  memcpy(AP->high[AP->amountOfRows], high, AP->amountOfColumns * sizeof(float));

  // Increase once all is done
  AP->amountOfRows++;
}