delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/02/07/13:14:45

Date: Mon, 7 Feb 2000 18:36:14 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
X-Sender: eliz AT is
To: Martin Chan <s987517 AT mailserv DOT cuhk DOT edu DOT hk>
cc: djgpp AT delorie DOT com
Subject: Re: Memory allocation
In-Reply-To: <389EC5B4.AA3D2666@mailserv.cuhk.edu.hk>
Message-ID: <Pine.SUN.3.91.1000207183129.20861D-100000@is>
MIME-Version: 1.0
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

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019