[comp.sys.mac.programmer] Help with port to LSC of command line-driven program

strasser@murdu.OZ (Mike Strasser) (08/08/88)

I'm new to programming Macs and have a program in C which has run
successfully on PCs, VAXes and a Unix machine (so I know it works). 
It's a forest growth simulation model for which I've written a nifty
command line interface using scanf().

I want to eventually re-write the program to run on Macs to be used for
my own research and for teaching, but before that I want to get it going
in a "quick-and-dirty" fashion first (for other reasons).

We have LSC 2.15 (soon to order 3.0) and 3/5 of IM (I'm waiting for the
bookshop to get new stocks of vols I & III).  I've read of people
running their programs under LSC without problems, but I appear to be
unlucky. 

When I try to run the program I get nowhere because it can't even read
the first file properly before it either stops with a spurious error or
bombs.  I don't know enough 68k asm to understand Macsbug.

The routine which bombs is being called from another through one of
these structures:

typedef struct {
  char Name[9];
  void (*FuncPtr)();
} FuncVar;

(there's an array of them).  Each associates a name with a function
pointer so that the user types the name of the function and it gets
called, thus:

      (*Funcs[i].FuncPtr)();

(where Funcs is the aforementioned array).  

Could this be the problem?  The offending function is essential before
anything else is done with the model.  This function reads numbers from
file using fscanf() into members of structures which belong to a linked
list whose storage is handled by malloc() and free().  Should I do
anything else about memory allocation on a Mac (short of re-writing
quite a bit of code).  Perhaps the problem is with memory allocation.  

What about program segmentation? Do I need to have particular functions
in the same segment? All functions pointed to in the array Funcs (and
the calling function) are in the same segment.  Does <stdio> need to be
there too (it's huge: 18+ K)?

Can someone please give me some ideas, or tell me what other information
I need to provide?  I have nothing to go on at the moment.  The whole
program is c. 3000 lines of code, but I could mail bits of it if necessary.

---------------------------------------------------------------------------
Mike Strasser                ACSnet, CSnet:  strasser@murdu.oz
                             Internet:       strasser%murdu.oz@uunet.uu.net
Forestry Section
University of Melbourne
Creswick, Victoria.  3363    Phone: (053) 45 2405
A u s t r a l i a                   +61 53 45 2405
---------------------------------------------------------------------------

beard@ux1.lbl.gov (Patrick C Beard) (08/11/88)

I couldn't send this direct, using following address...
To: strasser@munnari.UUCP
Subject: Re: Help with port to LSC of command line-driven program
Newsgroups: comp.sys.mac.programmer
In-Reply-To: <1426@murdu.OZ>
Organization: Lawrence Berkeley Laboratory, Berkeley
Cc: 
Bcc: 

In article <1426@murdu.OZ> you write:
>I'm new to programming Macs and have a program in C which has run
>successfully on PCs, VAXes and a Unix machine (so I know it works). 
>...
>When I try to run the program I get nowhere because it can't even read
>the first file properly before it either stops with a spurious error or
>bombs.  I don't know enough 68k asm to understand Macsbug.
>
>The routine which bombs is being called from another through one of
>these structures:
>
>typedef struct {
>  char Name[9];
>  void (*FuncPtr)();
>} FuncVar;
>

This seems perfectly acceptable.

>(there's an array of them).  Each associates a name with a function
>pointer so that the user types the name of the function and it gets
>called, thus:
>
>      (*Funcs[i].FuncPtr)();
>

Have you thought about what might happen if a string of longer than
8 characters (with the '\0' that makes 9) was placed into Name[9]?  If
the array bounds were blown up, the FuncPtr could point to random memory.
If you reversed the order of these in the structure, you would at least
not have to worry about the function calls dying (although something
else would get screwed up!).

>(where Funcs is the aforementioned array).  
>
>Could this be the problem?  The offending function is essential before

As I said above.
>
>What about program segmentation? Do I need to have particular functions
>in the same segment? All functions pointed to in the array Funcs (and
>the calling function) are in the same segment.  Does <stdio> need to be
>there too (it's huge: 18+ K)?
>

As for segmentation, the placement of functions in various segments is
only critical for memory management, and if you refer to any function
that is in another segment, a jump table entry is generated for it
even if the function is declared static!  LSC does this and it is
quite nice.  Stdio needn't be in the same segment and the functions
pointed to by Funcs needn't be in the same segment as the calling
function.  Period.

>Can someone please give me some ideas, or tell me what other information
>I need to provide?  I have nothing to go on at the moment.  The whole
>program is c. 3000 lines of code, but I could mail bits of it if necessary.
>...
>Mike Strasser                ACSnet, CSnet:  strasser@murdu.oz
>                             Internet:       strasser%murdu.oz@uunet.uu.net

I hope this helps.

Patrick Beard
Lawrence Berkeley Laboratory
beard@ux1.lbl.gov