0

I have the following code that deals with reading matrix which uses double pointers. I would like to understand why would author choose to use double pointers as follows:

int N=10;
int i,j;
open = (long double**)calloc(N+2, sizeof (long double*));
for(i=0;i<N+2;i++)
  open[i]=(long double*)calloc(N+2, sizeof (long double));

Usage

long double coord[500][2];
for(i=0;i<N+1;i++){
    fscanf(fin,"%s %s",column1,column2);
    coord[i][0]=atof(column1);
    coord[i][1]=atof(column2);
for(i=0;i<N;i++){
    for(j=0;j<N;j++)
        open[i][j]=sqrt(pow(coord[i][0]-coord[j][0],2.)+pow(coord[i][1]-coord[j][1],2.));

I failed to see how is the matrix initialized with double pointer while only a single array is declared open[i] instead of open[i][j]

gfdsal
  • 651
  • 1
  • 8
  • 25
  • What's the context? Are those strings or something else? What's `open`? – Lundin Jun 11 '20 at 13:54
  • @Lundin `open` is basically `NxN` matrix. So there are coordinates and `open` is basically holding the distances between full graph of coordinates. – gfdsal Jun 11 '20 at 14:00
  • [dont cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar Jun 11 '20 at 14:03
  • 1
    We need to know how it was declared. I'm not playing yet another game of "guess the type". Also, `open` happens to be the name of a very common Unix/POSIX/DOS function that has existed since the dawn of time, so the name choice isn't exactly brilliant. – Lundin Jun 11 '20 at 14:03
  • @Lundin my apologies, I have updated the code to introduce minimal example to understand the underlying reference. `open` here is just a variable name and not any special function. – gfdsal Jun 11 '20 at 14:12
  • 1
    @gfdsal Sorry for being grumpy, but you still haven't shown the declaration of `open`. – Lundin Jun 11 '20 at 14:18
  • 2
    Anyway, it's quite likely that the original programmer simply didn't know how to [Correctly allocate multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays). It's very common. The TL;DR is that allocations like in your code doesn't make any sense unless you actually need individually variable dimensions, like in a table of strings. – Lundin Jun 11 '20 at 14:21
  • @Lundin thanks, I shall review the link. – gfdsal Jun 11 '20 at 14:33
  • 1
    By allocating this in an actual 2D matrix, the floating point numbers can be data cached and that might potentially do wonders for performance. Similarly, that would make parallel processing of the array feasible, if this is some calculation-intense code you need to run over and over. – Lundin Jun 11 '20 at 14:37
  • @Lundin correct, this is calculation-intensive travelling salesman problem. – gfdsal Jun 11 '20 at 14:39
  • @Lundin one last point to convey since you made me realize an important issue. declaration as suggested in the answer gives segmentation error```long double **open; open = calloc(N+2, sizeof (long double*));```. while the code as is working fine. – gfdsal Jun 11 '20 at 14:55

1 Answers1

0

Presumably the declaration is

long double **open;

The first malloc() allocates an array of pointers. Each pointer points to a row of the matrix.

The malloc() calls in the for loop allocate memory for each row of the matrix.

This is how you allocate a 2-dimensional matrix dynamically.

Since you're allocating a matrix of long double, you need to use that type in the sizeof calls.

open = calloc(N+2, sizeof (long double*));
for(i=0;i<N+2;i++)
  open[i]=calloc(N+2, sizeof (long double));
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I have updated the code. Please do see if its still valid. `open` seems to be of type `long double` due to `long double coord[500][2];` What I dont understand is how double pointers is exactly allocating the matrix dynamically. So first we are allocating the array of the matrix and then for each cell in the array we are creating rows? – gfdsal Jun 11 '20 at 14:18
  • 2
    Rather, you allocate a 2D matrix dynamically with: `unsigned char (*open)[N+2] = calloc(1, sizeof unsigned char[N+2][N+2] );`... `free(open);`. – Lundin Jun 11 '20 at 14:23
  • 2
    @gfdsal The double pointers _isn't_ allocating a 2D matrix, they merely give an array-like syntax when de-referencing it. The allocation in your code is all over the place, fragmented all over the heap and needlessly slow because of that. – Lundin Jun 11 '20 at 14:25
  • @Lundin i see makes little sense now. Also so add there is no explicit declaration such as mentioned here `long double **open;` I believe the declaration is in `open = (long double**)calloc(N+2, sizeof (unsigned char*));` so open is indeed the type `long double ` – gfdsal Jun 11 '20 at 14:32
  • @Lundin yes, I agree allocation is all over the place and bit tricky to grasp. I am just trying to grasp double pointers concept by rereading the answer and your comments. – gfdsal Jun 11 '20 at 14:33
  • @Barmar Declaration as suggested in the answer. compiles fine but gives segmentation error while executing```long double **open; open = calloc(N+2, sizeof (long double*));```. while the code as is working fine. – gfdsal Jun 11 '20 at 14:56
  • You probably have a bug somewhere else in the program and it's corrupting the heap. – Barmar Jun 11 '20 at 15:04