Say that you are several levels deep into a recursive function. The function was originally called in main. Is there a way for you to break out of the recursion and go straight back to main without having to go through all the other functions above?
You could use exceptions for that - either throw some suitable exception or craft your own and use it. Although using exceptions for flow control is generally not recommended this is the only reliable way here.
In C you can use longjmp/setjmp for this, but I don't think it's safe to use this in C++ (bypasses destructors ?). You'll probably have to use exceptions.
The question is how you got there? What kind of algorithm buries you deep into a recursion without a way of getting out of it?
Any recursive function must have a way to end the recursion, it recurses only if a condition is either true or false. When that doesn't hold, the recursion ends and the function returns instead of recursing deeper.
Why don't you just end the recursion this way, returning through all the levels?
If you're desperate, an exception is the way to go, but that's (rightly, IMO) frowned upon.
You might be able to get the location off the stack and use assembler to jmp to it, but why would you want to?
Also you have to consider that when you have moved on to pastures new someone is going to have to maintain it.
Make your function so it's tail call optimizable. Then there's no "functions above" to worry about.
No, you can't break from your recursion and return directly back to your main(). If your recursive function does not do other work after the recursive call you would effectively accomplish the same thing. I recommend restructuring your recursive function. A description of why you want to break from the recursion early would also be helpful.
I got the same problem in nqueens backtracking algo.
The simplest way would be to add a global boolean variable and use it to prevent any further action in your parent functions.
Related
I'm looking to implement something similar to the deferred IEnumerable concept in C++, but without template implementations. (My question is very similar to this other question, except for the deal-breaking templates.)
In our code, we have many functions that receive a spec to query and will store the results in a passed std::vector&. Often these functions call lower level versions which do their own filtering and combining with other sets. Sometimes they will create local vectors (with a different temp allocator) to call the lower-level functions, then push_back to the caller's vector after filtering that. These are not generic functions, in either word or spirit.
I'm looking to eliminate the copies and allocs, and even the need for a results container. Plus while I'm doing this, I'd like to give it a short-circuit ability.
There's an obvious need for deferred operations here. In C#, this would be easy. Everything yields into an IEnumerable, and use the Where() etc. operators, all deferred.
I have an idea how to do this in C++, passing in callbacks rather than containers. We just need the "push_back" abstracted, plus a returned bool for "stop here". The callbacks would be chained very similarly to IEnumerable operators, though in operation it would be more of a push-pull, and maybe feel a bit like IObservable.
And on top of all this, I have to avoid templatizing these function implementations. I don't want to refactor a lot of code, and I don't want to run into unsupported compiler surprises on the 15 or so different platforms we're currently compiling to. So no promises, no (I think). And no C++11.
This seems like a solved problem, but I've poked around on Google and maybe haven't found the right way to ask it. Has this problem been solved already?
I re-read your question and I actually think this can be servicable, but it's definitely not ideal without C++11.
You can consider boost::function<boost::optional<T>()> (yay Boost) as a range. The semantics of this range are super simple- you invoke it, and when it's finished, it gives back an empty optional instead of a value.
For output operations like push_back, you can consider a boost::function<void(T)> that will accept the value and then do whatever you need with it. However generally, these aren't that useful, as most functions can be written as returning a modified range, then the caller can simply use that range directly rather than requiring to take an output range.
Not having lambdas here is a total bitch, but you can look into e.g. Boost.Phoenix/Lambda to help.
EDIT: added backquotes to callback template. The interface was reading the asterisks as markdown indicators, not just as asterisks!
In a Windows DLL/Linux SO I am writing, I give the user app a way to register a callback function. Works great. The callback prototype looks like (void)(*callback)(void*);
I was having a fit of paranoia while writing the docs and realized, I have no really good way to know if the registered address is valid. The only feedback is either a crash or call the callback inside a try/catch.
I have no idea what exception would be thrown if the callback did not exist and who-knows-what executed. NOt even really sure that the call to "nowhere" could recover itself enough to generate the exception instead of a crash.
Yes, I know it's the user's problem. Just trying to be thoughtful and maybe help the user understand his bug.
So, what exception would this throw? Windows and Linux answers please if they differ.
Or, is there a better way to approach this without having to use an exception catch to detect the missing function?
There's no way to recover. Similarly, you cannot recover if the callback contains a line like *(int*)(0x1234) = 5;. Just live with it.
As a C++ library developer, you're not in the business of making sure that nothing ever crashes. You merely provide code that does what it promises when used the way you document.
A bit off-topic, but callbacks in the form of void(*)(), i.e. taking 0 arguments, are less than useful. A useful C-style callback accepts a user specified argument, so that the user can find the state corresponding to the callback. E.g.:
typedef void callback_fn(void* user_arg);
callback_id register_callback(callback_fn* callback, void* user_arg);
void unregister_callback(callback_id);
Without user_arg the user of your callback will be forced to use a global variable to store state corresponding to the callback function.
The situation you describe is rather unlikely. I have never seen such handling anywhere. The program would just crash and ruin its user.
But your concern is valid, because the failure root cause (assigning wrong address) and its manifestation (call to invalid address) can be so far away from each other that it could be very hard to identify it.
All I could advise here is to "fail fast and loud". For instance, you could do test call of the callback whenever it is assigned. This will still lead to crash, but now in the stack trace user will see where it all started from.
Again, this is not something an ordinary library user would expect...
As I answered here:
How to test if an instance is corrupted?
(completely different type of question, but same applies)
If the pointer is "not recognisable NULL or similar", then there is no way, in code, to tell if it's valid or not.
You also can't use try/catch to capture the failure, as "failure to execute the code" does not result in a throw.
Since this a "programmer error", I don't believe it's a big issue. Programmers can do what they like with their own code anyway, so whatever mechanism you add, it's going to be possible to circumvent in some way or another.
As others have said, there's no way to check, but... Is it
really necessary? I'm all in favor of defensive programming,
but the only way you can possibly get a pointer to a function
(other than a null pointer) is by taking the address of
a function. Some compilers do allow explicitly converting
a pointer to an object to a pointer to function, despite the
fact that the standard requires a diagnostic in such cases. But
even then, the client code needs an explicit cast to screw up.
And unlike objects, functions life through out the lifetime of
the program, so you cannot have a problem with a dangling
pointer—a pointer which was once valid, but isn't any
more. There is, in fact, practically no way to get an invalid
pointer to a function except intentionally (the only way that
occurs to me is if the unload a DLL with the function), and if
someone intentionally wants to screw up, there's no way you'll
be able to prevent it.
Infinite recursion is most often not desired, and when it happens it usually causes a stack overflow or segfaults.
But for theory's sake, and plain curiosity, I've been thinking if it'd be possible to create actual infinite recursion, intentionally.
Working in C++ and C where the stack, usually, grows for each function call, and each function returns and pops the part of the stack it handled.
Here's the thought. Would it be possible to force a function to clear out it's own stack space and then call another function so that the new function effectively replaces the first function, without the first function needing to return and then fire again via a loop.
I'm not only thinking about plain loops as a possible use for this, if there would be any. Loops usually do a good job at what they do. But what if you were to use it for sending signals through a node network, that carry on indefinitely in their own process threads until they reach a certain condition. It might be a tool that could be used for some problems.
Remember, I'm not really asking if it's practical, only if it's possible to do. For science!
Would it be possible to force a function to clear out it's own stack
space and then call another function so that the new function
effectively replaces the first function
This is used for tail-call-optimization, so yes, it is possible and used in practice. In languages like C++ this a nice feature, because sometimes algorithms are simpler to express using recursion, but more efficiently implemented using a loop. The transformation can in some cases be done automatically by the compiler.
There is a technique called trampolining that is used to implement continuation-passing style programming without the use of tail-call optimization. If you consider any language without support for TCO, such as JavaScript, and research solutions for CPS in that language, then it is likely that the solution involves trampolining.
Essentially, with trampolining there is a function called a trampoline which iteratively calls thunk-returning functions.
I know that you said "without the first function needing to return and then fire again via a loop"—that's what trampolining is—but considering that this is C++, leaving scopes by, for example, returning is central to the core design of C++'s automatic resource management via destructors (see: RAII). You could alternatively use C's setjmp()/longjmp() functions to wipe out stack, but then you would need to be very careful in making sure that all resources are released properly.
This does remind me of an optimisation that can be done in assembler code. Say you have this:
call FuncA
call FuncB
you can replace it with:
push ReturnAddress
push FuncB
jmp FuncA
ReturnAddress:
This causes the ret at the end of FuncA to jump to FuncB directly rather than back to the caller and then onto FuncB. Not really possible in higher level languages.
There's also this:
call FuncA
ret
which can be changed to:
jmp FuncA
I'm chasing a bug where a member value of an object seems to magically change, without any methods being called which modify it. No doubt something obvious but proving hard to track down.
I know I can put conditional break-points in methods based on the variable value, but is it in any way possible to actually put a breakpoint on a variable itself? e.g a breakpoint which fires when x==4? I know I can put watches on, what about breakpoints?
Edit: this is a native-only project, no managed malarkey.
You can use a data breakpoint. There are a number of restrictions about how and when they can be used, namely that they work only in native code.
(To the best of my knowledge, you can only tell it to break when the variable changes, not when it changes to a specific value, but I'm not entirely sure; most of my code is mixed managed/native and thus can't use data breakpoints).
What you should do is just wrap the variable in a set/get - not just a template functions but actually in a separate class, where set/get MUST be used to access. Then put a breakpoint in there. Alternatively, for easier chop and change, you could wrap the value in a class, and use operator overloads (with appropriate breaks in) to alter. That's probably the cleanest and most portable solution.
What you may also find is that the variable being modified is not in ways you expect. Best example I've got is that I had unsigned int where I subtracted from zero when I meant to increment from zero, so when I was looking for places that I knew modified it, that didn't flag up. Couldn't work out wtf was going on.
However, as far as I know, VC++ supports no mechanism to break on arbitrary changes, if the data breakpoint won't work for you. for example, if it was changed due to stack/heap corruption. But if you're running in debug, I'd expect that VC++ would break on those.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
Specifically, I'm wondering which of these I should write:
{
shared_ptr<GuiContextMenu> subMenu = items[j].subMenu.lock();
if (subMenu)
subMenu->setVisible(false);
}
or:
{
if (items[j].subMenu.lock())
items[j].subMenu.lock()->setVisible(false);
}
I am not required to follow any style guidelines. After optimization, I don't think either choice makes a difference in performance. What is generally the preferred style and why?
EDIT: the type of items[j].subMenu is boost::weak_ptr. lock() creates a shared_ptr out of it. There is actually an ambiguous difference in the two versions above, regarding how long the temporary shared_ptr lasts, so I wrapped my two examples in { braces } to resolve the ambiguity there.
An alternative method:
if(shared_ptr<GuiContextMenu> subMenu = items[j].subMenu.lock()) {
subMenu->setVisible(false);
}
//subMenu is no longer in scope
I'm assuming subMenu is a weak_ptr, in which case your second method creates two temporaries, which might or might not be an issue. And your first method adds a variable to a wider scope than it needs to. Personally, I try to avoid assignments within if statements, but this is one of the few cases where I feel its more useful than the alternatives.
In this particular case, you really should use the version with the temporary variable. The reason is not performance, but correctness - basically, you are not guaranteed that the two x.lock() calls return the same value (eg. if another thread releases the last strong reference on the object just between the two calls). By holding the strong reference in the temporary variable, you ensure it won't go away.
Other than that:
the compilers usually can't optimise out function calls, unless they are provably side-effect free (this is hard to do, but attributes may help) or inlined. In this case, the call has side-effects.
using temporaries can lead to shorter, more readable and more maintainable programs (eg. in case of error, you fix it in one place)
I think you're correct about either choice being no different after optimisation.
Personally, I would declare a new variable if it makes the code more readable, such as when you're chaining calls, or putting function calls inside function calls. As long as it's maintainable and the code achieves the same effect at no speed difference, it all boils down to readable code.
Edit:
mmyers bought up a good comment. Yes, be careful about calling lock() twice, as opposed to just once. They will have different effects depending on your implementation.
The choice is essentially up to you, but the basic thing you should look out for is maintainability.
When the return value is anything other that a boolean, assigning it to an intermediate variable can often simplify debugging. For example, if you step over the following:
if( fn() > 0 ) ...
all you will know after the fact was that the function returned a value either less than zero, or zero or more. Even if the return value were incorrect, the code may still appear to work. Assigning it to a variable that can be inspected in your debugger will allow you to determine whether the return value was expected.
When the return is boolean, the actual value is entirely implicit by the code flow, so it is less critical; however under code maintenance you may find later you need that result, so you may decide to make it a habit in any case.
Even where the return value is boolean, another issue to consider is whether the function has required side-effects, and whether this may be affected by short-circuit evaluation. For example in the statement:
if( isValid && fn() ) ...
the function will never be called is isValid is false.
The circumstances under which the code could be broken under maintenance by the unwary programmer (and it is often the less experienced programmers that get the maintenance tasks) are many, and probably best avoided.
In this specific example, I think it depends on what lock() does. Is the function expensive? Could it return different things each time the function is called (could it return a pointer the first time and NULL the second time)? Is there another thread running that could interleave between the two calls to lock()?
For this example, you need to understand the behavior of lock() and the rest of your code to make an intelligent decision.
I prefer the first one most of the time because it makes the code more clear and easy to read, therefore less error prone. For example, you forgot a parenthesis on that second example :)
In this case, actually, I'd probably do what you did in the second example, however if I needed to use that submenu more than a few times I'd go with the first one to make the code easier to read. As for performance, I thing any sane compiler would be able to optimize that (which is probably why you saw no difference in performance).
Also, as mmyers pointed out, that also depends on what lock() does. In general, if it's a simple getter method or something like that, you'll be fine.
Whatever YOU prefer. For me, it depends on how much I'll use it; for two lines, I might just write it out both times, whereas I create a variable if I use it more. However, YOU are the one who will most likely have to maintain this code and continue looking at it, so use whatever works for you. Of course, if you're at a company with a coding guideline, follow it.
I think the preferred style is whatever style you think makes your code more readable and maintainable. If you're a team of more than one, the only other consideration is that it's generally a good idea for everyone to adopt the same style, again for readability and ease of maintenance.
In this case I think you should use the temporary. Even if you know the implementation to .lock() is inexpensive, that can change. If you don't need to call lock() twice, don't. The value here is that it decouples your code from the implementation of lock(). And that's a good thing generally.