answersLogoWhite

0


Best Answer

No. In most designs, local variables are considered a good thing thanks to the isolation that they provide (no other code can "abuse" your variable). However, it is important to distinguish between local static variables, local automatic variables, and locally made references to the heap.

Local static variables are initialized only once at application initialization time, and retain their most recently assigned value over multiple invocations of the function which contains them. Local static variables must be used with care and can lead to non-reentrant code (the famous example being the strtok() CRT function). However, these variables are usually great to track the total number of something, or the minimum or maximum value of a particular item, etc.

In C, local automatic variables are the most common form of local variables. These are typically allocated on the stack, so care must be taken when declaring large automatic variables, or when using recursion.

When the size of a local automatic variable is a concern, dynamic memory management method should be considered those maintain a local auto pointer on the stack but place the data in the heap. This method allows to allocate large amounts of data without burden on the stack, and the lifetime of such data is no longer limited by the lifetime of the scope in which it was allocated.

Very rare cases exist where local variables are generally avoided; these are mostly in the area of very resource limited embedded programming.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Should you avoid local variable scope?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

In most languages where does a local variables scope begin and end?

A local variable only exists within the scope in which it is declared. As soon as the scope ends, the variable ceases to exist. { // beginning of a scope, i does not yet exist int i = 42; // local variable declared, i now exists } // end of scope, i no longer exists


What is a local variable and What statements are able to access a local variable?

A local variable is a variable that can only be called on by the module. Where as a global variable can be called upon by any module. Only statements made inside the same module can call on a local variable.


What is Variable Shadowing in Java?

In Java, there are three kinds of variables: local variables, instance variables, and class variables. Variables have their scopes. Different kinds of variables have different scopes. A variable is shadowed if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope, the one in the outer scope is shadowed.A Local Variable Shadows An Instance VariableInside a class method, when a local variable have the same name as one of the instance variable, the local variable shadows the instance variable inside the method block.


Describe the differences between local and module-level variables?

local Variable A Local variable is a variable whose scope is limited to the Block of the Subroutine defining it. Private Sub Command1_Click Dim a as integer End Sub Module Level Variable A Module Level variable is a variable whose scope is limited to the Form Module defining it. Public Sub Command1_Click Dim a as integer End Sub Global Level Variable A Global Level variable is a variable whose scope can be limited to the entire project defining it. Private Sub Classjnitialize () Dim a As Integer End Sub


What is the difference between local and global variable of C language?

4m Sai. Gloabal Variable is variable which is declared before main (). int a=10; main() { } here a is a global variable. where ever u r using this variable ... we'll get the value as 10. Local Variable is a variable which is declared inside the main(). int a=10; main() { int a=5; ........ ....... } here a=5 is a local variable and a=10 is called as global variable. then if u want to get a value... printf("%d",a); then result 'll be a=5; B'cos compiler gives main preference to local variables.. if any local declareation is not there then it 'll prefer global variable.

Related questions

In most languages where does a local variables scope begin and end?

A local variable only exists within the scope in which it is declared. As soon as the scope ends, the variable ceases to exist. { // beginning of a scope, i does not yet exist int i = 42; // local variable declared, i now exists } // end of scope, i no longer exists


Scope of static variables?

Scope of static variable is with in the file if it is static global. Scope of static variable is with in the function if variable is declared local to a function. But the life time is throughout the program


What is a local variable and What statements are able to access a local variable?

A local variable is a variable that can only be called on by the module. Where as a global variable can be called upon by any module. Only statements made inside the same module can call on a local variable.


What is Variable Shadowing in Java?

In Java, there are three kinds of variables: local variables, instance variables, and class variables. Variables have their scopes. Different kinds of variables have different scopes. A variable is shadowed if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope, the one in the outer scope is shadowed.A Local Variable Shadows An Instance VariableInside a class method, when a local variable have the same name as one of the instance variable, the local variable shadows the instance variable inside the method block.


Which operator is allow to access hidden global variable?

The scope resolution operator, ::, overrides local scope and allows access to objects that are hidden due to global to local scope rules.


Describe the differences between local and module-level variables?

local Variable A Local variable is a variable whose scope is limited to the Block of the Subroutine defining it. Private Sub Command1_Click Dim a as integer End Sub Module Level Variable A Module Level variable is a variable whose scope is limited to the Form Module defining it. Public Sub Command1_Click Dim a as integer End Sub Global Level Variable A Global Level variable is a variable whose scope can be limited to the entire project defining it. Private Sub Classjnitialize () Dim a As Integer End Sub


What is the difference between local and global variable of C language?

4m Sai. Gloabal Variable is variable which is declared before main (). int a=10; main() { } here a is a global variable. where ever u r using this variable ... we'll get the value as 10. Local Variable is a variable which is declared inside the main(). int a=10; main() { int a=5; ........ ....... } here a=5 is a local variable and a=10 is called as global variable. then if u want to get a value... printf("%d",a); then result 'll be a=5; B'cos compiler gives main preference to local variables.. if any local declareation is not there then it 'll prefer global variable.


What is the difference between a global scope and a local scope?

Scope refers to the accessibility of an object. A globally scoped object is available globally (to the entire program) while a locally scoped object is only available locally (to the containing function or class). In general, you want to avoid global objects whenever possible as it tends to introduce dependencies.


What variable scope is preferable in modules to avoid overwriting existing data?

C: static other: (default)


Analyze the difference among local module-level and global variables and explain how ActiveX DLLs are created?

Local Variable A Local variable is a variable whose scope is limited to the Block of the Subroutine defining it. Private Sub Command1_Click Dim a as integer End Sub Module Level Variable A Module Level variable is a variable whose scope is limited to the Form Module defining it. Public Sub Command1_Click Dim a as integer End Sub Global Level Variable A Global Level variable is a variable whose scope can be limited to the entire project defining it. Private Sub Class_Initialize () Dim a As Integer End Sub


What is the scope of any variable?

The Scope of a variable defines the areas of a program where this variable would be visible and can be used. For ex: a. Method variables - are visible only inside the method where they are declared and hence their scope is only the method b. Class variables - are visible inside the class and can be used by any method inside the class and hence their scope is the whole class.


Explain different types of coding style in software engineering?

Meaningful variable names. Always use descriptive and meaningful variable names. Don't worry about the length. Meaningful variable names help other people reading your code understand what is going on. Example: if (a <= = 24) What is a? This is confusing. if (vacationHours <= = 24) See, better. <li>Variable scope indicated by name. Code is easier to read and understand if each variable indicates it's scope. One way to do this is to add prefixes to variable names, for example: Scope = Class member or instance variable: m_hours Scope = Class static variable: s_hours Scope = Parameter passed in a method call: p_hours Scope = local variable to a method: l_hours Regards ~Anoop