C++ 有趣的一件事:另類遞迴 (An interesting event:abnormal recursion)

想試寫兩個程式,可以互相呼叫及傳遞訊息什麼的。

Try to design two programs that they can transmit messages to each other, so they can do some communicating things or so.

.

於是先寫了只有空白視窗的prg1.exe及prg2.exe,兩執行檔放在同一個資料夾。接著想要用prg1執行prg2,

First, I built prg1.exe and prg2.exe each with only an empty window and put them in the same folder. And then, I modified prg1 so let it be able to call and execute prg2.

.

卻將這行(其中誤將 prg2寫成 prg1)

WinExec((ExtractFilePath(Application->ExeName)+"\\prg1″).c_str(),SW_SHOW);

並且不小心寫在prg1的FormCreate:

However, I mindlessly added this code line above into prg1’s event ‘FormCreate’, and included an fatal mistake in the code: ‘prg1’ — it should be ‘prg2’.

As below:

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  WinExec((ExtractFilePath(Application->ExeName)+"\\prg1").c_str(),SW_SHOW);
}

.

未思索立馬按F9編譯執行。只見prg1的視窗出現,然後電腦不知在忙什麼,很遲緩。打開工作管理員一看,哇! 整排grg1而且一直增加中,按刪除也動不了。原來是prg1一直執行prg1。

GG了,強制關機,重開機改程式….。

Then, without thinking, I clicked the F9 to run compile+execute prg1. As prg1’s windows appeared, my computer became very slow in any action. I realized that something wrong happened. Hardly and finally, I opened Task Manager. My god! Amount of ‘prg1’ had been listed and still keep generating speedy all the time. I had been aware one thing had been happening: prg1 was executing itself.

I tried to stop this stubborn prg1 in the task manager, but it wasn’t work. Finally, pressed the OFF button that in front the machine panel, then turn it on. Modified the prg1..

Tagged: ,

發表留言