# Thread: dynamic allocation from 1 instead of zero

1. Also spotted another problem: if (!mat) inside the for-loop should be if(!mat[i]).

Freeing should just take a pointer - and of course you should free in reverse order, so start with the loop, and finally free the mat pointer.

--
Mats

2. Ops, my distreaction about [i].

Here I post my "free" function. From what you mentioned it should be correct.

Code:
```void free_darray2d(double **mat, size_t nrows, size_t ncols, int index)
{
//nrows = number of rows
//ncols = ncolumns
//index = 1 if you want allocation in the fortran way starting from 1
//index = 0 if you want the standard C allocation way starting from 0

long i;

for(i=index; i<nrows+index; i++)
free(mat[i]);

free(mat);

return;
}```

On the other hand, for a 1D array, by analogy, there is no need to set a vect[0] = NULL since I allocate it as I did for the row pointer of the 2D matrix; am I saying it right?

Code:
```//Double 1d array
void darray1d(double *vect, size_t nrows, int index)
{
//nrows = number of rows
//index = 1 if you want allocation in the fortran way starting from 1
//index = 0 if you want the standard C allocation way starting from 0

long i;

//Malloc pointers to rows
vect = malloc((nrows+index)*sizeof(double*));
if (!vect){
printf("Matrix MAT could not be allocted");
exit(1);
}

return;
}```
Here I post the 1d array al;location as well. This is my last question!
Thank you very much

3. Ok, so I just spotted another mistake: you are comparing i <= nrows (in both allocation and freeing). That should be "i < nrows+index", and i = index should be your starting point. [Actually your free code is CORRECT, but if you use <, then you don't need the -1, which makes the code simpler to understand].

--
Mats

4. Right; writing on a forum gets me quite distracted apparently!

thanks for spotting them out.
It should work now; I will post something if I get errors in compilations

Thank you again
cfd

5. Hi again,

I unfortunately get a BUS error with the function that I posted.
I then tried to modify it by making it a pointer to function since I need to return the matrix that I want to allocate, and instead of a BUS error, I now get a segmentation fault

what could that be?

thank you in advance
best,

Code:
```int **iarray2d(size_t nrows, size_t ncols, int index)
{
//nrows = number of rows
//ncols = ncolumns
//index = 1 if you want allocation in the fortran way starting from 1
//index = 0 if you want the standard C allocation way starting from 0

long i;
int **mat;

//Malloc pointers to rows
mat= malloc((nrows+index+1)*sizeof(int*));
if (!mat){
printf("Matrix MAT could not be allocted");
exit(1);
}

mat[0] = NULL;

for(i=index; i<nrows+index+1; i++){
mat[i] = malloc((ncols+index)*sizeof(int*));
if (!mat[i]){
printf("Matrix MAT could not be allocted\n The exec. will Exit now\n");
exit(1);
}
}

return mat;
}```

6. Originally Posted by cfdprogrammer
Hi again,

... instead of a BUS error, I now get a segmentation fault
Apologies, I solved the segmentation fault. I was freeing the memory twice!
So, to sum it up: I get a bus error with the original version of the function, but it works fine when I use a pointer to function instead.
What would be the cause to it? Are they not equivalent?

thanks

7. Ah, yes, to modify a pointer to pointer, you need a pointer to pointer to pointer (so int ***mat, and use *mat inside the function - it may be better to use a new variable inside the function, and before the function returns, you set the *mat = tmpMat or whatever you call it).

--
Mats

8. Originally Posted by matsp
Ah, yes, to modify a pointer to pointer, you need a pointer to pointer to pointer (so int ***mat, and use *mat inside the function - it may be better to use a new variable inside the function, and before the function returns, you set the *mat = tmpMat or whatever you call it).

--
Mats
Great, thank you very much.
I'll use the pointer to function now since it works well as it is.

Regards,
cfd

9. I now get a new error in execution:

this line repeated many many times
Code:
```meteoIni.a(32753) malloc: *** error for object 0x30303130:
Non-aligned pointer being freed (2)```
does anyone know what is happening?
thanks

10. Probably that you are freeing something that you didn't allocate.

--
Mats

11. Originally Posted by matsp
Probably that you are freeing something that you didn't allocate.

--
Mats
Hi,

that is the strange thing, since I am only freeing what I am allocating.
I am allocating 6 different variables with this new function, and what is strange about the error that I get, is that it is only happening when I allocate ONE of the six variables, and this variable is not even the last to be allocated and freed

thank you again

12. You are probably writing to some memory that you didn't intend to write to, or some such.

--
Mats

13. Thank you, I'll figure it out although I can't explain it. It worked with no problem when I was allocating it with the NR library with the same range
strange

best

Popular pages Recent additions