[fpc-pascal]help with pointers?

Matt Emson memsom at interalpha.co.uk
Sun Oct 21 01:24:19 CEST 2001


>This means you must make sure your program can keep track in one way or
>another of which pointers are already initialized and when they have to be
>freed. There is no magic trick to get around this, unless you switch to
>environment in which all memory management is handled for you by the run
>time system (such as Java).

The best advice is to always initialize your pointers to nil before using 
them, and set them back to nil after you have deallocated the memory they 
point to (the second part is necessary in Delphi, please correct me if I'm 
wrong.)

Secondly, if you use the try.. finaly construct whereever possible in 
routines that use local pointer variables, you will save yourself a lot of 
work:

var
  X: ^word;
begin
  X := nil; //initialize X ... possible this could be an allocation from 
new() etc..
  try
     //do something with X
  finally
    if assigned(X) then
       ; //free X
   end;
end;

If you are carefull about the way you lay out your code, the 'assigned' test 
can be removed.

Better still, don't use pointers ;-) Almost everything you can do with 
pointers can be done with classes and references (cf. Java - no pointers)

Matt
-- 
---
Don't have BeOS? Get it! - http://free.be.com/
Wanna keep BeOS alive? Save BeOS - 
http://www.BeFAQs.com/save/saveform.html

-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d? s+++:+ a- C++ UL+ P L++ E---- W- N+ o+ K- w 
O- M V PS PE-- Y PGP- t- 5-- X- R- tv+ b+ DI++ D+ 
G e++ h--- r+++ y+++ 
------END GEEK CODE BLOCK------





More information about the fpc-pascal mailing list