Looping without running out of memory

In summary: If something goes wrong, it is hard to find out where the error lies, what piece of code ensures it keeps looping.It is more difficult to debug a recursive function because it can be harder to find the source of the problem.The MS C++ debugger's structure is different for different compilers.
  • #1
Pattielli
296
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
  • #2
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
 
  • #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.
 
  • #4
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.
 
  • #5
Thank dduardo a lot for your explanation

I see it now, i will buy a C book and learn it...:sm:
 
  • #6
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:
  • #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,
 
  • #8
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.
 
  • #9
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.
 

FAQ: Looping without running out of memory

1. How can I prevent my program from running out of memory when using loops?

The most effective way to prevent your program from running out of memory when using loops is to use efficient data structures and algorithms. This means choosing the most appropriate data structure for your task and optimizing your code to minimize unnecessary memory usage.

2. What is the role of garbage collection in preventing memory issues when looping?

Garbage collection is a process that automatically frees up memory that is no longer being used by your program. This helps prevent memory issues when looping by removing any unused memory, making it available for your program to use again.

3. How can I design my loops to minimize memory usage?

One way to minimize memory usage when looping is to break up your code into smaller, more manageable chunks. This allows for smaller portions of memory to be used at a time, rather than one large block of memory being allocated and potentially causing issues.

4. What are some common mistakes that can lead to memory issues when looping?

Some common mistakes that can lead to memory issues when looping include using inefficient data structures, not properly deallocating memory after use, and infinite loops. It is important to carefully review your code and make sure you are using memory efficiently.

5. Are there any tools or techniques that can help me identify and fix memory issues when looping?

Yes, there are various tools and techniques that can help you identify and fix memory issues when looping. These include memory profilers, which can help you track how much memory is being used by your program, and debugging tools, which can help you identify and fix any coding errors that may be causing memory issues.

Similar threads

Replies
18
Views
1K
Replies
9
Views
2K
Replies
2
Views
952
Replies
6
Views
1K
Replies
5
Views
897
Replies
5
Views
1K
Replies
13
Views
1K
Replies
9
Views
4K
Back
Top