Jump to content

Recommended Posts

Posted

In this exercise, you will attempt to estimate the number of stack frames that the kernel will permit a process to populate the system's main memory. To illustrate this, think of an infinite recursive program. One might wonder how many times will this program be allowed to spawn, i.e, how many stack frames can this program push onto the stack before either 1. the system runs out of memory or 2. the kernel kills this rouge process (the latter is more likely to happen in a *nix based system). To this end, write a simple C program that enables you to give a rough estimate on the number of stack frames pushed on the stack. Clearly, you are expected to write an infinite recursive function and an associated driver to get things started, either in separate modules or in a single C module. Your task now would be to figure out how many times this recursive function was called before the program was terminated.

Posted

Do you know what recursion means?

Because it's easy task..

Call the same function over and over again..

 

f.e.

 

void crash( int depth )

{

printf( "Current depth %d\n", depth );

crash( depth + 1 );

}

Posted

i have no idea what recursion means?

can you explain it?


Do you know what recursion means?

Because it's easy task..

Call the same function over and over again..

 

f.e.

 

void crash( int depth )

{

printf( "Current depth %d\n", depth );

crash( depth + 1 );

}

 

i have no idea what recursion means?

can you explain it?

and does that code apply to the question?
Posted

Recursion is similar repetitions, in this case repeated function calls.

 

To really understand recursion though, you need to understand recursion. Just joking, a good real life example is the effect you see standing between two mirrors. Can even draw parallels between this and what is being asked of you.

Posted

Recursion is a kind of loop, implemented by a function calling itself.

 

int factorial( int i) { if i==0) return(1) else return(i*factorial(i-1)}; /* only valid for i>=0 */

 

It's been a few years since I wrote a program, so this recursive definition of factorial might be buggy.

Posted

and does that code apply to the question?

 

Compile and run it. That should be whole answer to question..

When stack will be full, it should crash program.

Posted

When written and used properly, a recursive function will not normally run out of memory; at least simple ones. You might write a recursive sort and try to process so much data it runs out of stack or memory. C functions do not spawn threads or processes unless specifically made to do it.

Posted (edited)
When written and used properly, a recursive function will not normally run out of memory; at least simple ones. You might write a recursive sort and try to process so much data it runs out of stack or memory.

 

CPU stack created by CreateThread() has default size of 1 MB of memory.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms682453%28v=vs.85%29.aspx

when 2nd parameter dwStackSize is 0 function is using default thread size.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686774%28v=vs.85%29.aspx

 

CPU stack created by CreateProcess() or other function might have different size, but there is limit. After exceeding limit, there will be overflow.

 

Whenever function calls other functions return address is stored on stack, and all registers that might be used are stored as well, utilizing stack. So new function can use these CPU registers for its own purpose.

 

I have modified code to:

 

#include <stdio.h>

 

void crash( unsigned long long depth )

{

if( !( depth % 1000 ) ) printf( "Current depth %d\n", (int) depth );

 

crash( depth + 1 );

if( !( depth % 1000 ) ) printf( "Current depth %d\n", (int) depth ); // this line is never executed, but compiler doesn't know about it..

}

 

int main( void )

{

crash( 0 );

return( 0 );

}

 

Make project in Visual C++ 2008 or so, compile in Release mode, and you will see.

 

On Windows XP it's showing output:

 

Current depth 0

Current depth 1000

Current depth 2000

Current depth 3000

Current depth 4000

Current depth 5000

Current depth 6000

Current depth 7000

Current depth 8000

Current depth 9000

Current depth 10000

Current depth 11000

Current depth 12000

Current depth 13000

Current depth 14000

Current depth 15000

Current depth 16000

Current depth 17000

Current depth 18000

Current depth 19000

Current depth 20000

Current depth 21000

Current depth 22000

Current depth 23000

Current depth 24000

Current depth 25000

(exactly 25715)

 

And then closes automatically. Which is typical stack overflow behavior of Windows OS.

 

C functions do not spawn threads or processes unless specifically made to do it.

 

Yes, of course.

Nobody here were talking about creating new threads.

Recursion is not doing so.

 

In Windows the main application thread is created by CreateProcess(), and its stack size is as big as calling function wanted, or default.

Edited by Sensei
Posted

Personal realizations about recursion:

It is more elegant to use recursion when appropriate from code-poetry side

There are indeed things that can be done with recursion that can not be solved with loops (without significant complication)

In compiled languages recursive loops are about as fast as loops, though in optimization stages when those are unrolled sometimes which does actually make loop code often faster, but i digress.

In interpreted languages recursive calls are significantly slower than loops.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.