如何判断变量何时超出范围? [英] How to tell when a variable goes out of scope?

查看:106
本文介绍了如何判断变量何时超出范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在进行引用计数时,任务之一是当变量超出范围时减小计数器。但是我最大的问题是,在实现引用计数器的实现级别上,我无法说出变量何时超出范围。

In doing reference counting, one of the tasks is to "decrement the counter when the variable goes out of scope". But my biggest problem is I can't tell in my head when a variable goes out of scope, at the implementation level of implementing a reference counter.

可以解释一下全部(或主要)如何使变量超出范围?

Could one explain all (or the main) ways in which a variable can go out of scope?

我是专门针对高级编程语言而不是玩具/入门级本科生语言。我在想像JavaScript或Rust,它们具有闭包和嵌套函数定义(至少在JavaScript情况下)。同样,当您使用指针等并使用可变函数参数时。假设您将一个可变值传递给一个函数,然后使用该可变值返回一个闭包,诸如此类。

I am specifically talking about in the case of a highly advanced programming language, not a toy / introductory undergraduate language. I am thinking like with JavaScript or Rust, which has closures and nested function definitions (at least in the case of JavaScript). Also when you are using pointers and such and using mutable function parameters. Say you pass in a mutable value to a function, then return a closure using that mutable value, stuff like that.

当变量超出范围时,您可以通过什么方式知道? ?如何组织得足够好以便可以将其添加到引用计数器?

What are all the ways you can tell when a variable goes out of scope? How do I get this organized enough so I can add it to a reference counter?

推荐答案

当执行会到达声明它的块的末尾。

A local variable goes out of scope when execution reaches the end of the block in which it was declared.

全局/静态变量永远不会超出范围。

Variables that are global / static don't ever go out of scope.

作为复合数据类型的字段(类/对象,结构/记录,数组等)的变量可能没有作用域。本身,但如果是这样,则取决于它们所属的复合数据类型实例的范围。

Variables that are fields of a composite data type (an class / object, a struct / record, an array, etc) may not have a "scope" per se, but if they do, it is determined by the scope of the composite data type instance they are part of.

如果您试图在编译时对此进行分析……您使用符号表。有关编译器编写的教科书对此进行了介绍。

If you are trying to analyses this at compile time ... you use a symbol table. This is covered in textbooks on compiler writing.

这篇关于如何判断变量何时超出范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆