Looping without running out of memory

  • Thread starter Thread starter Pattielli
  • Start date Start date
  • Tags Tags
    Memory Running
Click For Summary

Discussion Overview

The discussion revolves around the concept of looping in programming, particularly the implications of creating infinite loops and managing memory usage. Participants explore various programming techniques, the nature of recursion, and the challenges associated with debugging loops in different programming languages.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant questions whether looping from 0 to infinity would cause memory issues or crash the computer, expressing a desire to understand how to loop indefinitely without negative consequences.
  • Another participant suggests that while infinite loops can be created in programming, practical limitations such as time and system resources make them unfeasible.
  • A participant discusses the need to store computed values in specific locations during looping, aiming to update these values rather than create new ones, and seeks advice on how to approach this problem.
  • Concerns are raised about the use of recursive functions, with one participant warning that they can be difficult to debug and suggesting that iterative solutions may be preferable.
  • There is a discussion about the differences between C and C++, with recommendations for learning resources and the implications of choosing one language over the other.
  • Some participants express skepticism about the possibility of truly looping to infinity, questioning the limits of computer memory and the behavior of variables in programming.
  • Another participant clarifies that if memory is not dynamically allocated during execution, the program is less likely to run out of memory, but acknowledges that exceeding allocated memory for variables can lead to errors or unexpected behavior.

Areas of Agreement / Disagreement

Participants express differing views on the feasibility and implications of infinite loops, the use of recursion, and the management of memory in programming. There is no consensus on the best approach to looping or the effectiveness of recursion versus iteration.

Contextual Notes

Limitations include varying interpretations of infinite loops, the complexity of debugging recursive functions, and the potential for memory issues based on programming practices and language specifics. The discussion does not resolve these complexities.

Pattielli
Messages
296
Reaction score
0
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:
Computer science news on Phys.org
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:

while(1){statements}

Another way is

for(;1;){statements}

or

do{statements}while(1);

or

label:top {statements} goto top;

or ...

There are many ways to do this
 
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.
 
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.
 
Thank dduardo a lot for your explanation

I see it now, i will buy a C book and learn it...:sm:
 
Pattielli said:
Is there any way for me to do that without making such things happen-looping forever ?
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:
RecursiveFunction(Counter, OtherArguments)
{
 Counter = Counter + 1;

 OtherFunction(OtherArguments);

 if (Counter < PrettyHighNumber)
 {
  RecursiveFunction(Counter,OtherArguments);
 }
}

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:
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,
 
Pattielli said:
Simon, Would you please tell me why you said it is hard to debug a recursive function ?
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.
 
I don't think you could loop *to* infinity :)
 
  • #10
You could loop to infinite frustration when your program crashes.
 
  • #11
Simon666 said:
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.

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.
 
  • #12
aychamo said:
I don't think you could loop *to* infinity :)
Do the computers have infinite memory ?
 
  • #13
No, but unless your dynamically allocating memory locations as your program runs you won't 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.
 

Similar threads

  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 40 ·
2
Replies
40
Views
4K
Replies
2
Views
2K