-4

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;
}

When I run it, I get:

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

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?

Steve
  • 722
  • 4
  • 10
  • 24

1 Answers1

2

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.

How can I make it finish after returning 0?

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.

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.

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.

einpoklum
  • 118,144
  • 57
  • 340
  • 684