This obviously is a call. I need a jump, since the call inevitably ends after 700k steps with segmentation fault. goto in the actual application does not work since it's about 2 points in code situated in different areas.
Any help will be highly appreciated.
Also I would much appreciate answers on topic, and not"why you need this?".
:) You guessed wrong. I'm trying to do exactly what the code does. An infinite loop. Why it gives after a number of loops segmentation fault, I can guess: it's probably related to the fact that every time it CALLS a() saves the return address, and probably that vector fills up at some point. That is the exact reason that I need a JUMP (code machine like jump).
Please remember that this is the exemplification of the issue, not the source code itself so stop asking yourselves (and me) what is the purpose of function a()
Indeed the problem is stack overflow as the return address is pushed onto the stack with each call. If you want an infinite loop, perhaps you should use looping rather than recursion:
1 2 3 4
while (true)
{
// etc
} // <----- an infinite loop
The 2 points that the JUMP needs to connect are 15k code lines apart. that would cross several class declarations, object instantiations and a few dozen include directives.
I'm not an expert, but I would guess that a while loop is not the answer.
However, to avoid ending up with a function calling another function and so on and so forth until it calls itself and never returns(thus generating segmentation fault) can be solved by a very complex "return value" design, but that means redesigning the problem to comply with the program and not designing the software to solve the problem.
That is why my question stands:
Is there a way to JUMP TO instead of CALLing a function ?
The 2 points that the JUMP needs to connect are 15k code lines apart. that would cross several class declarations, object instantiations and a few dozen include directives.
If that's true, then how can these two points be in the same function? You're not telling us something here.
If you think such as a jump is needed for some reason, that's a strong indicator that the section of code you need to jump to should be in its own function instead of being part of another. If the nature of the problem doesn't make that viable, you should elaborate a bit on your specific case.
A jump-statement must reside in the same function and can appear before only one statement in the same function. The set of identifier names following a goto has its own name space so the names do not interfere with other identifiers. Labels cannot be redeclared.
If that's true, then how can these two points be in the same function?
they are not! After a long runtime the function in question calling functions, object constructors, etc, ends up calling itself. that's why I simplified all to this code:
1 2 3 4
void a(){
x++;
a();
}
so, Is there a way to JUMP TO instead of CALLing a function ?
resulting something like:
Obvioulsy this only makes sense if the stack frame for where you want to jump to exists.
In C, you can use setjmp and longjmp. In C++ you could in theory do this, but I STRONGLY recommend against it, because it does not unwind the stack. You could more than likely implement want you want safely, by throwing an exception at the jump site, and catching it at the destination.
kev82
Obvioulsy this only makes sense if the stack frame for where you want to jump to exists.
In C, you can use setjmp and longjmp. In C++ you could in theory do this, but I STRONGLY recommend against it, because it does not unwind the stack. You could more than likely implement want you want safely, by throwing an exception at the jump site, and catching it at the destination.
Except the "recommend" part, your answer is very interesting. And thanks a lot. I'll dig in the
throwing an exception at the jump site, and catching it at the destination
direction.
Also, is there a way to completely wipe that stack of return addresses?
So if a function doesn't ever call itself. Do you have to mutually recursive function? I.e. ones which (recursively) call each other?
With regard to your particular question, if you just jump to the function, rather than call it, then how would you know to which memory location you should return when your function returns?
Also, is there a way to completely wipe that stack of return addresses?
The return address is part of the stack frame. Every function creates a stack frame, the only way to destroy the stack frame is to return, or throw an exception.
Just in an attempt to stop you heading into a world of problems with longjmp. Here is the exception implementation. This is not a good design though. I think it highly likely that your code should be rewritten differently to not require this.
Thank you for the answer.
Xander314 , I do not intend to go back to that point in code again. I'll try to describe the actual problem:
imagine a huge tree of objects. On some point, one function ( let's call it: errorcheck() ), on one of the objects detects a "flaw". Well, in that point I need to give a command like "compute()" to one of the objects hierarchically above me. I know exactly the object, the params, and the command, but unfortunately is one of my "parents". Calling that function will at some point end calling errorcheck() again.
So for the "error" and it's parameters to be transmitted via return statement, it's impossible. That is why I need a way to jump to a function instead of calling it. And probably to wipe the stack frame from the "parent" down somehow.