首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@autorelease and Loops (for,while,do)语法

@autorelease and Loops (for,while,do)语法
EN

Stack Overflow用户
提问于 2013-08-26 12:44:16
回答 4查看 629关注 0票数 4

clang允许以下循环语法:

代码语言:javascript
复制
for (...) @autorelease { ... }

while (...) @autorelease { ... }

do @autorelease { ... } while (...);

到目前为止,我还没有找到任何关于该语法的文档(苹果在指南中没有使用这种语法,至少在介绍@autorelease构造的指南中没有),但假设上面的三条语句相当于下面的三条语句是否合理:

代码语言:javascript
复制
for (...) { @autorelease { ... } }

while (...) { @autorelease { ... } }

do { @autorelease { ... } } while (...);

因为这正是我所期望的(按照标准C语法规则),但我不完全确定是否真的是这样。它也可能是一些“特殊的语法”,其中的自动释放池并不是每次循环迭代都更新的。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-08-26 13:04:55

这两种语法是相同的。

代码语言:javascript
复制
-(void)aFunc
{
    int i=0;
    for(;i<5;)
        @autoreleasepool {
            ++i;
        }
}
-(void)bFunc
{
    int i=0;
    for(;i<5;)
    {
        @autoreleasepool {
            ++i;
        }
    }
}

装配代码

代码语言:javascript
复制
 "-[AppDelegate aFunc]":                 ## @"\01-[AppDelegate aFunc]"
    .cfi_startproc
Lfunc_begin0:
    .loc    1 12 0                  ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:12:0
## BB#0:
    pushq   %rbp
Ltmp2:
    .cfi_def_cfa_offset 16
Ltmp3:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp4:
    .cfi_def_cfa_register %rbp
    subq    $32, %rsp
    movq    %rdi, -8(%rbp)
    movq    %rsi, -16(%rbp)
    .loc    1 14 12 prologue_end    ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:14:12
Ltmp5:
    movl    $0, -20(%rbp)
LBB0_1:                                 ## =>This Inner Loop Header: Depth=1
    .loc    1 15 5                  ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:15:5
Ltmp6:
    cmpl    $5, -20(%rbp)
    jge LBB0_3
## BB#2:                                ##   in Loop: Header=BB0_1 Depth=1
    .loc    1 16 26                 ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:16:26
Ltmp7:
    callq   _objc_autoreleasePoolPush
    .loc    1 17 13                 ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:17:13
    movl    -20(%rbp), %ecx
    addl    $1, %ecx
    movl    %ecx, -20(%rbp)
    .loc    1 18 9                  ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:18:9
    movq    %rax, %rdi
    callq   _objc_autoreleasePoolPop
    jmp LBB0_1
Ltmp8:
LBB0_3:
    .loc    1 19 1                  ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:19:1
    addq    $32, %rsp
    popq    %rbp
    ret
Ltmp9:
Lfunc_end0:
    .file   2 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObject.h"
    .file   3 "/Users/Parag/Desktop/Test/Test/AppDelegate.h"
    .cfi_endproc

    .align  4, 0x90
"-[AppDelegate bFunc]":                 ## @"\01-[AppDelegate bFunc]"
    .cfi_startproc
Lfunc_begin1:
    .loc    1 20 0                  ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:20:0
## BB#0:
    pushq   %rbp
Ltmp12:
    .cfi_def_cfa_offset 16
Ltmp13:
    .cfi_offset %rbp, -16
    movq    %rsp, %rbp
Ltmp14:
    .cfi_def_cfa_register %rbp
    subq    $32, %rsp
    movq    %rdi, -8(%rbp)
    movq    %rsi, -16(%rbp)
    .loc    1 22 12 prologue_end    ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:22:12
Ltmp15:
    movl    $0, -20(%rbp)
LBB1_1:                                 ## =>This Inner Loop Header: Depth=1
    .loc    1 23 5                  ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:23:5
Ltmp16:
    cmpl    $5, -20(%rbp)
    jge LBB1_3
## BB#2:                                ##   in Loop: Header=BB1_1 Depth=1
    .loc    1 25 26                 ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:25:26
Ltmp17:
    callq   _objc_autoreleasePoolPush
    .loc    1 26 14                 ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:26:14
    movl    -20(%rbp), %ecx
    addl    $1, %ecx
    movl    %ecx, -20(%rbp)
    .loc    1 27 9                  ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:27:9
    movq    %rax, %rdi
    callq   _objc_autoreleasePoolPop
Ltmp18:
    .loc    1 28 5                  ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:28:5
    jmp LBB1_1
Ltmp19:
LBB1_3:
    .loc    1 29 1                  ## /Users/Parag/Desktop/Test/Test/AppDelegate.m:29:1
    addq    $32, %rsp
    popq    %rbp
    ret
Ltmp20:
Lfunc_end1:
票数 7
EN

Stack Overflow用户

发布于 2013-08-26 12:57:31

当您认为任何条件语句都可以省略{ ... }块时,第一个语法示例工作的原因是明确的,只会执行以下语句。

例如:

代码语言:javascript
复制
if (something == YES)
    NSLog(@"Something is yes");

等于

代码语言:javascript
复制
if (something == YES)
{
    NSLog(@"Something is yes");
}

@autoreleasepool { ... }块只是条件后面的下一个语句。

就我个人而言,我使用第二种语法,因为它在进行更改时不太容易出错,而且我发现它更容易阅读。假设在条件块和@autoreleasepool { ... }块之间添加语句时,结果与原来的结果有很大不同。看看这个天真的例子..。

代码语言:javascript
复制
int i = 1;
while (i <= 10)
    @autoreleasepool
    {
        NSLog(@"Iteration %d", i);
        ++i;
    }

将输出“迭代1”到“迭代10”。然而:

代码语言:javascript
复制
int i = 1;
int total = 0;
while (i <= 10)
    total += i;
    @autoreleasepool
    {
        NSLog(@"Iteration %d", i);
        ++i;
    }

实际上将导致无限循环,因为++i语句从未达到,因为它在语法上相当于:

代码语言:javascript
复制
int i = 1;
int total = 0;
while (i <= 10)
{
    total += i;
}

@autoreleasepool
{
    NSLog(@"Iteration %d", i);
    ++i;
}
票数 8
EN

Stack Overflow用户

发布于 2013-08-26 12:59:34

我尝试了以下代码:

代码语言:javascript
复制
@interface Foo : NSObject
@end

@implementation Foo

- (void) dealloc
{
    NSLog(@"Deallocating %@.", self);
    [super dealloc];
}

@end

for (;;) @autoreleasepool {
    [[[Foo alloc] init] autorelease];
    sleep(1);
}

控制台开始填充已释放的Foo实例,因此语法似乎与预期的一样工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18444432

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档