Date: Mon, 7 Feb 2000 18:36:14 +0200 (IST) From: Eli Zaretskii X-Sender: eliz AT is To: Martin Chan cc: djgpp AT delorie DOT com Subject: Re: Memory allocation In-Reply-To: <389EC5B4.AA3D2666@mailserv.cuhk.edu.hk> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Reply-To: djgpp AT delorie DOT com Errors-To: dj-admin AT delorie DOT com X-Mailing-List: djgpp AT delorie DOT com X-Unsubscribes-To: listserv AT delorie DOT com Precedence: bulk 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.