Esixt - 1 year ago 130

Pascal Question

The point is to make it work with pointers. Also, I found similar problem with C++. Its not working with Pascal, well, I prob. missed something.

1) I make types and variable for my pointers:

`type`

PReal = ^double;

PPReal = ^PReal;

var

data : ^PPReal;

2) This is how I allocate memory (x,y,z are dimensions):

`GetMem( data, sizeof(PPReal)*x );`

for i:=0 to x-1 do begin

GetMem( (data+i)^, sizeof(PReal)*y );

for j:=0 to y-1 do begin

GetMem( ((data+i)^+j)^, sizeof(real)*z );

end;

end;

3) And this is how I try to Dispose() it:

`for i:=0 to x-1 do begin`

for j:=0 to y-1 do begin

Dispose( ((data+i)^+j) );

end;

Dispose( (data+i) );

end;

Dispose( data );

However, I'm getting 216(memory access) or 204(invalid pointer operation) runtime errors. Like this. Full code (FreePascal IDE)

Any help appreciated.

Answer Source

The last dispose is mirrored to its getmem, but the other two not. Fix that and it works:

```
for i:=0 to x-1 do begin
for j:=0 to y-1 do begin
writeln(i,' ',j,' *');
Dispose( ((data+i)^+j)^ );
end;
writeln(i,' ',j);
Dispose( (data+i)^ );
end;
Dispose( data );
```

p.s. freemem is the logical companion to getmem, as dispose is to new.