Sign in to follow this  
m0ng00se

break out of C++ function?

Recommended Posts

What is the correct way to break out of deeply nested VC++ code and return control to the console? To be specific if a string search match is true then I want to break out of all the code, print to the screen and return control to the user. My code works fine but at the moment it falls back into the function that called it, which falls back into the function that called that etc... before it eventually hits the print to screen code. If I tell it to print as soon as a match is found then it does but then goes on to run all the other code after it's printed. I'm just a little unsure how to break out of everything. There's "return" and "break" and "continue" but my understanding is that they just break from the current loop and return to the function that called them. Obviously I can quickly hack my way out but I wondered what the correct and safe way to break out completely is so that memory gets cleaned up properly? m0ng00se

Share this post


Link to post
Share on other sites
Such a problem sounds like you have structuring problems in your code. Besides exceptions there is an exit() function, but I'm not sure if it plays nicely with C++. If you are using just C free/malloc then you can use the atexit() function.

http://www.cppreference.com/stdother/exit.html

And it certainly would be an abuse of exceptions, as they are for "exceptional" circumstances, not normal program flow.

Share this post


Link to post
Share on other sites
Quote:
Original post by incin
exceptions?


Exceptions would be a very poor way of handling an event that's supposed to happen. Exceptions are meant to handle exceptional events, such as errors. They're expensive, and your code has to take extra special care to ensure that things go smoothly when the stack unwinds. I see too much code lately that uses exceptions as some sort of message-passing facility, and that's not at all what they were designed for.

Share this post


Link to post
Share on other sites
Obviously the best solution would be to return from all of the functions cleanly.
But, while being an abuse of exceptions, it's the only way to make sure that 'the memory get cleaned up properly'.

Share this post


Link to post
Share on other sites
Quote:
Obviously I can quickly hack my way out but I wondered what the correct and safe way to break out completely is so that memory gets cleaned up properly?


Yes, there is. The following:

Quote:
at the moment it falls back into the function that called it, which falls back into the function that called that etc... before it eventually hits the print to screen code.


There are two other methods, but they're both horrible. Neither of them properly reclaims memory.

Re-organize the code so that the above problem isn't necessary.

For any more concrete example, post the code.

Share this post


Link to post
Share on other sites
Thanks guys. Yes I realise it is a design problem in the first place and I admit I was looking for a quick fix so I wouldn't have to try and redesign several thousand lines of code. The design sort of changed several months into the actual coding. It used a deep search of an AI database but I found a way to use a more specific search that could exit as soon as a match is found and only fall back into the deep search if a match isn't found.

I think you're probably right and I should fall back through all the calling functions cleaning up memory and resetting variables as I go. Just means I run a lot of code I don't use on that loop or I write a new parent loop that controls everything with a nice exit function built in.

m0ng00se

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this