Sun Studio 12: Fortran User's Guide

3.4.96 –stackvar

Allocate local variables on the stack whenever possible.

This option makes writing recursive and re-entrant code easier and provides the optimizer more freedom when parallelizing loops.

Use of -stackvar is recommended with any of the parallelization options.

Local variables are variables that are not dummy arguments, COMMON variables, variables inherited from an outer scope, or module variables made accessible by a USE statement.

With -stackvar in effect, local variables are allocated on the stack unless they have the attributes SAVE or STATIC. Note that explicitly initialized variables are implicitly declared with the SAVE attribute. A structure variable that is not explicitly initialized but some of whose components are initialized is, by default, not implicitly declared SAVE. Also, variables equivalenced with variables that have the SAVE or STATIC attribute are implicitly SAVE or STATIC.

A statically allocated variable is implicitly initialized to zero unless the program explicitly specifies an initial value for it. Variables allocated on the stack are not implicitly initialized except that components of structure variables can be initialized by default.

Putting large arrays onto the stack with -stackvar can overflow the stack causing segmentation faults. Increasing the stack size may be required.

The initial thread executing the program has a main stack, while each helper thread of a multithreaded program has its own thread stack.

The default size of the main stack is about 8 Megabytes. The default thread stack size is 4 Megabytes on 32–bit systems, 8 Megabytes on 64–bit systems. The limit command (with no parameters) shows the current main stack size. If you get a segmentation fault using -stackvar, try increasing the main and thread stack sizes.

Example: Show the current main stack size:


demo% limit
cputime         unlimited
filesize        unlimited
datasize        523256 kbytes
stacksize       8192 kbytes      <–––
coredumpsize    unlimited
descriptors     64
memorysize      unlimited
demo%

Example: Set the main stack size to 64 Megabytes:


demo% limit stacksize 65536

Example: Set each thread stack size to 8 Megabytes:


demo% setenv STACKSIZE 8192

You can set the stack size to be used by each slave thread by giving the STACKSIZE environment variable a value (in Kilobytes):

% setenv STACKSIZE 8192

sets the stack size for each slave thread to 8 Mb.

The STACKSIZE environment variable also accepts numerical values with a suffix of either B, K, M, or G for bytes, kilobytes, megabytes, or gigabytes respectively. The default is kilobytes.

For further information of the use of -stackvar with parallelization, see the “Parallelization” chapter in the Fortran Programming Guide. See csh(1) for details on the limit command.

Compile with -xcheck=stkovf to enable runtime checking for stack overflow situations. See 3.4.120 –xcheck=keyword.