Board index » delphi » 4 Turbo PASCAL Questions for the more advanced.

4 Turbo PASCAL Questions for the more advanced.

In article <31420999.11816...@news.fastlane.net>, THEAnal...@Nfo.Org
says...
-
-Question #1:
-   Can someone tell me the differences between using New/Destroy and
-GetMem/FreeMem?  I see everyone on this group using GetMem/FreeMem but
in my
-classes all we are taught is New/Destroy.

     Standard Pascal uses the procedures new() and dispose() to handle
dynamic memory.  Given a pointer variable (of any type), these procedures
allocate (or deallocate) the appropriate amount of memory for the thing
pointed to (sorry about that phrase).  I believe GetMem and FreeMem are
Borland extensions that need to be told how much memory to get or free,
and are thus somewhat more dangerous, and definitely less portable.

-Question #2:
-   A while back I remember glancing at one of my text books on pascal
and seeing
-something like "PROCEDURE Foo( Bar : PROCEDURE (t:Char; I:integer);" and
this
-would allow you to pass any procedure to Foo that was defined like
"(t:Char;
-I:integer)" so that you could call Bar as though it were that other
procedure.
-   I can't remember which text book had the information (I have 4 and
each over
-900 pages).
-   Any suggestions?

     This is the Standard Pascal method of passing "procedure
parameters".  An example of where this might be useful -- you can write a
procedure to integrate a function by using, say, the trapezoidal rule.  
It will integrate any function you pass to it.  By using a function
parameter, your "integral" function will be able to integrate any other
function you might be able to code.

   FUNCTION integral (F : FUNCTION (x : real) OF real;
                      lowlimit, hilimit : real);

    BEGIN   { integral }
     { compute the integral of F for x between lowlimit and hilimit }
    END;

    FUNCTION myfunction (x : real) : real;

    BEGIN   { myfunction }
     myfunction := 1 + sin (sin (x))
    END;
{ more code }
    writeln ('The integral of 1 + sin(sin(x)) from 0 to 1 is ',
             integral (myfunction, 0, 1));

Bob Schor
Pascal Enthusiast

Quote
>Question #3:
>   In the TP 7 help there are built in procedures that have optional
parameters.
>I have seen a few functions, like I think "Allocated" (not "allocate"),
that
>return a boolean value if the user has put a variable on the command
line of a
>function/procedure, but I cannot figure out how to define a
procedure/function
>that can do this.

>Question #4:
>   I have examples of how to compile a PASCAL file to .com in TP 3,
however TP 7
>won't let me do that and I don't have access to a TP 3 compiler anymore.
>   And at this very moment I don't have access to my TP 3 examples to
show you
>what won't work in TP 7.  Sorry. . . :(

>--
>Plase POST all replies as I have not given a real e-mail address in the
header,
>and others may want answers to my questions.

 

Re:4 Turbo PASCAL Questions for the more advanced.


I believe GetMem and FreeMem are
Quote
>Borland extensions that need to be told how much memory to get or free,
>and are thus somewhat more dangerous, and definitely less portable.

 Why less portable? Would you describe C's 'malloc' (which does the same thing)
as non-portable? A byte is a byte on whatever platform you are working on.
Unfortunatly, Turbo Pascal is far from portable simply because it is not as
standardized as C/C++. Too many people regard it as a teaching language when
in fact TP is a very extensive and respectable compiler with most of the
features you would find in C.

Tim Lewis.

Re:4 Turbo PASCAL Questions for the more advanced.


In article <314fd2a...@news.1starnet.com>, Donald Johnson
Quote
<jwe...@stargate.1starnet.com> wrote:

New/Dispose vs GetMem/FreeMem:
 ...
Quote
>Example for GetMem/FreeMem.

><********* Start of Code *********>
>Program Mem_Demo;
>{$R-}

Type
  Rec = Record
    Field1 : String;
    Field2 : Integer;
  End;
  RecAry      : Array[1..1] of Rec;
 ...

Quote

>When using this form of dynamic allocation, you need to make sure that
>you turn off Range Checking.  If you do not, you will get a range check
>error as soon as you try to access element #2.

AFAIR, if one uses instead :

     RecAry      : Array[1..65520 div SizeOf(Rec)] of Rec;

then rangechecking can be left on, which is a Good Thing, although it
will be ineffective in a RecAry.  Just possibly that should be a 65519,
see the manuals.

I think you missed an "^" in

Quote
>  For Cntr := 1 to Count do
>    Read(InFile, Ary[Cntr] );

...........................^

--
John Stockton, Surrey, UK.  Turnpike v1.12.  MIME.

Other Threads