从main返回0后程序没有停止 [英] Program doesn't stop after returning 0 from main

查看:60
本文介绍了从main返回0后程序没有停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个程序,用霍夫曼编码对文件进行编码。
它可以正常工作,但是由于某种原因,从主函数返回0后,它并没有停止。

主要功能如下:

I wrote a program that encodes files with Huffman coding.
It works fine but for some reason after returning 0 from main function it doesn't stop.
Main function looks like:

int main(int argc, char *argv[])
{
    if (argc < 5)
    {
        std::cout << "..." << std::endl;
    }
    else
    {
        if (!std::strcmp(argv[1], "haff") && !std::strcmp(argv[2], "-c"))
            HuffmanC(argv[3], argv[4]);
        if (!std::strcmp(argv[1], "haff") && !std::strcmp(argv[2], "-d"))
            HuffmanD(argv[3], argv[4]);

        std::cout << "Operations: " << count << std::endl;
    }

    return 0;
}

运行时,我得到:


MacBook-Pro-Alex:代码alex $ ./main haff -c test.txt test.haff
操作:37371553

MacBook-Pro-Alex:code alex$ ./main haff -c test.txt test.haff
Operations: 37371553

它以空行结尾,终端说程序继续运行,但是最后一个 cout 语句执行得很好,并且在我得到它时应该返回0并完成。返回0后如何完成?还是其余的代码有问题?

It ends with an empty line and terminal says that the program keeps running, but the last cout statement executes well and as I get it it should return 0 and finish. How can I make it finish after returning 0? Or is the problem in the rest of the code?

推荐答案


还是其余的问题代码?

Or is the problem in the rest of the code?

可能。也许您已经以某种方式损坏了堆栈,以便将其从主程序返回到您未曾想到的地方。没有完整的可验证示例,我们真的无法知道。

Possibly. Perhaps you've corrupted your stack somehow, so that you're "returning" from main to someplace you didn't expect. We can't really know without an complete, verifiable example.


如何返回0后完成?

How can I make it finish after returning 0?

您可以在MacOS上使用 kill 命令强制终止它。使用GUI任务管理器可能行不通。

You can use the kill command on MacOS to terminate it forcefully. Using the GUI task manager may or may not work.

但是,也许更有效的措施是将调试器附加到进程中,并查看其实际作用。

But perhaps a more effective course of action would be to attach a debugger to the process and see what it's actually doing.

您可以阅读此解释有关如何在MacOS上使用XCode进行此操作的解释-但是我不使用MacOS,所以我不知道。另外,@ SergeyA慷慨地建议尝试使用pstack 来获取进程当前堆栈。当然,如果堆栈出现乱码,则不会告诉您实际上会得到什么。

You could read this explanation on how to do this on MacOS with XCode - but I don't use MacOS, so I wouldn't know. Also, @SergeyA graciously suggests trying using pstack to get the process' current stack. Of course, if the stack has been garbled, there's no telling what you'll actually get.

请确保应用程序已编译为包含调试信息。

Make sure the application is compiled with debugging information included.

最后-首先运行带有调试器的程序可能更好,并在最后一个 cout<< 行。

Finally - it's probably better to run the program with a debugger attached in the first place, and set a breakpoint at the last cout << line.

这篇关于从main返回0后程序没有停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆