关于CGPoint的内存管理

发布于 2024-09-24 03:19:57 字数 355 浏览 7 评论 0原文

参考需要CGPointMake解释吗?

如果我错了请纠正我,在CGPointMake<的实现中/code>, CGPoint p; 声明一个 struct 的局部变量,在离开作用域后应将其释放。但为什么函数可以毫无风险地返回值呢?

不管怎样,假设CGPointMake的实现是正确的,我应该释放由CGPointMake创建的CGPoint吗?

refer to CGPointMake explaination needed?

Correct me if I am wrong, in the implementation of CGPointMake, CGPoint p; declare a local variable of a struct, which should should be freed after leaving the scope. But why the function can return the value without risk?

Anyway, assume that implementation of CGPointMake is correct, should I free the CGPoint that created by CGPointMake?

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

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

发布评论

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

评论(2

寄居者 2024-10-01 03:19:57

它不需要被释放,因为它从来不存在于堆上。仅需要释放堆分配的内存。在方法/函数存在后,在堆栈上分配的内存(如CGPointMake()中所做的那样)将被自动清理。

该函数可以返回一个点,因为编译器会看到“啊哈,这个函数想要返回一个结构,它的大小是 sizeof(CGPoint) 字节,所以我将确保返回中有足够的空间对于这么大的东西来说,内存插槽很有价值。”然后,当函数退出时,返回值被复制到返回内存槽中,函数退出,返回槽中的值被复制到新的目的地。

It doesn't need to be freed, because it never lived on the heap. Only heap-allocated memory needs to be freed. Memory that is allocated on the stack (as is done in CGPointMake()) will be cleaned up automatically after the method/function exists.

The function can return a point because the compiler sees "Aha, this function wants to return a struct, which is sizeof(CGPoint) bytes big, so I'll make sure that there's enough space in the return value memory slot for something that big." Then as the function exits, the return value is copied in to the return memory slot, the function exits, and the value in the return slot is copied over to its new destination.

千笙结 2024-10-01 03:19:57

该函数可以返回一个CGPoint,因为该结构“足够小”,可以直接从函数返回。您没有返回指针,而是直接返回整个内容。与采用 CGPoints 作为参数的方法相同 - 您可以直接按值传递整个内容。

正如 Dave 所说,CGPoint 不是对象,它们只是结构。 CGPointMake 不会“分配”内存。它只是一个返回具有正确大小的结构设置的函数,然后您通常将其捕获到您自己的堆栈上的本地或传递或其他什么。

与任何其他基本类型(int、float 或其他结构)一样,它在超出范围时不需要释放。

(注意:许多体系结构/编译器/“应用程序二进制接口”对于用作参数或返回值的事物的大小都有优化和大小限制。在这种情况下,CGPoint 实际上可以完全适合一个 64 位寄存器( 2x 32 位浮点数),这使得它并不比返回 int 更重量级,但是编译器还可以使用其他技巧来复制更大的结构,例如 CGRect。)

The function can return a CGPoint because the struct is "small enough" to be returned directly from a function. You're not returning a pointer, you're returning the whole thing directly. Same with methods that take CGPoints as parameters-- you can pass the whole thing by value directly.

As Dave notes, CGPoints aren't objects, they're just structs. CGPointMake doesn't "allocate" memory. It's just a function that returns a struct set up with the right sizes, which you then usually capture into a local on your own stack or pass along or whatever.

Like any other primitive type (int, float, or other struct), it doesn't need to be freed when it goes out of scope.

(Note: many architectures/compilers/"application binary interface"s have optimizations and size limits regarding the size of a thing used as an argument or return value. In this case, a CGPoint could actually fit entirely inside one 64-bit register (2x 32-bit floats) which makes it no more heavyweight than returning an int. But the compiler can also do other tricks as far as copying in and out larger structures e.g. CGRect.)

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