Scope Vs. Lifetime of Variable

Scope vs. Lifetime of Variable

What is Scope?

Scope is the region or section of code where a variable can be accessed.

What is a lifetime?

Lifetime is the time duration where an object/variable is in a valid state.

For, Automatic/Local non-static variables Lifetime is limited to their Scope.

In other words, automatic variables are automagically destroyed once the scope({,}) in which they are created ends. Hence the name automatic to begin with.

What is wrong in your code example?

So Yes your code has an Undefined Behavior.

In your example scope of *p is entire function body after it was created.

However, x is a non-static local/automatic variable and hence the lifetime of x ends with it's scope i.e the closing brace of } in which it was created, once the scope ends x does not exist. *p points to something that doesn't exist anymore.

Note that technically x does not exist beyond its scope however it might happen that the compiler did not remove the contents of x and one might be able to access contents of x beyond its scope through a pointer(as you do).However, a code which does this is not a valid C++ code. It is a code which invokes Undefined Behaviour. Which means anything can happen(you might even see value of x being intact) and one should not expect observable behaviors from such a code.

Scope vs life of variable in C

Scope is the region where the variable is accessible.

Life time is the time span during which an object remains valid.

An simple example:

#include <iostream.h>

void doSomething()
{
x = 5; //Error! Not Accessible
}

int main()
{

int x = 4;
std::cout<< x << endl;
{
int x = 2;
cout << x << endl;
}
doSomething();
std::cout<< x << endl;
return 0;
}

The above gives the output:

4

2

4

In above program,

lifetime of variable x = 4 is throughout the main, i.e: It remains alive throughout the execution of the main, Also it is accessible within the main, that is its scope. Note that it is not accessible in the function because it is beyond the scope of the variable x.

while scope and lifetime of variable x = 2 is within the enclsing braces{ } inside the main.

scope or lifetime of variable in C

its basically a function activation record,which will be pushed on the system stack and when your function is returning it will first copy all values to the return result area,that is nothing but a=foo(); and then it will destroy that function activation record from the system stack,I hope it would help

Scope and lifetime of local variables in C

As you already know that b goes out of scope in each instance, and accessing that memory is illegal, I am only dumping my thoughts on why only one case throws the warning and other doesn't.

In the second case, you're returning the address of a variable stored on Stack memory. Thus, the compiler detects the issue and warns you about it.

The first case, however skips the compiler checking because the compiler sees that a valid initialized address is assigned to a. The compilers depends in many cases on the intellect of the coder.

Similar examples for depicting your first case could be,

char temp[3] ;
strcpy( temp, "abc" ) ;

The compiler sees that the temp have a memory space but it depends on the coder intellect on how many chars, they are going to copy in that memory region.

Scope and Lifetime of Inline Variables Declared in Nested Blocks, Do the scope apply for Objects as well?

You still have to free the object manually, just as before.

The lifetime of the object has nothing to do with the scope of the variable. This has always been the case, and still applies. (Indeed, it is very much possible to create an object without assigning it to a variable at all.)

Even in this old-school example, the object would be leaked without the Free, even though the Bitmap variable goes out of scope at the procedure's end;.

procedure Test;
var
Bitmap: TBitmap;
begin
Bitmap := TBitmap.Create;
try
// use the bitmap
finally
Bitmap.Free;
end;
end;

Without Bitmap.Free, the object would still exist on the heap (and it might continue to use any non-memory resources it has access to), even though you have misplaced your last pointer to it!



Related Topics



Leave a reply



Submit