重新分配+ memcpy 2D 浮点数组导致分段错误

发布于 2025-01-18 09:53:52 字数 3567 浏览 4 评论 0原文

我制作了一个带有浮子数组的结构(Somemisc),因此我可以填充一些值,然后尝试将其浮点数阵列记忆到其他结构的浮点阵列,然后打印出结果以查看它是否有效。

另一个结构(arraypairs)应该容纳两个阵列。因此,当我想对“一对”进行一些更改时,低[i]属于高[i],反之亦然。

因此,我制作了2个somemismisc对象,用数字填充数组,然后尝试在我扩展带有Realloc的Arraypairs对象的低阵列和高阵列的地方,然后我尝试将新行悬挂到新行,然后最终来自给出的两个Somemisc成员阵列的Memcpy内容作为函数的参数。

但这会导致细分故障和/或未定义的行为,我不知道为什么。

#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 arreay realloc seggentation dudd efform误差

但是我已经有arraypairs* apaddarraypair的参数列表中,以及&amp;带有object arr&amp;

我还尝试了该答案所建议的那样进行删除,但这也没有用:

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));
}

I made a structure (SomeMisc) which has a float array, so I can fill it with some values, and then try to memcpy its float array to a different struct's float array, and print out the result to see if it worked.

The other structure (ArrayPairs) is supposed to hold two arrays of arrays. So that low[i] belongs to high[i] and vice versa when I want to make some changes on "a couple".

So I make 2 SomeMisc objects, fill their arrays with numbers, and then try to make a function where I expand the low-array and high-array of the ArrayPairs object with realloc firstly, then I try to malloc space to the new rows, and then finally memcpy content from the 2 SomeMisc member arrays given as arguments to the function.

But it keeps resulting in segmentation faults and/or undefined behavior, and I can't figure out why.

#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);
}

I followed this answer: 2d array realloc Segmentation Fault Error

But I already have the ArrayPairs* AP in the parameter list of addArrayPair, and the & with the object arr when calling the function.

I also tried dereferencing as was suggested in that answer, but this didn't work either:

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));
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏了南城 2025-01-25 09:53:52

您会为时过早增加ap-&gt; MANTEROFROWS。这意味着,当您进行ap-&gt; low [ap-&gt; noseofrows]时,您将使用距离索引,并具有 dem> nofection crathive

而不是(re)分配ap-&gt; MANTEROFROWS + 1元素,并增加ap-&gt; MANTEROFROWS一旦完成了所有分配和复制:

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++;
}

You increase AP->amountOfRows too early. That means when you do AP->low[AP->amountOfRows] you will use an out-of-bounds index, and have undefined behavior

Instead (re)allocate AP->amountOfRows + 1 elements, and increase AP->amountOfRows once all allocations and copying is done:

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++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文