When a variable goes out of scope, does it mean that it does not exist?

I'm not sure I understand the scope - a variable outside the scope (I use Ruby) exists somewhere or it ceases to exist (I know that you cannot access it). Would it be inaccurate to say that a variable outside of scope no longer exists?

Perhaps this is a philosophical question.

+4
source share
7 answers

If you use a managed language, then you do not allocate and do not allocate memory as much as possible, it no longer exists.

Technically, this happens, but the GCs are not deterministic, so it's technically difficult to say when it actually disappears.

+5
source

The variable does not match its value.

The variable itself ceases to exist when it goes beyond the scope. The value that the held variable can represent an object, and this object can continue to exist over the lifetime of the variable. The garbage collector returns the object later.

+3
source

When it goes out of scope, it still exists (in the sense that it has some memory allocated for it) for some time until garbage collection clears it. But, as you mean, it has lost that name and is unattainable.

+2
source

When a variable goes out of scope, does anyone around hear its scream?

This is not a ruby ​​question, but a general question about garbage collection. In a garbage-collected language like Ruby or C #, when a variable falls out of scope, it is flagged in some way, which means that it is no longer used. When this happens, you can no longer attack him, and he sits around poking his thumbs, but he still has a dedicated memory.

At some point, the garbage collector will wake up and look for variables marked as unused. He will get rid of them, and at this moment they are no longer in memory.

It may be harder than this, depending on how the garbage collector works, but it's close enough :)

+2
source

It exists for a little while until the garbage collector provides it (if it can).

+1
source

Rob Kennedy answered this correctly, but I thought I'd add a little more detail.

The important thing to recognize is the difference between a variable and the value that it represents.

Here is an example (in C #, because I don't know Ruby):

object c = null; if (1 == 1) // Just to get a different scope { var newObj = new SomeClass(); newObj.SomeProperty = true; c = newObj; } 

In the above code, newObj goes beyond the scope at the end of the if statement and, as such, "does not exist", but the value it referred to is still alive and good, which c refers to. When all references to the object disappear, the garbage collector will take care of cleaning it.

+1
source

If you are talking about file objects, this becomes more than a philosophical question. If I remember correctly, files do not close automatically when they go out of scope - they only close if you ask them to close, or if you use the File.open do |file| style File.open do |file| or if they collect garbage. This can be a problem if other code (or unit tests) tries to read the contents of this file, and it has not yet been cleared.

0
source

All Articles