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: Fischlin Andreas
Subject: Re: [Gm2] double free or corruption with pointers
Date: Mon, 2 Aug 2010 17:34:05 +0000

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]