是否在堆栈中创建过Objective-C中的对象? [英] Are objects in Objective-C ever created on the stack?

查看:85
本文介绍了是否在堆栈中创建过Objective-C中的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我了解,在C ++中,您可以在堆栈上创建对象:

As far as I understand, in C++ you can create objects on the stack:

SomeClass object = SomeClass();

或在堆上:

SomeClass *object = new SomeClass();

在Objective-C中,您似乎总是在堆上创建对象,因为[SomeClass alloc]返回指向新实例的指针.这是正确的吗?

In Objective-C you always seem to create objects on the heap, as [SomeClass alloc] returns a pointer to a new instance. Is this correct?

是否在堆栈上分配了对象?如果是这样,那将是一个很好的例子?否则,为什么不呢?

Are objects ever allocated on the stack? If so, what would be a good example? Else, why not?

推荐答案

简短的答案是对象总是分配在堆上,而不是在堆栈上.

The short answer is that objects are always allocated on the heap, not on the stack.

虽然这还不是全部.在Objective-C中,块也是完整的Objective-C对象.它们的独特之处在于它们有时在堆栈中.特别是,使用块文字语法创建的块以及引用周围范围的块都在堆栈上.如果检查他们的班级,您将甚至可以看到此内容,该班级为(私人)NSStackBlock.如果使用Block_copy()-copy复制它们,则结果副本将位于堆(NSMallocBlock)上.

This isn't quite the whole story though. In Objective-C, blocks are also full Objective-C objects. They're peculiar in that they are sometimes on the stack. In particular, blocks created with the block literal syntax, and which reference surrounding scope, are on the stack. You can even see this if you inspect their class, which will be (the private) NSStackBlock. If you copy them, with Block_copy() or -copy, the resultant copy will be on the heap (NSMallocBlock).

一个暗示是,堆栈分配的块仅在创建它们的作用域结束之前才有效.在ARC(以及ARC的早期版本中的IIRC)之前,这意味着您必须复制要保留在其创建范围之外的块,以便将它们放到堆上.现在,在大多数情况下,ARC会为您处理此问题,这也意味着更难预测堆栈中是块还是堆中的块.

One implication of this is that stack-allocated blocks are only valid until the end of the scope in which they were created. Prior to ARC (and IIRC in early version of ARC as well), this meant that you had to copy blocks that you wanted to live past their creation scope so they'd be on the heap. ARC handles this for you in most cases now, and also means that whether a block is on the stack or the heap is harder to predict.

这个小型测试程序显示了这一点(使用ARC off 编译):

This small test program shows this (compile with ARC off):

#import <Foundation/Foundation.h>

// Compile this without ARC.

int main(int argc, char *argv[]) {
    @autoreleasepool {

        NSMutableString *string = [NSMutableString stringWithString:@"foo"];
        void(^stackBlock)() = ^{
            [string setString:@"bar"];
        };
        NSLog(@"stackBlock class: %@", NSStringFromClass([stackBlock class]));

        void(^heapBlock)() = [[stackBlock copy] autorelease];
        NSLog(@"heapBlock class: %@", NSStringFromClass([heapBlock class]));
    }
}

输出:

stackBlock class: __NSStackBlock__
heapBlock class: __NSMallocBlock__

(请清楚一点,您肯定不应该在真实代码中使用NSStackBlock/NSMallocBlock的检查.它们是私有实现详细信息类.此代码仅用于演示.)

(Just to be clear, you certainly shouldn't use a check for NSStackBlock/NSMallocBlock in real code. They're private implementation detail classes. This code is just for demonstration.)

这篇关于是否在堆栈中创建过Objective-C中的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆