2

I'm tring to define jmp_buf as pointer and using it in nested longjmp(s).as follow:

 ...
jmp_buf *bfj;
...

and then writing if else:

if( setjmp(*bfj) == 0){
DS[SP-2].int_val=(int)bfj;;
//to store the bfj
}else {}

and somewhere else using the stored bfj to longjmp

 bfj = (jmp_buf *)DS[TOP].int_val;
 longjmp(*bfj,1);

where DS[TOP].int_val is where I stored it. as it may seems clear,I want to do nested gotos and returns using stored bfj. but well when I try to debug I get "unhandeled exception". I get this at the very starting point:

if( setjmp(*bfj) == 0)

I would be pleased if someone would tell the solution.

angela
  • 99
  • 1
  • 1
  • 8
  • 6
    You [have been told](http://stackoverflow.com/questions/3087268/question-with-longjmp) that what you're trying to do isn't a good idea. If you don't heed advice, what are you asking for? – sbi Jun 21 '10 at 23:00
  • I know it's not good idea to use longjmps but when you're tring to write a code that is near to assembly language for the output of the semantic phase of a compiler, it's the only solution get to my mind.so for the case that I'm writing, it's the way and yes it's not the good way.I must produce code that is near to assembly,(Three address code). I never use goto or longjmp for regular programs but in this case,I have to produce such code. it's the natural of compilers. – angela Jun 21 '10 at 23:09
  • 4
    No, it is not the "natural of compilers." A compiler is no different from any other program: All it does is translate some kind of input to some kind of output. The fact that you think you need to use `longjmp` (and that you clearly don't understand what it does or how it works) suggests you're doing something *very* wrong. – greyfade Jun 21 '10 at 23:20
  • I've understood it.I said the third phase of compiler--->compiler course at university.not those regular and simple compilers you think. the compiler for Fortran has estimated 18 man year effort! It's not that simple as you think.(the regular definition of compilers) – angela Jun 21 '10 at 23:38
  • 2
    I think it's a fair estimate to say that the _difference_ between two most recent Microsoft C++ compilers is 18 man-years of effort! But greyfade is right: even a complex compiler is just transforming data structures. – MSalters Jun 22 '10 at 07:34
  • 1
    I guess you don't see "compiler course at university" and you don't seem to know what is three address code. – angela Jun 23 '10 at 16:45

1 Answers1

2

From your code, you are not actually allocating memory for your jmp_buf. There are a couple of things you can do:

  1. Dynamically allocate your jmp_buf with new and you will want to delete it when you are done with it
  2. Put the jmp_buf on the stack jmp_buf bfj; and when you want it's pointer, you would take it's address with &bfj.

So, #1 would look like:

jmp_buf *bfj = new jmp_buf;
...

if( setjmp(*bfj) == 0){
DS[SP-2].int_val=(intptr_t)bfj;

while #2 would look like:

jmp_buf bfj;
...

if( setjmp(bfj) == 0){
DS[SP-2].int_val=(intptr_t)&bfj;

Another potential issue is that you should never cast a pointer to an int as a pointer may take more memory then an int (this happens on the common 64 bit programming models). If you can't store the pointer directly, you should use intptr_t instead.

R Samuel Klatchko
  • 74,869
  • 16
  • 134
  • 187