|
INTERVIEW QUESTIONS
C
FUNCTIONS IN C
DETAILS
Question: What is the difference between goto and longjmp() and setjmp()?
Answer: A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution.
Generally, a jump in execution of any kind should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program.
A goto statement simply bypasses code in your program and jumps to a predefined position. To use the goto statement, you give it a labeled position to jump to. This predefined position must be within the same function. You cannot implement gotos between functions.
When your program calls setjmp(), the current state of your program is saved in a structure of type jmp_buf. Later, your program can call the longjmp() function to restore the program’s state as it was when you called setjmp().Unlike the goto statement, the longjmp() and setjmp() functions do not need to be implemented in the same function.
However, there is a major drawback to using these functions: your program, when restored to its previously saved state, will lose its references to any dynamically allocated memory between the longjmp() and the setjmp(). This means you will waste memory for every malloc() or calloc() you have implemented between your longjmp() and setjmp(), and your program will be horribly inefficient. It is highly recommended that you avoid using functions such as longjmp() and setjmp() because they, like the goto statement, are quite often an indication of poor programming practice.
|
|
|
Category |
Functions in C Interview Questions & Answers -
Exam Mode /
Learning Mode
|
Rating |
(0.3) By 8741 users |
Added on |
10/22/2009 |
Views |
72580 |
Rate it! |
|
|
Question:
What is the difference between goto and longjmp() and setjmp()?
Answer:
A goto statement implements a local jump of program execution, and the longjmp() and setjmp() functions implement a nonlocal, or far, jump of program execution.
Generally, a jump in execution of any kind should be avoided because it is not considered good programming practice to use such statements as goto and longjmp in your program.
A goto statement simply bypasses code in your program and jumps to a predefined position. To use the goto statement, you give it a labeled position to jump to. This predefined position must be within the same function. You cannot implement gotos between functions.
When your program calls setjmp(), the current state of your program is saved in a structure of type jmp_buf. Later, your program can call the longjmp() function to restore the program’s state as it was when you called setjmp().Unlike the goto statement, the longjmp() and setjmp() functions do not need to be implemented in the same function.
However, there is a major drawback to using these functions: your program, when restored to its previously saved state, will lose its references to any dynamically allocated memory between the longjmp() and the setjmp(). This means you will waste memory for every malloc() or calloc() you have implemented between your longjmp() and setjmp(), and your program will be horribly inefficient. It is highly recommended that you avoid using functions such as longjmp() and setjmp() because they, like the goto statement, are quite often an indication of poor programming practice. Source: CoolInterview.com
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|
|
Related Questions |
View Answer |
|
How do you determine whether to use a stream function or a low-level function?
|
View Answer
|
|
Is it better to use a macro or a function?
|
View Answer
|
|
How do you use a pointer to a function?
|
View Answer
|
|
Why should I prototype a function?
|
View Answer
|
|
What is a static function?
|
View Answer
|
|
Is using exit() the same as using return?
|
View Answer
|
|
Differentiate between a linker and linkage?
|
View Answer
|
|
What is a function and built-in function?
|
View Answer
|
|
What is an argument ? differentiate between formal arguments and actual arguments?
|
View Answer
|
|
What is the purpose of main( ) function?
|
View Answer
|
|
What are the advantages of the functions?
|
View Answer
|
|
What is a method?
|
View Answer
|
Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.
View All Functions in C Interview Questions & Answers - Exam Mode /
Learning Mode
|