Why is the compiler caching my "random" and NULLED variables?

Posted by alex gray on Stack Overflow See other posts from Stack Overflow or by alex gray
Published on 2012-04-12T23:25:52Z Indexed on 2012/04/12 23:28 UTC
Read the original article Hit count: 244

Filed under:
|
|
|
|

I am confounded by the fact that even using different programs (on the same machine) to run /compile, and after nilling the vaues (before and after) the function.. that NO MATTER WHAT.. I'll keep getting the SAME "random" numbers… each and every time I run it. I swear this is NOT how it's supposed to work.. I'm going to illustrate as simply as is possible…

#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {

    int rPrimitive = 0;  rPrimitive = 1 + rand() % 50;
    NSNumber *rObject = nil; rObject = [NSNumber numberWithInt:rand() % 10];
    NSLog(@"%i  %@", rPrimitive, rObject);

    rPrimitive = 0;   rObject = nil;
    NSLog(@"%i  %@", rPrimitive, rObject);
    return 0;           
}

Run it in TextMate:

i686-apple-darwin11-llvm-gcc-4.2
8  9
0  (null)

Run it in CodeRunner:

i686-apple-darwin11-llvm-gcc-4.2
8  9
0  (null)

Run it a million times, if you'd like. You can gues what it will always be. Why does this happen? Why oh why is this "how it is"?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about c