V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
Livid
V2EX  ›  iDev

向各位请教一个代码问题

  •  
  •   Livid · 2010-07-25 06:44:29 +08:00 · 4688 次点击
    这是一个创建于 4996 天前的主题,其中的信息可能已经有所发展或是发生改变。


    在 Xcode 中新建一个 command line tool (Foundation),然后把上面的代码粘贴进去,然后运行。

    同样的逻辑如果是在 gcc -o hello hello.c 里的话是输出两个 5050,而在这里是 5050 10100。

    谁能解释一下为什么吗?
    11 条回复    1970-01-01 08:00:00 +08:00
    airwolf
        1
    airwolf  
       2010-07-25 07:01:38 +08:00
    int foo(int start, int end) {
    int result = nil;

    int i;

    int max = end + 1;

    for (i = start; i < max; i++) {
    result = result + i;
    }

    return result;
    }

    这样貌似就好了,虽然会有 warninig ...
    Livid
        2
    Livid  
    MOD
    OP
       2010-07-25 07:04:10 +08:00
    Objective-C 和 C 的函数变量的生命周期不一样么?

    如果把上面的代码用 C 写,只需要把 NSLog() 换成 printf(),然后得到的就是两个 5050。
    airwolf
        3
    airwolf  
       2010-07-25 07:14:51 +08:00
    嗯,或者说处理的方式不一样?

    感觉 Objc 有的地方很拖泥带水,必须手动清理先(虽然我不理解深入的,只是感觉)
    ashchan
        4
    ashchan  
       2010-07-25 09:22:35 +08:00 via iPhone
    foo 函数中的result未初始化,结果永远是不可预料的。gcc可能自动将它初始化为0了。

    这跟作用域没什么关系。

    解决方法是改为 int result = 0;
    Livid
        5
    Livid  
    MOD
    OP
       2010-07-25 09:32:35 +08:00
    @ashchan

    嗯,我知道让 result = 0 可以得到期望的结果,但是……

    http://gist.github.com/489173

    那么你再试试这个。

    看看 Hello 和 5050 输出的顺序,这也是我没法理解的。
    lizdo
        6
    lizdo  
       2010-07-25 11:09:26 +08:00
    我这边好像没啥不对的呀
    2010-07-25 11:05:49.221 Bug[5133:a0f] Hello
    2010-07-25 11:05:49.257 Bug[5133:a0f] Hello
    2010-07-25 11:05:49.257 Bug[5133:a0f] result1: 5050
    2010-07-25 11:05:49.258 Bug[5133:a0f] result2: 5050

    先Call的foo(), 所以先打的Hello嘛
    Livid
        7
    Livid  
    MOD
    OP
       2010-07-25 11:18:25 +08:00
    哦,抱歉。

    第二个是我晕了,确实没有任何问题。
    Livid
        8
    Livid  
    MOD
    OP
       2010-07-25 11:19:47 +08:00
    第一个:

    http://gist.github.com/489051

    在我的机器上运行的结果是:

    2010-07-25 11:19:36.250 HelloWorld[2906:a0f] result1: 5050
    2010-07-25 11:19:36.253 HelloWorld[2906:a0f] result2: 10100
    ashchan
        9
    ashchan  
       2010-07-25 12:25:25 +08:00
    在不同的机器上跑结果会不同的,@livid可以试试在Xcode 4里测一下,我这跟3下面不同。我还是认为未初始化变量是一种编程错误,不需要纠结会出来不正确的结果值吧?;-)
    huahang
        10
    huahang  
       2010-07-25 16:59:29 +08:00
    C和C++的Spec里面,都没有指定说int型的默认构造器会把值初始化为零。
    huahang
        11
    huahang  
       2010-07-25 16:59:59 +08:00
    所以哪怕第一个函数返回了5050,也是侥幸。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2863 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 11:32 · PVG 19:32 · LAX 04:32 · JFK 07:32
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.