Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Looping without running out of memory

  1. May 19, 2004 #1
    I have just read a book about looping, and first of all, I would like to say sorry because my question is supid and will be really easy to you, I guess...
    But I am really new to computer, i honestly don't even know how to use WinWord correctly, it is an obvious truth.

    In my loop i run from 0 to 1000 and then compute the values of the function I put in the loop, I 'd like to know if I 'd like to loop from 0 till infinity, will it run out of my memory and my computer will stop moving ?
    Is there any way for me to do that without making such things happen-looping forever ?

    Thank you
    Last edited: May 19, 2004
  2. jcsd
  3. May 19, 2004 #2


    User Avatar
    Staff Emeritus

    What exactly are you trying to compute? In practical terms, you don't have time to wait for a computer to infinitely loop. You can do it, but I don't think you have that much time. Another question: What programming language are you using to do this?

    If you want an infinite loop in C, you do:


    Another way is





    label:top {statements} goto top;

    or ....

    There are many ways to do this
  4. May 19, 2004 #3
    Thanks a lot,
    I mean some ways to loop and then put what I compute in a certain place, repeat the loop again and then when my program runs around if it meets that place, it will use things stored in there, not to create new ones. I would like this loop to go till it meets the goal.
    Another important point is that when it calculate the new values in the new places, it will update the old values in the old places. So finally, I can ask my program to take only the places where their values are highest of all, and it will draw me a way from start to end, that is a good way to go, and the only way my program just finds for me ...
    Do you know any ways to think correctly about this problem ? Any ways suggested are all highly appreciated...

    Thank you very much.
  5. May 20, 2004 #4


    User Avatar
    Staff Emeritus

    I'm having a hard time understanding what your trying to do. Are you just starting to learn about the concept of programming? If so, I suggest you get a book on the language of your choice and read through it. In programming, you have things called variables where you store information. Depending on the variable type you can store different things. For instance, in C you can define an int variable. A variable with type int can store integers. Similiarly a char type variable can store characters. In languages like C, you need to be careful with loops and variable initialization. There is somthing called scope, which basically means that variables initialized within a loop are confined to the loop and deleted after it exits.
  6. May 20, 2004 #5
    Thank dduardo a lot for your explanation

    I see it now, i will buy a C book and learn it...:sm:
  7. May 20, 2004 #6
    Never ever use recursive functions as mistakes are easily made and hard to debug. It is bad programming practice. If you really must use it, one way is build in a safety, for example:

    Code (Text):

    RecursiveFunction(Counter, OtherArguments)
     Counter = Counter + 1;


     if (Counter < PrettyHighNumber)
    And I recommend buying a C++ instead of C book, as C++ is more powerful and if you want to move from C to C++ you would have to "unlearn" some stuff. Better get it right from the first time.
    Last edited: May 20, 2004
  8. May 27, 2004 #7
    Oh, well, Simon, I am sorry for not having answered your post, I didn't see it ...True.:sm:

    What I asked was actually not about recursive, mmm..perhaps that was what I had in mind and about how readers would interpret my questions was all beyond my notice...
    I was told that recursive is still good in some cases, I find it neater than long code snippets, also that when I became more knowledgeable about how recursives and functional programming styles work, I could run into more compilers, code optimization, analysis,... or even write better code in any program...

    Simon, Would you please tell me why you said it is hard to debug a recursive function ?
    What is the MS C++ debugger's structure ? I am using MSC++...

    Thank Simon a lot,
  9. May 27, 2004 #8
    What I meant was: if something goes wrong, it is hard to find out where the error lies, what piece of code ensures it keeps looping.
  10. May 27, 2004 #9
    I don't think you could loop *to* infinity :)
  11. May 27, 2004 #10
    You could loop to infinite frustration when your program crashes.
  12. May 27, 2004 #11
    How is it any more difficult than figuring out why any other kind of loop doesn't stop?

    There are a lot of times when using recursion can significantly reduce the amount of work you have to do. Writing the same algorithm to work in a loop usually requires a lot more code, which is generally harder to debug.
  13. May 27, 2004 #12
    Do the computers have infinite memory ?
  14. May 27, 2004 #13
    No, but unless your dynamically allocating memory locations as your program runs you wont run out. If you keep updating a particular variable then that variable location is the only thing altered (disregard program counters and stacks and the like for this explanation). When you declare a vairiable the compiler sets aside a specific amount of memory based on the compiler and variable type. As you increment said variable eventually you will come to the limit of the memory allocated for that one variable. Some languages will throw an except when you try to exceed the memory allocated (COBOL for instance) while others will simply roll the number over (C/C++ for example). If you are dynamically allocating memory locations as you go then your OS will (should) guard against excessive memory use in some cases and in other cases occupied ram will be shifted to the hard drive. and in other cases you may actually consume enough free resources from your system. It depends on the program you write.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Similar Threads for Looping without running Date
Files appearing in google drive without my input Feb 8, 2018
Calculators TI-89 PAY program, infinite loop problem? May 18, 2011
Loops in constructor Jun 21, 2008
Infinite Loops - Symbolic Computer & The Human Mind Feb 9, 2008
While loop in C Jun 18, 2006