- Marcar como novo
- Marcador
- Subscrever
- Silenciar
- Subscrever fonte RSS
- Destacar
- Imprimir
- Denunciar conteúdo inapropriado
I wrote a neat recursive subroutine which I use to search through three dimensional grids. It works great for small stacks. As the size of my problem increases, however, I soon run into stack overflow problems. I know I can increase the size of the stack (the default of 1Mb seems pretty small(?)) but I'm wondering if this is just a pretty naive use of recursion. On each call to my routine I set up a number of static arrays. Once I reach the end of my recursion the routine "unwraps" itself and at each step back through the stack I require this data once more. I guess that storing all this data in the stack causes my overflow.
Is there any way of reducing the stack cost of recursion or dynamically increasing the stack size. I don't want to simply wack up the stack size, not knowing when I'll inadvertently overflow again.
Anyone got an bright ideas. If not I'll have to rewrite using brute force, which is a shame.
Many thanks,
BPH (UK)
Is there any way of reducing the stack cost of recursion or dynamically increasing the stack size. I don't want to simply wack up the stack size, not knowing when I'll inadvertently overflow again.
Anyone got an bright ideas. If not I'll have to rewrite using brute force, which is a shame.
Many thanks,
BPH (UK)
Link copiado
3 Respostas
- Marcar como novo
- Marcador
- Subscrever
- Silenciar
- Subscrever fonte RSS
- Destacar
- Imprimir
- Denunciar conteúdo inapropriado
There's no way to dynamically increase the stack size. I'm unsure what you mean by "static arrays" - do you mean local arrays? If so, then these would go on the stack in a recursive routine. Consider using ALLOCATABLE instead, and ALLOCATE them to the desired size. DEALLOCATE on routine exit - or let that happen automatically.
Steve
Steve
- Marcar como novo
- Marcador
- Subscrever
- Silenciar
- Subscrever fonte RSS
- Destacar
- Imprimir
- Denunciar conteúdo inapropriado
I would think that increasing the stack size in the execution command would be more appropriate than using ALLOCATE and DEALLOCATE because the latter is defeating the purpose of using recursion, which is to rely on local variables allocated on the stack by the compiler in order to keep the code neat and clean.
I wonder if CVF turns tail recursions into loops for performance reasons? If yes, his 3D grid searching problem may be suitable for this kind of optimization.
Regards,
Greg Chien
http://protodesign-inc.com
I wonder if CVF turns tail recursions into loops for performance reasons? If yes, his 3D grid searching problem may be suitable for this kind of optimization.
Regards,
Greg Chien
http://protodesign-inc.com
- Marcar como novo
- Marcador
- Subscrever
- Silenciar
- Subscrever fonte RSS
- Destacar
- Imprimir
- Denunciar conteúdo inapropriado
You can have local ALLOCATABLE variables - each instance of the recursive routine would have its own copy, and they get deallocated on routine exit.
Our compiler can do tail recursion optimizations.
Steve
Our compiler can do tail recursion optimizations.
Steve
Responder
Opções do tópico
- Subscrever fonte RSS
- Marcar tópico como novo
- Marcar tópico como lido
- Flutuar este Tópico para o utilizador atual
- Marcador
- Subscrever
- Página amigável para impressora