[comp.lang.fortran] dynamic storage

lmeyer@eniac.seas.upenn.edu (Basya Meyer) (03/06/91)

As a 'c' programmer who must work in fortran, I need your help!  Is there
any way in fortran to create something (roughly approximating) a stack or 
linked list?  I would like to allocate spaces in memory as I go along.

Thanks,

Basya

roth@dtoa1.dt.navy.mil (Roth) (03/06/91)

In article <38747@netnews.upenn.edu> lmeyer@eniac.seas.upenn.edu (Basya Meyer) writes:
>As a 'c' programmer who must work in fortran, I need your help!  Is there
>any way in fortran to create something (roughly approximating) a stack or
>linked list?  I would like to allocate spaces in memory as I go along.
>
>Thanks,
>
>Basya

Assuming your compiler is Fortran 77 or earlier:

Part 1: implement your stack as an array, and use an integer
as a pointer into it. Briefly, without error checking, the data looks like
		integer stkptr
		real(?) stack( 100 )
		data stkptr / 0 /
& a push looks like
		subroutine push( areal )
		if ( stkptr .LT. 100 ) then
			stkptr = stkptr + 1
			stack( stkptr ) = areal
		else
			print *, 'stack overflow'
		endif
		end
etc.

Part 2: linked list as two arrays: first array is integer, and holds the
index of the location of the datum in the second array.
		integer lstptr( 100 )
		real(?) list( 100 )

an ordered list will have lstptr(1) = 1, lstptr(2) = 2, etc.

Part 3: Allocate memory as you go along is not fortran unfortunately,
but your system may have an interface that allows you to do that.

Apologies for not having something I can just e-mail you.

Recommended reading: Day, A. Colin, FORTRAN TECHNIQUES, Cambridge University
Press, 32 E 57th St, NYNY, 1979 (latest edition) ISBN 0 521 09719 3 paper
ISBN 0 521 08549 7 hardback

Also: Kernighan & Plauger, SOFTWARE TOOLS, Addison Wesley (I think; it looks
like someone stole my copy...)

Peter N Roth      roth@dtoa1.dt.navy.mil
Objects in this office are closer than they appear.

session@uncw.UUCP (Zack C. Sessions) (03/07/91)

lmeyer@eniac.seas.upenn.edu (Basya Meyer) writes:

>As a 'c' programmer who must work in fortran, I need your help!  Is there
>any way in fortran to create something (roughly approximating) a stack or 
>linked list?  I would like to allocate spaces in memory as I go along.

>Thanks,

>Basya

There is no true dynamic memory allocation possible with standard
FORTRAN. As another follow-up poster has shown, implementation of
stacks, linked lists (as well as queues, etc.) is nothing more than
an exercise in Data Structures. What some do (if both a C AND a FORTRAN
compiler is available) is use C subroutines to perform the dynamic
allocation.

Zack Sessions
session@uncw.UUCP

khb@chiba.Eng.Sun.COM (Keith Bierman fpgroup) (03/07/91)

In article <1050@uncw.UUCP> session@uncw.UUCP (Zack C. Sessions) writes:


   an exercise in Data Structures. What some do (if both a C AND a FORTRAN
   compiler is available) is use C subroutines to perform the dynamic

C is irrelvant. It is really an OS service issue. On many systems
(VAX, Sun, CDC, etc.) you can do it in extended Fortran. You may
prefer BLISS, Cybil, assembler or old Norse .....
--
----------------------------------------------------------------
Keith H. Bierman    kbierman@Eng.Sun.COM | khb@chiba.Eng.Sun.COM
SMI 2550 Garcia 12-33			 | (415 336 2648)   
    Mountain View, CA 94043

session@uncw.UUCP (Zack C. Sessions) (03/07/91)

khb@chiba.Eng.Sun.COM (Keith Bierman fpgroup) writes:


>In article <1050@uncw.UUCP> session@uncw.UUCP (Zack C. Sessions) writes:


>   an exercise in Data Structures. What some do (if both a C AND a FORTRAN
>   compiler is available) is use C subroutines to perform the dynamic

>C is irrelvant. It is really an OS service issue. On many systems
>(VAX, Sun, CDC, etc.) you can do it in extended Fortran. You may
>prefer BLISS, Cybil, assembler or old Norse .....
>Keith H. Bierman    kbierman@Eng.Sun.COM | khb@chiba.Eng.Sun.COM

I did not imply that C was _required_ to do dynamic memory allocation.
Certainly other languages do as well, most notably PL/1 with it's
ALLOCATE statement. I was merely pointing out an alternative since
dynamic memory allocation is NOT part of standard FORTRAN, but MAY
be available with some implementations of *Extended* FORTRAN. Oh,
and since I *did* suggest C, it may be obvious that I would prefer C.

Zack Sessions
session@uncw.UUCP

lmeyer@eniac.seas.upenn.edu (Basya Meyer) (03/07/91)

In article <KHB.91Mar6151242@chiba.Eng.Sun.COM> khb@chiba.Eng.Sun.COM (Keith Bierman fpgroup) writes:

>(VAX, Sun, CDC, etc.) you can do it in extended Fortran. You may
>prefer BLISS, Cybil, assembler or old Norse .....


	Thank you to all who replied--you have been very helpful and 
	informative.  I think I'll try the old Norse....

	Basya