Mail Archives: djgpp/2000/02/07/13:14:45
On Mon, 7 Feb 2000, Martin Chan wrote:
> transposed_matrix->array=(double**)calloc(row, sizeof(double*);
> --(transposed_matrix->array); /*offset pointer*/
>
> for(i=1; i<=col; i++)
> {
> transposed_matrix->array[i]=(double*)calloc(col, sizeof(double));
> --(transposed_matrix->array[i]); /*offset pointer*/
> }
You allocated transposed_matrix->array[] with space for "row" pointers,
but then you loop over it from 1 to "col". If "col" is greater than
"row", you overrun array[]; if it's smaller, some of the columns are not
allocated.
Also note that arrays in C begin from offset zero, so you should either
begin from zero and end at row-1, or use array[i-1] in the loop body.
> Then, when I try to free the dynamic array as below:
>
> for(i=1; i<=col; i++)
> free(transposed_matrix->array[i]+1)
> free(transposed_matrix->array+1);
>
> General Protection Exception error happens.
Crashes inside `free' usually mean that you overwrote allocated array
bounds. See section 9.2 of the DJGPP FAQ list for more details.
- Raw text -