C 数组与 Obj-C 数组

发布于 2024-10-20 12:37:02 字数 215 浏览 1 评论 0原文

在 Objective-C 中使用 alloc init 创建数组时,向数组添加对象时如何管理内存?

我想知道这与 C 有何关系,当您创建数组和 malloc 时,分配的大小需要是预期数组的大小,或者需要重新分配数组内存。

它们之间有何关系,或者了解 C 代码工作方式的好方法是什么。

添加对象时,objective-c 数组内存是否会在内部处理,或者这是如何工作的?

When an array is created in Objective-C using alloc init, how is the memory managed when objects are added to the array?

I would like to know how this relates to C when you create an array and malloc, the allocated size needs to be the size of the expected array or the array memory needs to be reallocated.

How do these relate or what is a good way to understand the way the C code works.

Does the objective-c arrays memory get handled internally when objects are added or how does this work?

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

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

发布评论

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

评论(2

月下伊人醉 2024-10-27 12:37:02

Objective-C 数组遵循与其他 Objective-C 对象相同的内存管理规则。如果分配了它,则需要释放它。

C 数组和 Objective-C 数组在概念上相似,但实现方式却截然不同。 AC 数组是一个连续的内存块,除了控制如何使用它的语言结构之外,几乎没有什么其他的。 Objective-C 数组是具有重要内置功能的对象。它们动态调整自身大小(如果它们是可变的)以适应添加的元素。他们正确地保留和释放他们存储的对象。它们可以自行排序、过滤、插入对象、删除对象等。您不应该对它们的实现方式做出任何假设。

Apple 的文档应该可以让您更好地了解 Objective-C 数组的功能(当您阅读它时,也可以看看其他集合类)。从这里开始阅读:
集合编程主题

An Objective-C array follows the same memory management rules as other Objective-C objects. If you allocate it, you'll need to release it.

C arrays and Objective-C arrays are similar in concept, but implemented quite differently. A C array is a contiguous block of memory with very little other than a language construct governing how you use it. Objective-C arrays are objects with significant built-in functionality. They dynamically resize themselves (if they're mutable) to accomodate added elements. They properly retain and release the objects that they store. They can sort themselves, filter themselves, insert objects, delete objects, etc. You should make no assumptions about how they're implemented.

Apple's documentation should give you a much better idea of what's possible with Objective-C arrays (and while you're at it, look at the other collection classes too). Start reading here:
Collections Programming Topics

蓝颜夕 2024-10-27 12:37:02

NSArrays(以及扩展的 NSMutableArrays)保留添加到其中的对象,并在数组本身被释放时向它们发送所有释放消息。

这样做的结果是,常见的模式是分配一个对象,初始化它,将它交给数组,然后释放它。由于数组会为自己保留它,因此它会随着数组本身的存在而持续存在,或者直到您指示数组删除它为止。您永远不需要向它发送另一个释放消息,数组机制会处理这个问题。

我猜想可变数组是作为类似链接列表的东西来实现的,因此它们可以在以后轻松扩展和收缩。它只是一个指针列表,也许还有一些元数据。因为它不会创建任何交给它的对象,只是给它们施加自己的束缚,所以它需要做的就是为指向对象的指针提供一个位置。

NSArrays (And by extension NSMutableArrays) retain the objects added to them, and send them all release messages when the array itself is deallocated.

The upshot of this is that a common pattern is to alloc an object, initialize it, hand it to the array, and then release it. Since the array retains it for itself, it'll last as long as the array itself does, or until you instruct the array to get rid of it. You will never need to send it another release message, the array machinery takes care of that.

I'd guess that the Mutable arrays are implemented as something like a linked list, so they can be easily expanded and contracted later. It's just a list of pointers, and maybe a little metadata. Since it doesn't create any objects handed to it, just puts it's own leash on them, all it needs to do it have a place for the pointer to the object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文