|
Blue Forest http://www.lslnet.com at 10:18 on June 6, 2006
[So very easily turn ]!
[B] a `C standard mechanism for handling [/b]
Standard C provides several unusual management mechanism, which is also available in standard C + +, although documents related to the first name has changed : the standard C header files from the old "name.h> map to a new standard C + + header file called" cname>. (C header file name prefixes to remember, they are specified in the Standard C Library File)
While the C + + backwards compatibility to preserve the C header files, but I advise you to use in any possible new head of the local paper. For many practical use, the biggest change is the first document in the new namespace for Std statement. Look at the following examples :
[code]
#include "Stdio.h>//, was replaced by the standard C #include" cstdio>
FILE *f = fopen ( "blarney.txt", "r");
Std Std : : : : FILE *f = fopen ( "blarney.txt", "r") ;//or the more C-esque
#include "Cstdio>
Using namespace std;
FILE *f = fopen ( "blarney.txt", "r");
[/code]
Unfortunately, Microsoft 's Visual C + + header files in the new namespace Std simultaneously and under the conditions of the statement, even if this is a standard C necessary. Until the Visual C + + support, I will use the old C style in their name.
(For vendors like Microsoft Treasury, the Secretary to achieve these C header files need to preserve and test the validity of two different sets of code, which is a hard task, and it will not bring any value)
[B] b `absolute termination [/b]
This is a completely abnormal neglect, the answer to that is probably a safe and simple method of the withdrawal. In some cases, this is the right approach.
[code]
Before you scoff. consider that some exceptions betray a condition so severe that reasonable recovery is unlikely anyway. Perhaps the best example is malloc of a small object returning NULL. If the free store manager can 't scrape together a few spare contiguous bytes. your program 's robustness is severely compromised, and the odds of recovery are slim elegant.
[/code]
C standard library header files "is not quite perfect stdlib.h> provides two functions : injured on and exit procedures, abnormal function of a life in paragraphs 4, 5 stage. They fail to return the call, and closing procedures.
While the concept is relevant, but the use of their results is different :
Injured on : rude to end the proceedings. This is the acquiescence of the security operation to end when injured on Call Lane diagnostic procedures. This approach may or may not end with the closure of renewable open documents or delete temporary documents, which your design. Exit civilization : to end the proceedings. Additional documents and the closure of its return to open the state to implement the environmental code, you call atexit exit also registered callback function.
Your usual procedures in the event of a major failure Call injured on the case, why was injured on an immediate end to the tacit consent procedure, you need to keep your data before calling injured on. (Discussed in the "signal.h> Lane will be mentioned again)
For the difference between the two, customers with the implementation of the exit clearance atexit registration code, which they are registered by the calling sequence is the opposite of the order. Examples :
[code]
#include "Stdio.h>
#include "Stdlib.h>
Static void atexit_handler_1 (void)
{
Printf ( "within 'atexit_handler_1' \n");
}
Static void atexit_handler_2 (void)
{
Printf ( "within 'atexit_handler_2' \n");
}
Int main (void)
{
Atexit (atexit_handler_1);
Atexit (atexit_handler_2);
Exit (EXIT_SUCCESS);
Printf ( "this line should never appear\n");
Return 0
}
When run yields LEAVES OF 13 SPECIES OF LAURACEAE
Within 'atexit_handler_2'
Within 'atexit_handler_1'
And returns a success code to calling environment.
*/
[/code]
(Note : If your main function in the process ended without explicit call exit, then you atexit registration processing function will be deployed).
Injured on the exit and return to the caller, not to control, and closing procedures. End conditions
Injured on the exit and unconditional termination of your procedures. You can also conditional to the end of your process, this mechanism is a diagnostic tool by programmers hi-definition assert : "assert.h> following examples :
[code]
#if Defined NDEBUG
# Assert (condition) ((void) 0)
#else
# Assert (condition) _assert ((condition), #condition, __FILE__, __LINE__)
#endif
//*******************************************************//
(62858 : the realization not only assert, for instance :
Visual C + + 6.0 is the realization :
# Assert (exp) (void) ((exp) | | (_assert (#exp, __FILE__, __LINE__), 0));
Borland C + + 5.5 is the realization :
# Assert (exp) ((exp) (void) 0 : _assert (#exp, __FILE__, __LINE__))
As for the function _assert (gcc _assert the library is a macro) is a description of the internal realization that the name does not necessarily have to _assert must, its contents are generally used printf function (often Windows platform Calling Messagebox) output error message (file name and the trip) and Call injured on termination procedures.
//*******************************************************//
In this definition, when a pre-defined symbols NDEBUG time, the claim is invalid, which means that you assert claim in the Debug version only Acer effective. In Release version, the macro does not assert any claim calculation. As this would cause some side effect, for example :
LEAVES OF 13 SPECIES OF LAURACEAE Debug version */
#undef NDEBUG
#include "Assert.h>
#include "Stdio.h>
Int main (void)
{
Int i = 0
Assert (++i> 0);
Printf ( "i is %d\n", i);
Return 0
}
When the output after the operation : LEAVES OF 13 SPECIES OF LAURACEAE
I is 1
*/
Now change the code to release version version NDEBUG definition :
LEAVES OF 13 SPECIES OF LAURACEAE release version */
#defing NDEBUG
#include "Assert.h>
#include "Stdio.h>
Int main (void)
{
Int i = 0
Assert (++i> 0);
Printf ( "i is %d\n", i);
Return 0
}
When the output after the operation : LEAVES OF 13 SPECIES OF LAURACEAE
I is 0
*/
[/code]
Therefore assert China is not only a substantive action, debugging and release version of the results may be greatly different.
Thus, in order to avoid such differences to ensure that the formula can assert includes side-effects.
(62858 assert : make sure there were not any substantive action. Microsoft provided the ASSERT macro and VERIFY etc., which can be used in VERIFY release version, but this is not the standard C and C + +, the function is not strong enough. )
Debug version only, will be deployed _assert assert function. Following is a similar code :
[code]
Void _assert (int test, char const *test_image.
Char const *file, int line)
{
If (!test)
{
Printf ( "Assertion failed : %s, file %s, %d\n line."
Test_image, file and line);
Injured on ();
}
}
[/code]
So. In asserting that the failure to produce a detailed diagnostic information, including source code file name and businesses, after calling injured on, I will give examples of this mechanism is very rough; Realize you were changed in the reservoir
Assert models are used in debugging the logic wrong, it will never exist in the procedures for issuing Lane.
[code]
LEAVES OF 13 SPECIES OF LAURACEAE 'f' never called by other programs */
Static void f (int 6-12)
{
Assert (p> NULL);
/* ... */
}
[/code]
I point out the logical errors and other mistakes on the use of mileage assert the distinction :
[code]
LEAVES OF 13 SPECIES OF LAURACEAE. . . Get file 'name' from user. . . */
FILE *file = fopen (name, mode);
Assert (file> NULL); LEAVES OF 13 SPECIES OF LAURACEAE very suspicious usage? ? ? Questionable use */
[/code]
Such errors appear in abnormal expression, but it does not Bug, it is running abnormal response to assert may not be correct, you should use other mechanisms, I presented below.
[B] c `non-local Goto[/b]
In comparison with the injured on the exit, so you can have a greater management goto unusual method, which sadly is the gotos are partial (Local) : goto can point to the logo in their internal functions, it is not an arbitrary process in place to control it. In order to overcome these restrictions, the standard C database setjmp and longjmp function, it may goto to any place. First document "setjmp.h> definition of these functions, including indirect jmp_buf, which is simple and direct :
Setjmp (j) to install goto indicators used jmp_buf j current process context information to the initial target. Guidelines include procedures for location information typical of this context, stack and frame indicators, there are memory and register values. When initialization context information, setjmp return 0.
Later call longjmp (j, r) j designated object to the outcome of the local goto (initialized before calling setjmp j), the goal of non-local goto Call, setjmp return r, r 0 1 return. (Remember : setjmp in this context can return 0)
By having two classes of return values, setjmp lets you determine how it 's being used. When setting j. setjmp works as you normally expect; but as the target of a long jump, setjmp "wakes up" from outside its normal context.
If the use longjmp to trigger abnormal termination, setjmpgoto to correct the anomaly approach Lane.
[code]
#include "Setjmp.h>
#include "Stdio.h>
Jmp_buf j;
Void raise_exception (void)
{
Printf ( "exception raised\n");
Longjmp (j 1); LEAVES OF 13 SPECIES OF LAURACEAE jumps to exception handler */
Printf ( "this line should never appear\n");
}
Int main (void)
{
If (setjmp (j) ====== 0)
{
Printf ( " 'setjmp' is initializing 'j' \n");
Raise_exception () ;//Restore context
Printf ( "this line should never appear\n");
}
Else
{
Printf ( " 'setjmp' was just jumped into\n");
LEAVES OF 13 SPECIES OF LAURACEAE this code is the exception handler */
}
Return 0
}
LEAVES OF 13 SPECIES OF LAURACEAE operating results :
'Setjmp' is initializing 'j'
Exception raised
'Setjmp' was just jumped into
*/
[/code]
: Jmp_buf used to restore other context is invalid, jmp_buf j; Look at the following examples :
[code]
Void f (void)
{
Setjmp (j);
}
Int main (void)
{
F ();
Longjmp (j 1); LEAVES OF 13 SPECIES OF LAURACEAE logic wrong */
Return 0
}
[/code]
You must call in the current context that the only non-local goto setjmp.
Signals
Secretary of the event management also ISO C standard packages (though more primitive). This event management packages installed with the definition signal, together with the trigger and the standard approach. Those signals can be different or abnormal expression of the expansion of their event triggered Lane. This is the purpose of discussion. I just concentrated on abnormal signal.
For the use of these management package, which should include the standard header file, "signal.h>, the first definition of a document and raise signal function sig_atomic_t signal types and the beginning of the implementation of the macro-SIG event. Here is the standard six-signal, but you can increase the library to the other. However, setting up a fixed definition of the function of the signal "signal.h>, you can expand your own signal to the setup function. Call raise to trigger signal and the corresponding access to the process. Operation System provides a tacit approach, but you can install your own signal behavior. Sig_atomic_t approach to communications with the external procedures. The proposed name for the type, each object is allocated to the atom, or interrupted (interrupt-safe).
When you register signal processing methods, the general processing function to provide your address. Every function must accept int value, and return void. In this way, signal processing methods as setjmp; abnormal context will only receive a single integer :
[code]
Void handler (int signal_value);
Void f (void)
{
Signal (SIGFPE, handler); LEAVES OF 13 SPECIES OF LAURACEAE registration process */
/* ... */
Raise (SIGFPE); LEAVES OF 13 SPECIES OF LAURACEAE through 'SIGFPE' Call to process */
}
[/code]
There are two options available for dealing with specific installation :
Signal (SIGxxx, SIG_DFL), 4003rd acquiescence of the approach using the system.
Signal (SIGxxx, SIG_IGN), 4003rd told overlooked signal system.
In all cases, the signal to return to the previous process at the target or SIG_ERR (mean registered unsuccessfully)
When the call processing method, which means that the beginning of abnormal signal processing. You can freely transfer injured on the approach, exit or longjmp to effectively end anomalies. Some interesting places : in fact, also injured on their own in-house call raise (SIGABRT), the tacit consent of diagnostic information and show SIGABRT unusual approach the end of proceedings. However, you can install your own way to change this behavior SIGABRT Exception Handling :
But you can not change the injured on the termination procedures, the following is injured on a similar code :
[code]
Void injured on (void)
{
Raise (SIGABRT);
Exit (EXIT_FAILURE);
}
[/code]
Here, if you return SIGABRT unusual approach, also injured on the end of proceedings.
Pete Sampras in the C standard, signal processing methods abnormal behavior also has a limit. Look at the details of standards 7.7.1.1.
(Translator's Note : The following is the draft document standard C : http://anubis.dkuug.dk/JTC1/SC22/WG14/www/docs Lane n843.pdf)
Report variables
"Setjmp.h> with" signal.h> normal for detecting anomalies in a notification process to be : When the notification of unusual event, exception handling process will be awakened. If you prefer to check mistakes in the code, then the standard database of such acts, the first document contains "errno.h> Lane. The first document defines errno, coupled with some commonly used to value errno. Standard Library, asked the Radio : EDOM, ERANGE, EILSEQ, namely domain, range and multibyte-sequence error, but the compiler may add other providers.
Errno includes the provision and access code : Treasury Secretary abnormal object code (individual integers), the target value given errno abnormal copy, and then detected anomalies in user mode.
Mainly focused on the use of the library errno "math.h> with" stdio.h>. To be installed at the beginning of the proceedings errno 0, but once again there is no reservoir code will be automatically installed errno to 0 (that is to say when you deal with the error, it must be re-deployed errno set up standards for the Secretary-0). Therefore, error detection, you must set up 0, and then continue to call Secretary standard procedures. Following are examples :
[code]
#include "Errno.h>
#include "Math.h>
#include "Stdio.h>
Int main (void)
{
Double x, y, the result;
LEAVES OF 13 SPECIES OF LAURACEAE. . . Somehow set of 'x' and 'y'. . . */
Errno = 0
Result = pow (x, y);
If (errno ====== EDOM)
Printf ( "domain error on y pair\n");
Else if (errno ====== ERANGE)
Printf ( "error range on result\n");
Else
Printf ( "x to the y = %d\n", (int) result);
Return 0
}
Note : errno not applied to the object :
Int *_errno_function ()
{
Static int real_errno = 0
Return &real_errno;// uncalled
}
# Errno (*_errno_function ())
Int main (void)
{
Errno = 0
/* ... */
If (errno ====== EDOM)
/* ... */
}
[/code]
Parameters and return values
Errno- as unusual objects but no restrictions :
All relevant parts together, allowing setting up and testing the same target.
Occasionally, objects can be changed.
If you did not call other procedures before they are detected or target replacement, then you will miss abnormal.
Acer targets abnormal internal targets were hiding.
On the whole nature is not static object thread.
In summary, every object you are vulnerable : they are too easily abused, not in your compiler warning messages, you may be impossible to predict the behavior of the procedure.
Eliminate these mistakes, you need to be targeted :
Composed of two parts : part abnormal, and the second abnormal.
Getting the right value.
Do not hide them.
Is the thread safety.
Function return value should meet these standards, as they call function is the creation of the unnamed interim targets, which can be deployed were understandable. When a call completed, the caller may object detection or copy the return value; After returning to the original target disappeared, it is not in the use of the object. As the unnamed target is the object, it can not be hidden.
(In C + +, I assume the function call expressions left only to return value means that the caller can use to return, I only limit such discussions in the C compatible technology, which some, but not invoked C (C standard -C98 joined cited support), so my assumption is reasonable)
[code]
Int f ()
{
Int error;
/* ... */
If (error) LEAVES OF 13 SPECIES OF LAURACEAE Stage 1 : The error occurred */
Return 1; LEAVES OF 13 SPECIES OF LAURACEAE Stage 2 : generate exception object */
/* ... */
}
Int main (void)
{
If (f ()> 0) Stage 3 : LEAVES OF 13 SPECIES OF LAURACEAE detect exception */
{
Stage 4 : LEAVES OF 13 SPECIES OF LAURACEAE handle exception */
}
Stage 5 : LEAVES OF 13 SPECIES OF LAURACEAE recover */
}
Standard C Library for the return value is the better way to spread abnormal, please consider the following examples :
If ((p = malloc (n)) ====== NULL)
/* ... */
If ((c = getchar ()) ====== EOF)
/* ... */
If ((ticks = clock ()) "0)
/* ... */
[/code]
Note : This phrase in a test carried out with a method of capturing the return value is more typical usage. It has two different meanings : the legal value of data with outliers. This code must explain two calculations know where it is the right path.
Function return value of the method was applied to a lot of public instruction in the use of Microsoft COM model. COM object unusual way to return HRESULT to inform Microsoft of the value of the use of 32-bit integers without symbols. When the discussion is not only an example. COM would only return to the state and the return of data, other information through point-parameters.
External indicators and parameters used to be a variant form of a C + + function return value, but they have the following differences :
You can ignore or discard the return value. However, the corresponding external binding to the message, you can ignore them completely, and the return value, function parameters tightly coupled with a call to bring.
Numerical any external parameters can be read back, although the function return value can only send one, but in the external parameters can provide more logical to return to duty.
Object : to return to duty in the interim prior to their transfer function is non-existent, disappeared after they were deployed. Calling targets abnormal function was longer than the lifespan. |
[So very easily turn ]!
Well, there are some really read! |
[So very easily turn ]!
Good to see these codes have more time!
---------------------
The revolution has yet to succeed, needs comrades! |
[So very easily turn ]!
...
Ah, written terrific! (Unfortunately, no dual K sentence understand : - () |
[So very easily turn ]!
TU 8 8 wrong wrong! ~ |
| |