gm2
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Gm2] double free or corruption with pointers


From: Martin Kalbfuß
Subject: Re: [Gm2] double free or corruption with pointers
Date: Tue, 03 Aug 2010 11:45:51 +0200

Many Thanks,

MODULE array;

IMPORT Storage, SYSTEM, STextIO, SWholeIO;

CONST maxArraySize = 32768;
TYPE DynArrayPtr = POINTER TO ARRAY[0..maxArraySize] OF INTEGER;
VAR myArray : DynArrayPtr;
VAR runtimeSize : INTEGER;

BEGIN
     runtimeSize := 5;
     Storage.ALLOCATE(myArray, SYSTEM.TSIZE(INTEGER)*runtimeSize);
     myArray^[3] := 5;
     SWholeIO.WriteInt(myArray^[3], 1);
     STextIO.WriteLn();           
     Storage.DEALLOCATE(myArray,SYSTEM.TSIZE(INTEGER)*runtimeSize);     
END array.

This works like I need it. But the boundaries aren't checked any longer
like they are for static arrays. :-(


Am Montag, den 02.08.2010, 17:34 +0000 schrieb Fischlin Andreas:
> Dear Martin,
> 
> I see many problems with your code:
> 
> NEW(myArray, 6, 6) 
> 
> is not a legal ISO statement, since you use it for a pointer type. Then the 
> only additional legal argument to 'myArray' would be the tag in case that the 
> record to which the pointer points is of the variant case kind. But that is 
> not the case in your situation.
> 
> You seem to want to program almost in assembler. But I don't see the need, 
> since you could easily write
> 
> CONST noOfArrEles = 6;
> TYPE DynArray = ARRAY [0.. noOfArrEles-1] OF INTEGER;
>    DynArrayPtr = POINTER TO DynArray;
> VAR myArray:  DynArrayPtr;
> ...
> NEW(myArray, TSIZE(INTEGER)*noOfArrEles);
>       or even safer
> NEW(myArray, TSIZE(DynArray));
> 
> I am still a bit confused about the fact that I am not sure you really want 
> to allocate even a two dimensional integer array. However, above code could 
> be easily expanded accordingly.
> 
> This code is more Modula-2 like and should accomplish the same in a more 
> legal manner.
> 
> Regards,
> Andreas
>  
> 
> ETH Zurich
> Prof. Dr. Andreas Fischlin
> Systems Ecology - Institute of Integrative Biology
> CHN E 21.1
> Universitaetstrasse 16
> 8092 Zurich
> SWITZERLAND
> 
> address@hidden
> www.sysecol.ethz.ch
> 
> +41 44 633-6090 phone
> +41 44 633-1136 fax
> +41 79 221-4657 mobile
> 
>              Make it as simple as possible, but distrust it!
> ________________________________________________________________________
> 
> 
> 
> On 01/Aug/2010, at 11:55 , Martin Kalbfuß wrote:
> 
> > Hi! The following program ends up with a double free corruption. I'm not
> > sure how this can happen. Anything wrong with my code?
> > 
> > MODULE test2;
> > 
> > IMPORT SYSTEM, SWholeIO, STextIO;
> > FROM Storage IMPORT ALLOCATE, DEALLOCATE;
> > 
> > TYPE DynArray = POINTER TO INTEGER;
> > VAR  myArray : DynArray;
> >     elementPtr : DynArray;
> > BEGIN
> >     NEW(myArray, 6, 6);
> >     elementPtr := SYSTEM.ADDADR(myArray, 2*5+3);
> >     SWholeIO.WriteInt(elementPtr^, 1); STextIO.WriteLn();
> >     DISPOSE(myArray);
> > END test2.
> > 
> > 
> > Thanks,
> > Martin
> > 
> > 
> > _______________________________________________
> > gm2 mailing list
> > address@hidden
> > http://lists.nongnu.org/mailman/listinfo/gm2
> 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]