[comp.sys.ibm.pc] Oddities in Turbo Pascal 4.0

madd@bu-cs.BU.EDU (Jim Frost) (06/26/88)

I've been making real heavy use of Turbo Pascal 4.0 recently and I've
come across some oddities that other users might like to be aware of.
The ones I have found had to do with what happens when you close
unopened files and a problem with the new exec function.

As anyone who uses 3.0 and 4.0 knows, there are quite a few
differences in the way files are handled now.  Some of these
differences introduced subtle incompatibilities.  Try the following
program under each and see the difference:

{ oddity.pas:

  this program shows an oddity in the way TP 4.0 works
}

program oddity;

var f : text;

{$i-}
begin
  writeln('Before close function');
  close(f);
  writeln('This will not print under TP 4.0');
  if ioresult <> 0 then
    ;
  writeln('Done.')
end.

What will happen:  Under 3.0, the (obviously invalid) close will not
affect anything.  Under 4.0, nothing will print (although no runtime
error will be given) until ioresult is checked.  This difference had
me running in circles for awhile, since I used to code this like this:

  reset(f);
  if ioresult <> 0 then begin
    close(f);                      { just to be sure }
    writeln('Error opening file'); { tell user }
    halt                           { back to the world of mashdos }
  end;

Another problem I ran into had to do with the new exec function in the
dos unit.  The exec function *can* cause memory allocation problems if
you goof with the environment.  I was working with a shell program so
I'd changed the vector at memw[prefixseg:$002C] (PSP environment
segment pointer) to point to my new environment.  An exec function
would work properly, but would leave the DOS memory allocation
information in a munged state; further exec's would crash the system.
This isn't really a bug in 4.0 (the Tech Ref guide says that touching
that vector is a no-no) but it's something I ran across.  My solution
was to use the exec function to call 'command /c' to run my program.
This is additional overhead that I didn't want, but it worked.

Note: does anyone know any way of determining the size of the
environment area given to a process?  I need this and the Tech Ref
Guide doesn't exactly tell you much about it.  In fact, it doesn't
tell you anything.  Any help is greatly appreciated.

For those of you who don't have 4.0 yet, get it.  It makes programming
in Pascal worthwhile (ok, possible).  It's too bad that you can't
alter the initialization code for a program; if you could, 4.0 would
be almost powerful enough to write an operating system in; you almost
never need machine code now.  I'm tempted to try to write an OS in it
anyway, but I suspect that it's a bit too closely tied to the MS-DOS
environment for that.

Happy hacking,

jim frost
madd@bu-it.bu.edu

raphael@hpisoa2.HP.COM (Bert Raphael) (06/28/88)

While we're on the subject, here's a tip on Turbo Pascal 4 that had
me going in circles for a while: 
The manual says that if you have several units used in your program,
the order of their names in the 'uses' statement does not matter.
NOT TRUE!  If the same identifier (variable, constant, procedure, etc.)
appears in the interface portion of more than one used unit, the system
gives you NO indication of the possible conflict, but merely uses the
copy in the most recently loaded Unit.  If you have Units using other
Units in creative ways, the resulting conflicts can be amazing.
I had many overlays in Pascal 3, with various amounts of duplication
in their code.  Simply converting each one to a Pascal 4 Unit caused
all kinds of problems, until I went through and made the Units essentially
disjoint.  There is not much help from the system to do such restructuring,
but the results are worth it!

I too would like some advice on understanding the environment set up by
Turbo Pascal 4.  When I tried adding the TAccess Unit (from the DataBase
ToolKit) to my large system, I got a "Heap error--103", which doesn't go
away no matter where I put the Heap boundaries, and I don't know what
RAM limits I'm exceeding, or what parameter settings or code modifications
might help!  Anyone have any suggestions?