3 Replies - 356 Views - Last Post: 05 November 2011 - 12:16 AM Rate Topic: -----

#1 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

I Almost got this overflow problem down, would this program overflow?

Posted 04 November 2011 - 10:57 PM

So frequently I find I write programs that need to loop forever using like while (true) etc. So the below is a silly program but I was thinking if there was a way you could loop through questions forever. In any case it appears that this program would ask these same questions forever without overflowing as the stack method is broken and popped off the stack but is called again later through the other code after main.? I'm still confused a little bit though wouldn't not breaking the code below main cause an overflow eventually too? Thanks for help in advance I almost understand this problem with overflowing I think but it still baffles me sometimes. If you don't break that loop in the code below Main wouldn't those loops go forever? I've tested the program though and it doesn't seem to use more memory....

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace ConsoleApplication1
{
    class Program
    {
        public static void FirstQuestionFunc()
        {

            //main function for asking the question
            while (true)
            {
                //ask for text and if it contains happy break
                Console.WriteLine("Enter Text");
                string a = Console.ReadLine();
                //half second pause
                Thread.Sleep(500);
                if (a.Contains("happy"))
                {
                    Console.WriteLine("You Got It");
                    //half second pause
                    Thread.Sleep(500);
                    //then break the infinite while loop
                    break;

                }
            }



        }
        
   
        static void Main(string[] args)
        {
            //main first stack function based above
            FirstQuestionFunc();


            while (true)
            {
                //ask the user to enter text and if it contains sad
                Console.WriteLine("Write Text Again");
                string b = Console.ReadLine();
                //half second sleep
                Thread.Sleep(500);
                if (b.Contains("sad"))
                {
                    Console.WriteLine("You Got It Man");
                    //pause half second
                    Thread.Sleep(500);
                    //loop forever with the other method forever

                    FirstQuestionFunc();

                }
            }





Is This A Good Question/Topic? 0
  • +

Replies To: I Almost got this overflow problem down, would this program overflow?

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: I Almost got this overflow problem down, would this program overflow?

Posted 04 November 2011 - 11:48 PM

Stack overflows really only happen when you have code that calls itself from itself (infinite recursion) or by putting large stack variables on the call stack.

You should not have a call stack overflow here because you call FirstQuestionFunc() from main, it throws that call on the stack, executes, pops off the stack and then goes into the loop on main which does the same thing forever and ever. Since each time you call FirstQuestionFunc() it puts it on the stack, transfers control and when it is finished it pops it back off. No where are you leaving calls on the stack so you are not going to be overflowing it.

Now lets assume you had a function like...

private void func() {
     func();
}



Here you are going to be throwing calls on the stack in an infinite recursion scenario. That is because before a function call can complete it calls itself, the calls are not removed from the stack and thus will get an overflow.

Hope that clarifies things. :)
Was This Post Helpful? 2
  • +
  • -

#3 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 763
  • Joined: 31-August 11

Re: I Almost got this overflow problem down, would this program overflow?

Posted 04 November 2011 - 11:52 PM

Actually this to me was amazingly well explained thank you so much. I was right about the stack etc. with this code. That made great sense dude phew! I feel like I am finally understanding this at least. Thanks!!

This post has been edited by tlhIn`toq: 05 November 2011 - 06:31 AM
Reason for edit:: No need to quote the entire immediately preceding post

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: I Almost got this overflow problem down, would this program overflow?

Posted 05 November 2011 - 12:16 AM

For posterity's sake, here's the thread with the original question:

http://www.dreaminco...1&#entry1456195
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1