对数组执行浅复制以调整数组大小有何影响?

发布于 2024-07-11 04:40:34 字数 243 浏览 3 评论 0原文

如果我对深复制和浅复制的理解是正确的,那么我的问题是不可能的。 如果您有一个数组 (a[10]) 并执行浅复制 (b[20]),这不是不可能的吗,因为 b 中的数据不连续?

如果我完全错了,有人可以建议一种快速的方法来模仿(在 C# 中)C++ 进行重新分配以调整数组大小的能力。

注意
我正在查看 System.Array 对象的 .Clone() 和 .Copy() 成员。

If my understanding of deep and shallow copying is correct my question is an impossible one.
If you have an array (a[10]) and perform a shallow copy (b[20]) wouldn't this be impossible as the data in b wouldn't be contiguous?

If i've got this completely wrong could someone advise a fast way to immitate (in c#) c++'s ability to do a realloc in order to resize an array.

NOTE
Im looking at the .Clone() and .Copy() members of the System.Array object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2024-07-18 04:40:34

您无法调整现有数组的大小,但是可以使用:

Array.Resize(ref arr, newSize);

这会分配一个新数组,将旧数组中的数据复制到新数组中,并更新 arr 变量(由-ref 在这种情况下)。 你是这个意思吗?

但是,仍然指向旧数组的任何其他引用将不会更新。 更好的选择可能是使用 List - 这样您就不需要手动调整它的大小,并且不会遇到过时引用的问题。 您只需添加/删除等。通常,您不会经常直接使用数组。 它们有它们的用途,但它们不是默认情况。


回复您的评论;

  • 装箱:List 不装箱。 这是关于泛型的要点之一; 在底层,ListT[] 的包装器,因此 List 有一个 int[ ] - 没有拳击。 旧的ArrayListobject[]的包装器,因此确实盒子; 当然,拳击
  • Array.Resize 的工作原理; 如果我记得的话,它会找到T的大小,然后使用Buffer.BlockCopy来blit内容实际的细节被内部调用隐藏- 但本质上在分配一个新数组之后,它是两个数组之间数据的 blit (memcpy),所以它应该很快; 请注意,对于引用类型,这仅复制引用,而不复制堆上的对象。 但是,如果您定期调整大小,List 通常会简单得多(并且更快,除非您基本上重新实现 List 重新实现备用容量的功能以尽量减少调整大小的次数)。

You can't resize an existing array, however, you can use:

Array.Resize(ref arr, newSize);

This allocates a new array, copies the data from the old array into the new array, and updates the arr variable (which is passed by-ref in this case). Is that what you mean?

However, any other references still pointing at the old array will not be updated. A better option might be to work with List<T> - then you don't need to resize it manually, and you don't have the issue of out-of-date references. You just Add/Remove etc. Generally, you don't tend to use arrays directly very often. They have their uses, but they aren't the default case.


Re your comments;

  • boxing: List<T> doesn't box. That is one of the points about generics; under the hood, List<T> is a wrapper around T[], so a List<int> has an int[] - no boxing. The older ArrayList is a wrapper around object[], so that does box; of course, boxing isn't as bad as you might assume anyway.
  • workings of Array.Resize; if I recall, it finds the size of T, then uses Buffer.BlockCopy to blit the contents the actual details are hidden by an internal call - but essentially after allocating a new array it is a blit (memcpy) of the data between the two arrays, so it should be pretty quick; note that for reference-types this only copies the reference, not the object on the heap. However, if you are resizing regularly, List<T> would usually be a lot simpler (and quicker unless you basically re-implement what List<T> does re spare capacity to minimise the number of resizes).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文