欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > Fortran: StackOverflow堆栈溢出

Fortran: StackOverflow堆栈溢出

2024/10/24 15:18:56 来源:https://blog.csdn.net/weixin_42849849/article/details/141276684  浏览:    关键词:Fortran: StackOverflow堆栈溢出

StackOverflow


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

program arraymemuse iso_fortran_envimplicit noneinteger, parameter :: n = 2*1024*1024 - 4096print *, 'Numeric Storage (bits): ', numeric_storage_sizeprint *, 'Creating array N ', ncall meanArray(n)containssubroutine meanArray(n)integer, intent(in) :: ninteger :: dumbinteger, dimension(n) :: aa = 1!print *, sum(a)read *, dumbend subroutine meanArrayend 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):           32Creating 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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com