首页 \ 问答 \ 参考变量是否占用内存?(Does reference variable occupy memory? [duplicate])

参考变量是否占用内存?(Does reference variable occupy memory? [duplicate])

这个问题在这里已经有了答案:

我读过参考变量与原始变量共享相同的内存地址 ,但也占用了堆栈上的一些空间。 并且,由于引用具有与原始变量相同的内存地址,因此它也被称为别名。

所以,我的问题是内存分配如何完成参考变量


This question already has an answer here:

I have read that reference variable shares the same memory address with the original variable but also takes up some space on the stack. And as reference has the same memory address as the original variable, it is also known as alias.

So, my question is how memory allocations are done for reference variables ?


原文:https://stackoverflow.com/questions/29322688
更新时间:2021-09-11 17:09

最满意答案

回答你的第一个问题:

a) 为什么文档将此1作为初始值包含在实际上不需要的情况下?

apidock注入不带1作为初始值:

如果没有为memo显式指定初始值,则使用collection的第一个元素作为memo的初始值。

第二个问题的答案在于第一个问题的答案,因为它初始化对象作为应用注入的数组的第一个元素。


To answer your first question:

a) Why the documentation includes this 1 as initial value when, in fact, it is not needed?

inject does not take 1 as initial value, from the apidock:

If you do not explicitly specify an initial value for memo, then uses the first element of collection is used as the initial value of memo.

Answer of second question lies in the answer of first itself, as it initialises the object as the first element of array on which inject is being applied.

相关问答

更多
  • 好吧,在Ruby中很少有doctest工具的例子,最新鲜的(现在)是yard-doctest : 认识YARD :: Doctest - 简单而神奇的宝石,它会自动解析你的@example标签并将它们变成测试! 可能没有像rustdoc那么强大,但它确实需要它。 Well, there few examples of doctest tool in Ruby, and the most fresh one (for now) is yard-doctest: Meet YARD::Doctest - sim ...
  • 在这种情况下,您需要为inject提供初始值,因为如果不这样做,初始值只是数组中的第一个元素: puts threads.inject(0) { |sum, e| sum + e.value}.to_f / threads.size You need to provide an initial value for inject in this case, since if you don't, the initial value is simply the first element in the arr ...
  • 使用Enumerable#group_by似乎更合适: >> "aaabdbbcccaaabcacba".chars.group_by { |x| x }.map { |key, a| [key, a.size] } => [["a", 8], ["b", 5], ["d", 1], ["c", 5]] 给定代码的问题: 传递给注入的块不关心不匹配的字符。 sum + 1 if letter == char和letter == char ? sum + 1 : sum sum + 1 if letter ...
  • 回答你的第一个问题: a) 为什么文档将此1作为初始值包含在实际上不需要的情况下? 从apidock注入不带1作为初始值: 如果没有为memo显式指定初始值,则使用collection的第一个元素作为memo的初始值。 第二个问题的答案在于第一个问题的答案,因为它初始化对象作为应用注入的数组的第一个元素。 To answer your first question: a) Why the documentation includes this 1 as initial value when, in fact ...
  • 无法保证Ruby中会引发哪个异常。 用户可能发送中断,您的文件系统可能无法写入等。而Ruby不保证这一点。 但是对于File操作,我可能会看一下IOError及其子EOFError。 这可以提供处理非例外(合理预期)事件的能力,例如文件不存在,或者没有读访问权限或没有写访问权限,这些事项可以编程,救援和尝试处理。 您还可以编写自己的异常,引发自定义异常(可能继承StandardError)并提供相应的操作或反馈。 这是我现在使用Ruby 1.9.3-p327从我的计算机生成的当前异常层次结构 BasicOb ...
  • inject就像fold一样,并且在其他语言中非常有效,特别是fold_left ,因为它是尾递归的。 inject is like fold, and can be very efficient in other languages, fold_left specifically, since it's tail-recursive.
  • 我不熟悉Ruby,但从描述来看,它似乎与array_reduce类似。 mixed array_reduce ( array $input , callback $function [, mixed $initial = NULL ] ) array_reduce()迭代地将函数函数应用于数组输入的元素,以便将数组减少为单个值。 除了“减少”之外,这种操作有时也称为“折叠” ; 在Mathematica: Fold[f, init, {a, b, c, d}] == f[f[f[f[init, a], b] ...
  • 它是内置的,它是懒惰的: perms = ['A', 'B'].repeated_permutation(2) #use the resulting enumerator like this: perms.each{|perm| p perm} It's built in, and it is lazy: perms = ['A', 'B'].repeated_permutation(2) #use the resulting enumerator like this: perms.each{|perm| ...
  • 我使用YARD 。 I use YARD.
  • 相当于折叠 。 我认为这更通常称为“减少” - 无论如何,这就是Python的名字。 翻译你的例子: Fold[#1*#2&, 1, list] #1*#2&是一个二元lambda函数,它的参数相乘。 在这种情况下,您可以使用Times来代替: Fold[Times, 1, list] 当然,也可以将Times应用到列表中: Apply[Times, list] 或者,简而言之: Times @@ list 注意:您使用产品而不是Times的问题中的版本不起作用。 产品用于其他东西,即Sum的类比。 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)