-2

I invoke a function 147 times in recursive and when it invokes for 147. times, program exe stops(codeblocks).

Before invokin function again, it assigned 1 int global variable to local, 1 int 2 dimensional global array to local and 1 string global variable to local variable. So, 146 of those maybe became a very huge load for program?

The function is:

2 Answers2

2

It seems your stack is overflowing by recursive calls. Quoting from above wiki page

In software, a stack overflow occurs when the stack pointer exceeds the stack bound. The call stack may consist of a limited amount of address space, often determined at the start of the program. The size of the call stack depends on many factors, including the programming language, machine architecture, multi-threading, and amount of available memory. When a program attempts to use more space than is available on the call stack (that is, when it attempts to access memory beyond the call stack's bounds, which is essentially a buffer overflow), the stack is said to overflow, typically resulting in a program crash

Very deep recursion and large stack variables along with recursion are some easy to fall reasons of stack overflow.

You may want to write a smarter code to get away from recursions.

Below links may help you get there.

  1. Way to go from recursion to iteration
  2. Replace Recursion with Iteration
Community
  • 1
  • 1
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • I don't think this is the answer. 147 iterations isn't enough to overflow the stack. Refactoring recursion into an iterative style isn't necessary unless you're hitting much deeper recursion than 147. – slim Jun 10 '14 at 09:54
  • Refactoring along with some smarter code. For ex: deferring the formatted array for path creation, cutting on memory, using least amount dynamic memory usign `realoc` that is required. And for further details we have (+1) your answer. – Mohit Jain Jun 10 '14 at 10:04
2

Each time you invoke your function, you allocate:

int visitedS[2416] = 2416 * 32 bits = 9.4KB
char pathS[4500] = 4500 * 8 bits = 4.4KB

So that's almost 14KB that gets placed on the stack every time you recurse.

After 147 recursions, you've put 1.98MB on the stack. That's not so huge - a typical Linux stack limit is 8MB.

I would check - through using a debugger or even adding debug print statements - your assumption that this is truly happening after 147 recursions. Perhaps there is a bug causing more invocations than you believed.

Even so, it may well be worth thinking about ways to reduce the memory footprint of each invocation. You seem to be creating local arrays which are copies of a global. Why not just use the data in the global. If your function must make changes to that data, keep a small set of deltas locally.

slim
  • 40,215
  • 13
  • 94
  • 127
  • Local array because i use them stack. After invoking, i can return the previous state of them. I am making deep first so if there is no way, i shhould copy data in locals, previous way. – user3721298 Jun 10 '14 at 10:03
  • Hence "keep a small set of deltas locally" – slim Jun 10 '14 at 10:25