Solved C++ jump

June 10, 2011 at 01:21:49
Specs: centos
I need a simple solution to this:

void a(){
x++;
a();
}

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.


See More: C++ jump

Report •


✔ Best Answer
June 10, 2011 at 07:38:21
The situation that you describe is exactly what exceptions were designed to deal with.

I didn't say that the concept of a jump was not necessary in computing; but it is not necessary, or even possible, in most high-level languages. There is no situation in C++ where you need to use a "jump" as you describe it. A rich selection of control structures are provided (which are, of course, coded using assembler "jmp" instructions), including the various instructions relating to exceptions, which obviate the need for direct manipulation of the program instruction pointer.



#1
June 10, 2011 at 03:19:16
The function is recursively calling itself. After enough steps it will fill the stack with return addresses and then fail.

It is possible for a function to recursively call itself, but there must be some way for the nest of calls to end. For example, if you made the function call

if < (x < 100) a();

then when x got to 100 the function would return and so all the nested calls would "unwind".

What exactly are you trying to achieve with this function?


Report •

#2
June 10, 2011 at 04:16:26
Thanks for the answer. Please remember that this is the exemplification of the issue, not the source code itself.

As I said before, in the actual application I need to JUMP to another function, not to call it (code machine like jump).


Report •

#3
June 10, 2011 at 05:05:28
What happens when this function you want to jump to comes to the end. Where does it go from there?

The programme counter is expecting an implicit Return statement at the end of a function. That Return address is put onto the Stack when the function is called. If you were to jump to a function the return statement would still be there but there is no telling what return address would be at the top of the stack. So all functions are called and return to the point from where they were called. That's the way it works in almost all programming languages.

Consequently there is no actual Jump to a function. The nearest you will get to it a GoTo and that goes to a predefined label, not a function.

If you feel that you need to jump to a function you need look at your programme design and decide what you are trying to achieve.

Stuart


Report •

Related Solutions

#4
June 10, 2011 at 05:24:58
I very much doubt that you really want to jump to another function. Why on earth would you want to do that? (I'm not saying it can't be done, I just don't think that's what you really want to do.)

If your original code was just an example, perhaps you could post some code that is more related to your problem. And perhaps explain why you think you need to jump to another function.


Report •

#5
June 10, 2011 at 05:30:55
ijack,
thanks for the answer, but if you are not saying it is impossible, would you be so kind and just tell me how to do it?


StuartS,
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.
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 ?

Please let me worry about results, safety, efficiency, opportunity, etc.


Report •

#6
June 10, 2011 at 06:33:54
Use inline assembler code to reset the frame pointer and save the return value of the first function from the stack. Call the function that you wish to "jump" to. In that function replace the return value on the stack with the one that you saved from the first function. The end result should be the same as a jump.

This is not for the faint-hearted. You need to thoroughly understand assembler and the function- calling convention and use of the stack frame of your compiler. You'll have to go through several attempts before you get it right, and consider all possible ways that the function may be called. Also you will have to consider whether this is a NEAR call or a FAR call. I'll leave you to worry about results, safety, efficiency, opportunity, etc.

On the other hand, you could just design your program properly so that it doesn't need to "jump". Why are you so reluctant to explain what your requirements are?

You do not need to code a "jump" into your program.


Report •

#7
June 10, 2011 at 07:23:03
ijack, it is a pretty sophisticated concept and hard for me to explain, but i'll give it a try:
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.
As about the need of a jump, I would politely but firmly disagree with you. The jump is essential in ASM there would be no computing without it, it is "natural" and sometimes spears you the effort of redesign the problem.


Report •

#8
June 10, 2011 at 07:38:21
✔ Best Answer
The situation that you describe is exactly what exceptions were designed to deal with.

I didn't say that the concept of a jump was not necessary in computing; but it is not necessary, or even possible, in most high-level languages. There is no situation in C++ where you need to use a "jump" as you describe it. A rich selection of control structures are provided (which are, of course, coded using assembler "jmp" instructions), including the various instructions relating to exceptions, which obviate the need for direct manipulation of the program instruction pointer.


Report •

#9
June 10, 2011 at 07:56:08
Thanks for the answer and to everyone involved.

Report •


Ask Question