answersLogoWhite

0

The only way to release a variable is to allow that variable to fall from scope, but once it has fallen from scope we can no longer use it anyway, so the question is somewhat moot. You simply cannot use something that has been released.

Consider the following:

int main () {

int x; // bring x into scope

x = 42; // use x

{ // introduce a new scope

int y; // bring y into this scope

y = x; // use x and y (both are in scope here)

assert (y==42); // OK!

} // end the new scope assert (x==42); // OK!

x = y; // error: no y in this scope!

}

When we exit the inner scope, y is released automatically, so we can no longer refer to it. x remains in scope until we exit the main function but is accessible to all inner scopes.

Note that if we'd declared x in both scopes, we'd only have access to the x in the nearest enclosing scope because we cannot have two entities with the same name in the same scope. The inner x effectively "masks" the outer x, but does not release it. When we exit the inner scope, the original x becomes accessible again:

int x; // global scope

int main () {

int x; // bring an x into local scope (mask the global x)

x = 42; // use x

{ // introduce a new scope

double x; // bring another x into scope (mask the local x)

x = 3.14; // use the new x

}

assert (x==42); // OK!

}

Note that the type does not matter; even with different types we cannot use the type alone to differentiate two objects with the same name in the same scope. The global x remains in scope when we return from main, however the program will exit at that point and will be released automatically.

I suspect you are really referring to reusing (as opposed to using) pointer variables, in which case you should rephrase the question: is it necessary to release a pointer in order to re-use that pointer? The simple answer is no, it is not necessary, however it really depends upon what is being referred to. If the pointer "owns" the object being referred to then yes, it must be released before the pointer can be reused. If not, then no, it must not be released. However, pointers have no notion of resource "ownership", so the onus of responsibility falls entirely upon the programmer to decide which pointers "own" which resources.

Resource handles (also known as "smart pointers") make the notion of ownership concrete, to the extent that existing resources are released automatically whenever a resource handle is reused or falls from scope. In other words, they behave exactly like ordinary variables would and are therefore much more intuitive than "naked" or "raw" pointers. Moreover, the added functionality comes at zero cost. Implementing shared ownership via resource handles incurs a small but not insignificant cost, however that cost is far outweighed by the ease of use. Nevertheless, we endeavour to minimise the use of shared resources for the same reason we discourage the use of global variables.

User Avatar

Wiki User

8y ago

What else can I help you with?

Related Questions

What is the number before a variable?

the coefficient of the variable


What is it called when the independent variable comes before the dependent variable?

An antecedent variable is one that comes before the dependent variable. They stand by themselves, thus are independent.


What is the coefficient of a term that consists of a single variable?

The numerical value that comes before the variable or, if none, the coefficient is 1.The numerical value that comes before the variable or, if none, the coefficient is 1.The numerical value that comes before the variable or, if none, the coefficient is 1.The numerical value that comes before the variable or, if none, the coefficient is 1.


Why plus sign included before variable in println method of java?

Such a plus sign should not be necessary. In Java, an expression like (+x) is equivalent to (x) (with or without the quotation marks).


What variable should you already know before you perform a experiment?

A manipulated variable. (Apex)


If a radical is multiplied by a number or variable you should put the number or variable the radical sign?

before


The number before a variable in a term?

The coefficient


A variable that changes as a result of the other variable?

The dependent variable.


What is a hypothesis variable?

A hypothesis is not a variable...it is something that you think will happen based on reasoning and observations, before doing an experiment.


Is it necessary for me to check out of the hotel before leaving"?

Yes, it is necessary to check out of the hotel before leaving.


Why is it necessary to dry the ice before adding to the water?

It is not necessary.


Is it necessary to be in a relationship before marriage?

Not necessary but extremley recommended