Unexpect outcome executing program in Dev-C

This is a discussion on Unexpect outcome executing program in Dev-C within the C Programming forums, part of the General Programming Boards category; Upon execution, the cmd window pops up and then flashes off. I don't understand why this happen even after I ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    Exclamation Unexpect outcome executing program in Dev-C

    Upon execution, the cmd window pops up and then flashes off. I don't understand why this happen even after I put in for loops with no limiting condition

    Code:
    #include <stdio.h>
    int count= 0;
    int recurse(int c);
    int main(void)
    {
      recurse(count);
      printf("%d",count);
      for(;;);
      return 0;
    }
    int recurse(int c)
    {
       recurse(c);
       printf("%d\n",count+=1); 
       return count;
       }

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Were you looking for this?

    ~/

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    he has an endless loop in his code... and a recurse.
    there is no reason for this program to close by itself!

    btw, you should remove the endless recurse, it's not a good idea to kill your stack like this.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #4
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by xephyr
    Upon execution, the cmd window pops up and then flashes off. I don't understand why this happen even after I put in for loops with no limiting condition

    Code:
    #include <stdio.h>
    int count= 0;
    int recurse(int c);
    int main(void)
    {
      recurse(count);
      printf("%d",count);
      for(;;);
      return 0;
    }
    int recurse(int c)
    {
       recurse(c);
       printf("%d\n",count+=1); 
       return count;
       }
    Read again your lesson about recursion. Your recurse function is broken. There is no end condition. It's a loop-until-auto-memory-is-exhausted loop.

    I suspect the function to be called until some memory or system error occurs and suddendly closes the windows.

    You never go to the printf() or the forever loop.
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #5
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Devil Panther
    he has an endless loop in his code... and a recurse.
    there is no reason for this program to close by itself!

    btw, you should remove the endless recurse, it's not a good idea to kill your stack like this.
    But it's a good way to suddenly end the program by lack of auto memory error !
    Emmanuel Delahaye

    "C is a sharp tool"

  6. #6
    Registered User
    Join Date
    Jul 2004
    Posts
    12

    Unhappy

    Read again your lesson about recursion. Your recurse function is broken.
    I suspect the function to be called until some memory or system error occurs and suddendly closes the windows.
    Well, the purpose of this code is to test the maximum number of times a function can recur on my system. Is there any other ways to do so?

  7. #7
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by xephyr
    Well, the purpose of this code is to test the maximum number of times a function can recur on my system. Is there any other ways to do so?
    There is no way to do that properly because the system will crash before you get the results. BTW, this information is useless. If you are concerned by that, the fix is simple. Don't use recursion. It's a plague.
    Emmanuel Delahaye

    "C is a sharp tool"

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There's no "maximum number of times a function can recur". There's a limit to stack space, and the app exits when it's exhausted. How many function calls this stack space can swallow depends on how much stack space each call needs.

    An example. A typical Windows gives the typical program a typical 1 MB of stack space. An atypical function call without arguments or local variables needs 4 bytes on a typical 32-bit x86 (the return address).
    So, if you make your program a little atypical in that it doesn't use any other stack space other than primitive function calls, you can have 262144 recursions. Note that the last few recursions are not capable of calling any output functions anymore though, because that would require more stack space than is left.

    The whole thing is utterly pointless though.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Quote Originally Posted by xephyr
    Well, the purpose of this code is to test the maximum number of times a function can recur on my system. Is there any other ways to do so?
    If you want to find out how many times your function recursed, go to the Start menu and open up a Command Prompt (pseudo DOS window), then run the program from there. You'll have to CD to the directory containing your .exe, then type the name of your exe to run it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Trying to loop my program or reset it Dev c++
    By GameGenie in forum C++ Programming
    Replies: 8
    Last Post: 07-21-2005, 01:39 PM
  2. Executing a program
    By trovoada in forum C Programming
    Replies: 15
    Last Post: 07-04-2005, 07:01 AM
  3. insufficient memory for tsr
    By manmohan in forum C Programming
    Replies: 8
    Last Post: 01-02-2004, 09:48 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21