Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Getting addresses...setjmp/longjmp

Getting addresses...setjmp/longjmp

Scheduled Pinned Locked Moved C / C++ / MFC
questionjsonhelp
17 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • _ Offline
    _ Offline
    _AnsHUMAN_
    wrote on last edited by
    #1

    Hi All, I have a scenario where I need to implement something like what setjmp does. I am well aware of using the setjmp API but the problem statement requires this to be implemented in a different manner. I have a variable that would store the address of the next function to be called. This variable could be passed as a parameter to another function and to another one from that function and so on... For this I will need the address of the function that is currently executing and the address to which I want to jump to. Any ideas on how can I read the addresses or implement this?

    You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

    _ A 2 Replies Last reply
    0
    • _ _AnsHUMAN_

      Hi All, I have a scenario where I need to implement something like what setjmp does. I am well aware of using the setjmp API but the problem statement requires this to be implemented in a different manner. I have a variable that would store the address of the next function to be called. This variable could be passed as a parameter to another function and to another one from that function and so on... For this I will need the address of the function that is currently executing and the address to which I want to jump to. Any ideas on how can I read the addresses or implement this?

      You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      I do not understand why you want the address of a function at runtime. You could simply store the addresses of all available functions in a pointer array and use that array in code.

      «_Superman_»
      I love work. It gives me something to do between weekends.

      Microsoft MVP (Visual C++)

      Polymorphism in C

      _ 1 Reply Last reply
      0
      • _ _AnsHUMAN_

        Hi All, I have a scenario where I need to implement something like what setjmp does. I am well aware of using the setjmp API but the problem statement requires this to be implemented in a different manner. I have a variable that would store the address of the next function to be called. This variable could be passed as a parameter to another function and to another one from that function and so on... For this I will need the address of the function that is currently executing and the address to which I want to jump to. Any ideas on how can I read the addresses or implement this?

        You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

        A Offline
        A Offline
        Aescleal
        wrote on last edited by
        #3

        Why do you need the address of the function that's currently executing? When a function returns you end up back at where you were executing before. So if I have a function pointer (declared as void (*function_pointer() ) and call it:

        function_ptr();

        when whatever function the pointer points to returns you'll be back executing the next statement in the calling function. Incidentally a lot of finite state machines use the technique of storing their states as a function pointer, at least when implemented in C. It might pay to do some research on them - they may hold the key to what you want. Cheers, Ash PS: If you're using C++ you don't want a function pointer, look at using an interface pointer instead.

        _ 1 Reply Last reply
        0
        • A Aescleal

          Why do you need the address of the function that's currently executing? When a function returns you end up back at where you were executing before. So if I have a function pointer (declared as void (*function_pointer() ) and call it:

          function_ptr();

          when whatever function the pointer points to returns you'll be back executing the next statement in the calling function. Incidentally a lot of finite state machines use the technique of storing their states as a function pointer, at least when implemented in C. It might pay to do some research on them - they may hold the key to what you want. Cheers, Ash PS: If you're using C++ you don't want a function pointer, look at using an interface pointer instead.

          _ Offline
          _ Offline
          _AnsHUMAN_
          wrote on last edited by
          #4

          I will explain the situation. File A.cpp: jmp_buf buf; void func1() { cout<<"abc"; longjmp(1,buf); } void func2() { cout<<"def"; } int main() { if (!setjmp(buf)) func1(); else cout<<"Do nothing"; } In this case I am able to do the navigation from func1 and back to main. Just like we do for a LABEL and GOTO case but this happens across function and file calls as well. In my case I have another file say DEF.cpp wherein resides a function called CallMe I am able to make the call to that function as well from main. but if I want that CallMe should call another function then I should also return back to the main function, for which I would need the address in the call stack to which I want to return to. Let me know in case I have misunderstood something

          You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

          A N 2 Replies Last reply
          0
          • _ _Superman_

            I do not understand why you want the address of a function at runtime. You could simply store the addresses of all available functions in a pointer array and use that array in code.

            «_Superman_»
            I love work. It gives me something to do between weekends.

            Microsoft MVP (Visual C++)

            Polymorphism in C

            _ Offline
            _ Offline
            _AnsHUMAN_
            wrote on last edited by
            #5

            How do I store the addresses of functions? These functions may also be defined in different files across the applications code.

            You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

            _ 1 Reply Last reply
            0
            • _ _AnsHUMAN_

              How do I store the addresses of functions? These functions may also be defined in different files across the applications code.

              You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

              _ Offline
              _ Offline
              _Superman_
              wrote on last edited by
              #6

              If you have a function -

              void fun() {}

              you can take its address as -

              void* funptr = fun;

              «_Superman_»
              I love work. It gives me something to do between weekends.

              Microsoft MVP (Visual C++)

              Polymorphism in C

              _ 1 Reply Last reply
              0
              • _ _AnsHUMAN_

                I will explain the situation. File A.cpp: jmp_buf buf; void func1() { cout<<"abc"; longjmp(1,buf); } void func2() { cout<<"def"; } int main() { if (!setjmp(buf)) func1(); else cout<<"Do nothing"; } In this case I am able to do the navigation from func1 and back to main. Just like we do for a LABEL and GOTO case but this happens across function and file calls as well. In my case I have another file say DEF.cpp wherein resides a function called CallMe I am able to make the call to that function as well from main. but if I want that CallMe should call another function then I should also return back to the main function, for which I would need the address in the call stack to which I want to return to. Let me know in case I have misunderstood something

                You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

                A Offline
                A Offline
                Aescleal
                wrote on last edited by
                #7

                Ah, I see what you want to do and now I see it I don't think it's a good idea in C++. setjmp and longjmp have so many problems in C++ that if you think you understand the complexity of using them you're usually wrong. You'd have all the same problems (what destructors get called, what happens if an exception is thrown which are undefined by the standard) with any hand rolled system you come up with. Cheers, Ash

                _ 2 Replies Last reply
                0
                • A Aescleal

                  Ah, I see what you want to do and now I see it I don't think it's a good idea in C++. setjmp and longjmp have so many problems in C++ that if you think you understand the complexity of using them you're usually wrong. You'd have all the same problems (what destructors get called, what happens if an exception is thrown which are undefined by the standard) with any hand rolled system you come up with. Cheers, Ash

                  _ Offline
                  _ Offline
                  _AnsHUMAN_
                  wrote on last edited by
                  #8

                  Thanks for your inputs. I have already done some investigation regarding the use of setjmp and longjmp and the other overheads underlying their usage, but still just wanted to get a second opinion before I throw it away... Thanks again.

                  You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

                  1 Reply Last reply
                  0
                  • _ _Superman_

                    If you have a function -

                    void fun() {}

                    you can take its address as -

                    void* funptr = fun;

                    «_Superman_»
                    I love work. It gives me something to do between weekends.

                    Microsoft MVP (Visual C++)

                    Polymorphism in C

                    _ Offline
                    _ Offline
                    _AnsHUMAN_
                    wrote on last edited by
                    #9

                    Thanks for that :). But I have a different issue over here. With all the functions loaded into the memory and millions of LOC I doubt this to be a feasible solution in my case.

                    You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

                    1 Reply Last reply
                    0
                    • _ _AnsHUMAN_

                      I will explain the situation. File A.cpp: jmp_buf buf; void func1() { cout<<"abc"; longjmp(1,buf); } void func2() { cout<<"def"; } int main() { if (!setjmp(buf)) func1(); else cout<<"Do nothing"; } In this case I am able to do the navigation from func1 and back to main. Just like we do for a LABEL and GOTO case but this happens across function and file calls as well. In my case I have another file say DEF.cpp wherein resides a function called CallMe I am able to make the call to that function as well from main. but if I want that CallMe should call another function then I should also return back to the main function, for which I would need the address in the call stack to which I want to return to. Let me know in case I have misunderstood something

                      You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

                      N Offline
                      N Offline
                      norish
                      wrote on last edited by
                      #10

                      Why don't you use c++ exception semantics? It might be as below.

                      struct TException;

                      void func1()
                      {
                      cout<<"abc";
                      throw new TException("func1");
                      }
                      void func2()
                      {
                      cout<<"def";
                      }
                      void main()
                      {
                      try {
                      func1();
                      }
                      catch (TException* e) {
                      e->printSomthing();
                      delete e;
                      }
                      catch(...) {
                      cerr<<"general exception which I dont know"<

                      A 1 Reply Last reply
                      0
                      • N norish

                        Why don't you use c++ exception semantics? It might be as below.

                        struct TException;

                        void func1()
                        {
                        cout<<"abc";
                        throw new TException("func1");
                        }
                        void func2()
                        {
                        cout<<"def";
                        }
                        void main()
                        {
                        try {
                        func1();
                        }
                        catch (TException* e) {
                        e->printSomthing();
                        delete e;
                        }
                        catch(...) {
                        cerr<<"general exception which I dont know"<

                        A Offline
                        A Offline
                        Aescleal
                        wrote on last edited by
                        #11

                        That's not what the author wanted - he wanted to be able to implement something like co-routines or resumable exceptions with his code (which is why set/longjmp attracted him, he could branch from an arbitrary lump of code back to another arbitrary lump). Exceptions are just good for a one way trip up the call stack. Oh, and as an aside - if you use C++ exceptions follow the rule "throw by value, catch by reference." The last thing you want to be doing is resource management when you're trying to handle exceptions - it's complete madness. And if you think I'm talking complete rubbish consider what happens if printSomething throws - you'll leak a pointer to a TException object. And I've just noticed you're using that cardinal sin void main(). Guess what? That doesn't compile on any standard conforming C++ compiler. Use one of the two standard forms instead:

                        int main()

                        or:

                        int main( int argv, char *argv[] )

                        Ash

                        N 1 Reply Last reply
                        0
                        • A Aescleal

                          That's not what the author wanted - he wanted to be able to implement something like co-routines or resumable exceptions with his code (which is why set/longjmp attracted him, he could branch from an arbitrary lump of code back to another arbitrary lump). Exceptions are just good for a one way trip up the call stack. Oh, and as an aside - if you use C++ exceptions follow the rule "throw by value, catch by reference." The last thing you want to be doing is resource management when you're trying to handle exceptions - it's complete madness. And if you think I'm talking complete rubbish consider what happens if printSomething throws - you'll leak a pointer to a TException object. And I've just noticed you're using that cardinal sin void main(). Guess what? That doesn't compile on any standard conforming C++ compiler. Use one of the two standard forms instead:

                          int main()

                          or:

                          int main( int argv, char *argv[] )

                          Ash

                          N Offline
                          N Offline
                          norish
                          wrote on last edited by
                          #12

                          Thanks your pointing out. That was a Java style coding. In c++, ordinarily;

                          throw TException("func1");

                          catch(TException& e) {
                          }

                          And I know void main is not compatible in c++. That was only concept codes, but may not be so good example.

                          A 1 Reply Last reply
                          0
                          • N norish

                            Thanks your pointing out. That was a Java style coding. In c++, ordinarily;

                            throw TException("func1");

                            catch(TException& e) {
                            }

                            And I know void main is not compatible in c++. That was only concept codes, but may not be so good example.

                            A Offline
                            A Offline
                            Aescleal
                            wrote on last edited by
                            #13

                            The Java style of exceptions was actually around a couple of years before Java first came out. Microsoft had an exception handling mechanism in MFC 1 and 2 that threw by pointer and caught by pointer requiring the caller to delete the exceptions. It also used setjmp and longjmp to transfer control despite all the problems with that. It's still there in MFC 19 years later (but uses real C++ exception handling these days). The only reason I brought the point up was I didn't want another neophyte C++ programmer getting the idea that the correct way to throw and catch was some knackered old view of the world imposed by a second rate C++ compiler from 20 years ago. The poor souls have enough to cope with! Cheers, Ash

                            1 Reply Last reply
                            0
                            • A Aescleal

                              Ah, I see what you want to do and now I see it I don't think it's a good idea in C++. setjmp and longjmp have so many problems in C++ that if you think you understand the complexity of using them you're usually wrong. You'd have all the same problems (what destructors get called, what happens if an exception is thrown which are undefined by the standard) with any hand rolled system you come up with. Cheers, Ash

                              _ Offline
                              _ Offline
                              _AnsHUMAN_
                              wrote on last edited by
                              #14

                              HI, Is there a mechanism in C++ apart from GNU where we can get the address of a label. I noticed in GNU compiler there is an operator '&&' (ITS NOT AN AND OPERATOR) ex:void *ptr; ... ptr = &&foo; //(where foo stands to be the name of a label) prefixed to the name of the label to get the address. Can we do something like this on the visual studio compiler.

                              You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

                              A 1 Reply Last reply
                              0
                              • _ _AnsHUMAN_

                                HI, Is there a mechanism in C++ apart from GNU where we can get the address of a label. I noticed in GNU compiler there is an operator '&&' (ITS NOT AN AND OPERATOR) ex:void *ptr; ... ptr = &&foo; //(where foo stands to be the name of a label) prefixed to the name of the label to get the address. Can we do something like this on the visual studio compiler.

                                You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_

                                A Offline
                                A Offline
                                Aescleal
                                wrote on last edited by
                                #15

                                && refers to an R-value reference, it's not the sort of thing you're looking for. You really ought to get yourself a good book on C++. Yes, there is an operator for getting the address of an object in a C++ program, it's the unary & operator. So: int *p = &n; assigns the address of the variable n to the variable p. Cheers, Ash

                                _ 1 Reply Last reply
                                0
                                • A Aescleal

                                  && refers to an R-value reference, it's not the sort of thing you're looking for. You really ought to get yourself a good book on C++. Yes, there is an operator for getting the address of an object in a C++ program, it's the unary & operator. So: int *p = &n; assigns the address of the variable n to the variable p. Cheers, Ash

                                  _ Offline
                                  _ Offline
                                  _AnsHUMAN_
                                  wrote on last edited by
                                  #16

                                  Ah! I specifically mentioned that this operator was on C++ for the GNU compiler. Probably you missed it, it does acquire the address of the label using "&&" but it is not there in C++ with the VS compiler. This link[^] states the implementation and yes I have read a few books but none of them explains this concept for VS :rolleyes:

                                  I am a HUMAN. I have that keyword (??? too much) in my name........ ;-)_AnsHUMAN_b>

                                  A 1 Reply Last reply
                                  0
                                  • _ _AnsHUMAN_

                                    Ah! I specifically mentioned that this operator was on C++ for the GNU compiler. Probably you missed it, it does acquire the address of the label using "&&" but it is not there in C++ with the VS compiler. This link[^] states the implementation and yes I have read a few books but none of them explains this concept for VS :rolleyes:

                                    I am a HUMAN. I have that keyword (??? too much) in my name........ ;-)_AnsHUMAN_b>

                                    A Offline
                                    A Offline
                                    Aescleal
                                    wrote on last edited by
                                    #17

                                    && is an operator supported by g++ 4.1 and later and VC++ 2010. It might have been something vendor specific in earlier versions - I can't comment as I try and avoid anything outside of the C++ standard as it's not portable. You can read more about r-value references here[^]. Cheers, Ash

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups