[ Pobierz całość w formacie PDF ]
.The scope of the variable is alsolimited to the block in which it is defined.An internal static variable is just like an external static variable except that the visibility of the variable is limited to ablock.It is also important to understand how an initialized static variable behaves.Consider the following code.An example in using staticvoid Testing( void ){LOOP(3) {static int nTest1=100;int nTest2=100;printf( "nTest1=%d, nTest2=%d\n", nTest1, nTest2 );++nTest1;++nTest2;} ENDLOOP/* Testing */Output from calling Testing() three timesnTest1=100, nTest2=100nTest1=101, nTest2=100nTest1=102, nTest2=100nTest1=103, nTest2=100nTest1=104, nTest2=100nTest1=105, nTest2=100nTest1=106, nTest2=100nTest1=107, nTest2=100nTest1=108, nTest2=100As you can see from this example, nTest1 is initialized only once, while nTest2 is initialized on each pass through the block.The rule is that the initialization of anystatic variables takes place at compile-time and that the initialization of automatic variables takes place at run-time.Static variables are initialized once at compile-time.Automatic variables are initialized as needed at run-time.7.10 Place Variables in the Block NeededHow many times have you tracked down a bug only to realize that you used an automatic variable before it was properly initialized, or used the variable well after itshould have been, when it no longer contained an appropriate value? While this may not happen to you as you write the function, it becomes a lot more likely whenyou go back to the code at a later date and modify it.The solution to this problem is to define variables only in the innermost scope in which they are needed.A new scope is created any time you use a begin brace {.The scope is terminated by an ending brace }.This means that your if statements, while statements, and so on.create new scopes.Variables defined within a scopeare visible only within that scope.As soon as the scope ends, so does your access to the variable.Consider the following code fragment.Limiting the scope of a variableLOOP(strlen(pString)) {char c=pString[loop];.} ENDLOOPIn this example, c is visible only within the loop.As soon as the loop exits, c is longer visible.In fact, it can be defined and reused in another scope.Define variables in the scope in which they are needed.In standard C, variables can be defined only at the beginning of a scope before the main body of code.In C++, variables can be defined wherever a statement isvalid.A useful #define for both C and C++ is the NewScope define.NewScope define#define NewScopehttp://www.duckware.com/bugfreec/chapter7.html 2003-06-16Writing Bug-Free C Code: General Tips Strona 4 z 10NewScope is a syntactical place holder (defined to be nothing) that allows a new scope to be introduced into a program.It also allows for a natural indentation style.Using NewScopevoid APIENTRY Function( args ){/*--- Comment ---*/(code block)/*--- Using NewScope ---*/NewScope {type var;(code block that uses var)}} /* Function */NewScope is useful in both C and C++ because it allows variables to be created that are private to a block.As soon as the block exits, the variables declared in theblock are no longer visible and cannot be referenced.7.11 Arrays on the StackWhen using arrays that are declared on the stack, you must be careful not to return a pointer to one of these arrays back to the calling function.Consider thefollowing example.A program with a subtle bug#include#include#includechar *myitoa( int nNumber ){char buffer[80];sprintf( buffer, "%d", nNumber );return (buffer);}int main(void){printf( "Number = %s\n", myitoa(234) );return 0;}This program contains a subtle bug in that myitoa() is returning the address of buffer, an array on the stack.It is subtle because despite this bug, the program stillworks properly!It is a bug to return the address of an array on the stack back to a calling function because after the function returns, the array is now in the part of the stack that nofunction owns
[ Pobierz całość w formacie PDF ]
Tematy
IndexPamela C. Corley Concurring Opinion Writing on the U.S. Supreme Court (2010)
Harris Writing Back to Modern Art ~ After Greenberg, Fried and Clark
dodatki+za+free
Chodzmy Razem LLOYD JOSIE REES EMLYN
Robert Jordan Koło czasu 09 Ognie Niebios
WARSZAWA 1939 1945 STRATY WOJENNE CZ II
Karol May 01 Nad Rio de la Plata
Ustawa z dnia 27 lipca 2001 r. Prawo ochrony Âśrodowiska
Namiestnicy Chrystusa , Peter de Rosa
plan pracy wychowawczo1