-1

I'm working on a project in C#, for which one of the questions is to make a program that receives a Stack and returns a new Stack with the digits that appear in that stack (every digit that appears will be in the new stack only once).

I made this code, which works pretty fine, but in the middle it just stops working and it gives this error "Unhandled Exception: OutOfMemoryException".

I was wondering what was the problem? From what I can understand it occurs because my program requires more memory than my computer can provide. Correct me if I'm wrong.

Does anyone know what I can do to fix this or how I can improve my code to prevent this?

What can I do in general in codes to prevent this problem from occurring in the future?

Thank you very much in advance. :)

public static Stack<int> Stk(Stack<int> S)
    {
        int Num;
        Stack<int> New = new Stack<int>();
        while (!S.IsEmpty())
        {
            Num = S.Pop();
            while (Num != 0)
            {
                if (!Check(New, (Num % 10)))
                {
                    New.Push(Num % 10);
                }
                Console.WriteLine("Original Stack: " + New);
                Num = Num / 10;
            }
        }
        return New;
    }

    public static bool Check(Stack<int> S, int num)
    {
        Stack<int> Temp = new Stack<int>();
        while (!S.IsEmpty())
        {
            Console.WriteLine("Stack Temp: " + Temp);
            if (num == S.Top())
            {
                while (!Temp.IsEmpty())
                {
                    S.Push(Temp.Top());
                }
                Console.WriteLine("Number found in Stack S!");
                Console.WriteLine("Stack S: " + S);
                return true;
            }
            Temp.Push(S.Pop());
        }
        while (!Temp.IsEmpty())
        {
            S.Push(Temp.Pop());
        }
        Console.WriteLine("Stack S: " + S);
        Console.WriteLine("Number NOT found in Stack S!");
        return false;
    }
RKei
  • 11
  • 4
  • 1
    *I was wondering what was the problem?* [Debugger Basics](https://msdn.microsoft.com/nl-nl/library/k0k771bt.aspx) – rene Jan 19 '18 at 21:09
  • @Debugger Basics - rene - The program works fine (it does what it needs to do) but always in the middle it just stops working (freezes) and gives me the "Unhandled Exception: OutOfMemoryException" error. – RKei Jan 19 '18 at 22:14
  • 2
    To be honest, a program that consistently crashes is not really the definition of something that "works fine." – Michael Roy Jan 19 '18 at 22:27
  • @MichaelRoy - My bad. I meant to say it was at least running, just not running correctly, not doing what it should do. Excuse me for my bad English, it's not my native language. – RKei Jan 20 '18 at 15:31

1 Answers1

0
if (num == S.Top())
{
    while (!Temp.IsEmpty()) // Infinite loop, Temp is never empty
    {
        S.Push(Temp.Top()); // Because of infinite loop, S is just going to fill up memory
    }

    //...

I'm not familiar with Top() on a Stack<T> but judging from the rest of the code it looks like it doesn't remove an object from your stack (the function I know of that does this is Peek). If that's the case you've got an infinite loop that fills up all available memory and ultimately runs out.

Edit: This answer focuses on the problem you're having. This smells like homework to me so I'm not going to rewrite it but I'd suggest paying at @ckuri's comment on this answer down below. It looks like you're doing a lot more work than you should. You may want to look at iterating the stack rather than popping to a whole new stack.

McAden
  • 13,714
  • 5
  • 37
  • 63
  • That seems like a simple spelling mistake; `Pop`. – InBetween Jan 19 '18 at 21:09
  • I'd considered that, but he seems to be using it like it's `Peek`. Following the logic that seems to be what he's trying to do, and contains the same issue. – McAden Jan 19 '18 at 21:11
  • Yes, I learned of the command Top() which checks top value without popping it out of the stack. What I was trying to do is go over the whole stack but without losing it (whatever's in it) and so I pushed what I popped into another stack, and in the end returned it into the same stack. From what I've learned, if I pop the values in a stack which I transferred into that function inside of that function, the stack from the main function also gets affected. Correct me if I'm wrong? (Sorry if it's a bit hard to understand, I can't figure how to explain it so well :P ) – RKei Jan 19 '18 at 22:10
  • In which case, this is the answer. You've got an infinite loop. Also, I've never heard of `Top` where is it documented? The function I know of that does that is `Peek` – McAden Jan 19 '18 at 22:26
  • @RKei If you want to go over the stack without touching it, then just enumerate it by using `foreach`. If you want to make a copy of a stack you just write `var copy = new Stack(sourceStack);` – ckuri Jan 19 '18 at 23:10
  • @McAden - Yes!!! It worked! I didn't really understand why it was infinite and after you wrote the notes in your code then I understood. I just needed to change the 'Temp.Top()' to 'Temp.Pop()'. Btw, I learned that 'Top()' checks the top value of the stack without popping it. I believe it's like the 'Peek' you're talking about, but I guess my Stack class it just programmed differently. So that was the fix for the problem. Thank you. – RKei Jan 20 '18 at 13:45
  • @ckuri - I haven't learned those commands yet ('foreach' and 'sourceStack') but it's good to know now how to make a copy of a stack. Thanks for the help though. – RKei Jan 20 '18 at 13:45