Reading 2d array from binary file and return the pointer this array (in C)

醉酒当歌 提交于 2019-12-04 13:58:33

The problem is that there is no 2D array in your code. The pointer-to-pointer look-up table thing is not a 2D array. It is [rows] number of segments scattered all over the heap, at random places. It is therefore also needlessly slow.

Also, you should keep memory allocation and algorithms separated.

Do something like this instead:

#include <stdio.h>
#include <stdlib.h>

void* allocArray (int rows, int cols)
{
  return malloc( sizeof(double[rows][cols]) ); // allocate 1 2D-array
}

void readArray (int rows, int cols, double array[rows][cols])
{
   FILE *data;
   data=fopen("matrix.bin", "rb");
   fread(array, sizeof(double[rows][cols]), 1, data); // read 1 2D-array
}

int main ()
{
  int cols = 7;
  int rows = 15;
  double (*myArray)[cols] = allocArray(rows, cols);

  readArray(rows, cols, myArray);

  printf("%f\n", myArray[1][0]);

  free(myArray); // free 1 2D-array
  return 0; 
}

The reason for the peculiar declaration double (*myArray)[cols] instead of the more logical double (*myArray)[rows][cols], is that we want to avoid the inconvenient array pointer de-referencing syntax. (*myArray)[1][0] is not easy to read. So instead of declaring an array pointer to a 2D array, declare an array pointer to a 1D array, then use pointer indexing on that array pointer. For any pointer, any_pointer[n] gives pointed-at item number n. Array pointers are no difference, so you get 1D array number n.

Your fread() call is overwriting all those pointers you painfully set up.

You need to read a single row at a time, and use the set-up pointer to store to:

for(size_t i = 0; i < rows; ++i)
  fread(myArray[i], cols * sizeof *myArray[i], data);

Also, when doing I/O and memory allocation you should check the return values too, of course.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!