参考变量是否占用内存?(Does reference variable occupy memory? [duplicate])
这个问题在这里已经有了答案:
- 为什么参考大小总是4个字节 - c ++ 2个答案
我读过参考变量与原始变量共享相同的内存地址 ,但也占用了堆栈上的一些空间。 并且,由于引用具有与原始变量相同的内存地址,因此它也被称为别名。
所以,我的问题是内存分配如何完成参考变量 ?
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
最满意答案
回答你的第一个问题:
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中的测试?(Documentation as tests in Ruby?)[2022-04-02]
好吧,在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 ... -
Ruby数组注入(Ruby array inject)[2022-07-31]
在这种情况下,您需要为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异常的文档(Documentation for Ruby exceptions)[2024-04-28]
无法保证Ruby中会引发哪个异常。 用户可能发送中断,您的文件系统可能无法写入等。而Ruby不保证这一点。 但是对于File操作,我可能会看一下IOError及其子EOFError。 这可以提供处理非例外(合理预期)事件的能力,例如文件不存在,或者没有读访问权限或没有写访问权限,这些事项可以编程,救援和尝试处理。 您还可以编写自己的异常,引发自定义异常(可能继承StandardError)并提供相应的操作或反馈。 这是我现在使用Ruby 1.9.3-p327从我的计算机生成的当前异常层次结构 BasicOb ... -
#inject和slowness(#inject and slowness)[2022-10-27]
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] ...
-
Ruby注入奇怪的行为(Ruby Inject Weird Behavior)[2023-09-07]
它是内置的,它是懒惰的: 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| ... -
为Ruby生成文档(Generating documentation for Ruby)[2022-08-07]
我使用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的类比。 ...