C++ program stop (freeze, no error, no exit) after a number of print (cout)
<p>I have this code to test the Ackermann function, I call it program <strong>P1</strong>. In the ackerman(int m, int n).
My purpose is, if  OUT haven't been defined, the program will run and just print the result after it completed. If OUT defined, the program will print "xxx" every "loop", and wait for a char input from stdin. I have an external process to read the output of <strong>P1</strong>, catch the "xxx" string (to do something) and write back a char to <strong>P1</strong>, so than it can continue.
if I compile without define OUT, and run P1 standalone, the code can run completely, and print out ack(3,6) = SomeValue, but if I enable OUT, and run the external process with P1, P1 can receive the char for a while, and "pause" suddenly, no crash, no error, no exit. I used c variable to count the times before P1 stopped, this is not a fixed number (3813, 3951, 3804,....). The external process is writed in C#, and was tested very well. I used it for many case but in this ackermann function, its stopped. Why it doesn't stop in the first loop, but in a pretty large number of loops? Is there any relation between the printing with the stopping?</p>

<p><strong>P1</strong>:</p>

<pre><code>int MAX = 99999;
class stack {
private:
    int num[99999];
    int size;
public:
    stack() {
        size = 0;
    }
    void push(long x) {
        if(size &lt; MAX)
            num[++size] = x;
        else {
            cout&lt;&lt;"Full Stack"&lt;&lt;endl;
            return;
        }
    }
    int pop() {
        if(size==0) {
            cout&lt;&lt;"Out of stack memory"&lt;&lt;endl;
            return -1;
        } 
        return num[size--];
    }
    bool isEmpty() {
        if(size == 0)
            return true;
        else
            return false;
    }
};

int ackman(int m,int n)
{
    stack v;
    v.push(m);
    int c = 0;
    while(!v.isEmpty()) {
        m = v.pop();
        if(m==0 ||n==0)
            n+= m+1;
        else {
            v.push(--m);
            v.push(++m);
            n--;
        }
        c++;

#ifdef OUT
        printf("c is %d\n",c );
        cout &lt;&lt; endl;
        //flush(stdin);
        //flush(stdout);
        cout &lt;&lt; "xxx" &lt;&lt; endl;
        getchar(); 
        //sleep(200);
#endif
    }
    return n;
}

void ack() 
{
    int m = 3, n = 6;
#ifdef DEMO
    m = 3;
    n = 4;
#endif
    try{
        int k = ackman(m,n);
        printf("Ack %d , %d = %d\n",m,n,k );
    }catch(...){
        cout &lt;&lt; "Co loi khi ack";
    }
}

//just example
int main(){
 ack();
return 0;
}
</code></pre>

<p><br /><strong>External process</strong><br /></p>

<pre><code>static List&lt;BenchMemory&gt; benchMemory(String exeName, String caseName)
            {
                int count = 0;
                Console.Title = "Bench memory: " + exeName + " case: "+caseName;
                Console.WriteLine("Exe: {0}, case: {1}", exeName, caseName);
                using (Process process = new Process())
                {
                    /* Create the start info object */
                    ProcessStartInfo startInfo = new ProcessStartInfo();
                    startInfo.FileName = exeName + ".exe";
                    startInfo.Arguments = caseName;
                    startInfo.UseShellExecute = false;
                    startInfo.RedirectStandardOutput = true;
                    startInfo.RedirectStandardInput = true;
                    List&lt;BenchMemory&gt; listMemResult = new List&lt;BenchMemory&gt;();
                    #region handler
                    DataReceivedEventHandler outDataHandler = (object sender, DataReceivedEventArgs e) =&gt;
                    {
                        if (e.Data != null)
                        {
                            count++;
                            Console.WriteLine(e.Data);
                            if (!process.HasExited &amp;&amp; e.Data != null &amp;&amp; e.Data.ToLower().Equals("xxx"))
                            {
                                var bMem = getProcessMemory(process, exeName, caseName);
                                listMemResult.Add(bMem);
                                process.StandardInput.WriteLine("k");
                            }


                        }
                    };
                    #endregion
                    process.StartInfo = startInfo;
                    process.OutputDataReceived += outDataHandler;
                    process.Start();
                    process.BeginOutputReadLine();
                    process.WaitForExit();
                    var topPeak = listMemResult.Take(10).ToList();
                    process.Refresh();
                    return topPeak;
                }
            }
</code></pre>