Getting addresses...setjmp/longjmp
-
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_
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
-
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
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_
-
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.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_
-
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_
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"< -
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"<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
-
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
-
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.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
-
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
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_
-
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_
&& 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
-
&& 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
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>
-
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>