Fortran: StackOverflow堆栈溢出

StackOverflow


测试代码. 静态数组可以在堆栈上分配,而不是使用堆。堆栈的大小有限,在某些情况下,静态数组可能会耗尽进程所允许的堆栈空间。

program arraymem

   use iso_fortran_env

   implicit none

   integer, parameter :: n = 2*1024*1024 - 4096

   print *, 'Numeric Storage (bits): ', numeric_storage_size
   print *, 'Creating array N ', n

   call meanArray(n)

contains

   subroutine meanArray(n)
      integer, intent(in) :: n
      integer :: dumb
      integer, dimension(n) :: a
      a = 1
      !print *, sum(a)
      read *, dumb
   end subroutine meanArray

end program arraymem

检查系统堆栈内存限制

$> ulimit -S  -s
8192
$> ulimit -H  -s
unlimited

堆栈的软限制为8MB,用户可以将其值提高到无限值。


命令pmap将打印内存映射,内含堆栈标记。

$> pmap -x `ps ax | grep a.ou[t] | awk '{print $1}' | head -1`

输出


Now we will compile the code above with gfortran, forcing the static arrays to be on stack. Some compilers move large arrays to heap automatically so we are bypassing this protection.

$> gfortran -fstack-arrays example_12.f90
$> ./a.out
Sometimes you will get an output like this:

 Numeric Storage (bits):           32
 Creating array N      2093056
Segmentation fault (core dumped)
We are so close to filling the stack that small variations in loading libraries could cross the limit. Try a few times until the code stops when asking for input from the keyboard.

Once the code is waiting for any input, execute this command on a separate terminal:

$> pmap -x `ps ax | grep a.ou[t] | awk '{print $1}' | head -1`
The command pmap will print a map of the memory and the stack is marked there.

Address           Kbytes     RSS   Dirty Mode  Mapping
0000000000400000       4       4       0 r---- a.out
0000000000401000       4       4       0 r-x-- a.out
0000000000402000       4       4       0 r---- a.out
0000000000403000       4       4       4 r---- a.out
0000000000404000       4       4       4 rw--- a.out
0000000001a3e000     132      16      16 rw---   [ anon ]
00007f8fb9948000    1808     284       0 r-x-- libc-2.17.so
00007f8fb9b0c000    2044       0       0 ----- libc-2.17.so
00007f8fb9d0b000      16      16      16 r---- libc-2.17.so
00007f8fb9d0f000       8       8       8 rw--- libc-2.17.so
00007f8fb9d11000      20      12      12 rw---   [ anon ]
00007f8fb9d16000     276      16       0 r-x-- libquadmath.so.0.0.0
00007f8fb9d5b000    2048       0       0 ----- libquadmath.so.0.0.0
00007f8fb9f5b000       4       4       4 r---- libquadmath.so.0.0.0
00007f8fb9f5c000       4       4       4 rw--- libquadmath.so.0.0.0
00007f8fb9f5d000      92      24       0 r-x-- libgcc_s.so.1
00007f8fb9f74000    2044       0       0 ----- libgcc_s.so.1
00007f8fba173000       4       4       4 r---- libgcc_s.so.1
00007f8fba174000       4       4       4 rw--- libgcc_s.so.1
00007f8fba175000    1028      64       0 r-x-- libm-2.17.so
00007f8fba276000    2044       0       0 ----- libm-2.17.so
00007f8fba475000       4       4       4 r---- libm-2.17.so
00007f8fba476000       4       4       4 rw--- libm-2.17.so
00007f8fba477000    2704     220       0 r-x-- libgfortran.so.5.0.0
00007f8fba71b000    2048       0       0 ----- libgfortran.so.5.0.0
00007f8fba91b000       4       4       4 r---- libgfortran.so.5.0.0
00007f8fba91c000       8       8       8 rw--- libgfortran.so.5.0.0
00007f8fba91e000     136     108       0 r-x-- ld-2.17.so
00007f8fbab29000      16      16      16 rw---   [ anon ]
00007f8fbab3d000       8       8       8 rw---   [ anon ]
00007f8fbab3f000       4       4       4 r---- ld-2.17.so
00007f8fbab40000       4       4       4 rw--- ld-2.17.so
00007f8fbab41000       4       4       4 rw---   [ anon ]
00007ffd65b94000    8192    8192    8192 rw---   [ stack ]
00007ffd663d3000       8       4       0 r-x--   [ anon ]
ffffffffff600000       4       0       0 r-x--   [ anon ]
---------------- ------- ------- -------
total kB           24744    9056    8324

参考

Stack overflow error

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值