0

Consider the following Objective-C code:

@interface ClassA : NSObject {

}
-(void) printVal;
@end


@implementation ClassA
-(void) printVal {
    int val;
    NSLog(@"%i", val);
}
@end


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    ClassA* cA = [[ClassA alloc] init];
    [cA printVal];
    [cA printVal];
    [cA printVal];

    [pool drain];
    return 0;
}

Why is this output:

2012-11-29 22:12:06.586 TestOne[20266:903] 0
2012-11-29 22:12:06.587 TestOne[20266:903] 32767
2012-11-29 22:12:06.588 TestOne[20266:903] 32767

In other words, why is val not reinitialized to 0 when it is redeclared, and why does it receive the value 32767 every subsequent time the method is called?

ericsoco
  • 24,913
  • 29
  • 97
  • 127
  • This is not an Objective-C question. It is a C question. Objective-C is C (a fact all too often forgotten)... Consider reading K&R; it's a great read. – matt Nov 30 '12 at 06:34
  • @matt, I figured it might be, but I'm even more unfamiliar with C than I am with Obj-C. Added the 'c' tag to the question. – ericsoco Nov 30 '12 at 06:35

4 Answers4

3

That is garbage value.

Initialise it with some value.

EDIT:

Storage class specifiers has some default value as

for auto- garbage value.

for static- 0.

for global/extern- 0;

for register-garbage.

In your case it is auto.

OHHH!!! I fotgot to answer your second part :(

Why initially it came as 0 and later on some 32767.

And you rightly pointed out in that link, now no need to explain in this answer. What can i do is refer to that link only.

How does an uninitiliazed variable get a garbage value?

Community
  • 1
  • 1
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Sorry. Didn't understand what you meant by garbage value, but on re-reading I think this answers the question. Do you have more detail on where you learned about garbage values? Just curious. – ericsoco Nov 30 '12 at 06:31
  • refer any c or c++ book, it is one of the important topic. You are often asked in interviews and source-code to solve and find the result kind of exams. – Anoop Vaidya Nov 30 '12 at 06:36
  • and, just found this: http://stackoverflow.com/questions/1422729/how-an-uninitialised-variable-gets-a-garbage-value however, i was hasty in accepting your answer -- why would calling the method a second time fill the uninitialized variable with a garbage value, while it contained 0 the first time the method was called? – ericsoco Nov 30 '12 at 06:45
1

In Objective-C, the only guarantee you have is that an int will be at least 32-bits wide. That may go to explain why it's not 0. As to why it's 32767, I haven't the foggiest, but 32768 is a power of 2, so that may pose a clue as to the answer.

hd1
  • 33,938
  • 5
  • 80
  • 91
  • I understand now that local variables must be initialized. So, beyond that, I suppose my question essentially falls into the realm of trivia. Still, it's very curious to me that the value would change simply because it was once accessed. – ericsoco Nov 30 '12 at 06:29
1

Local auto variables are not initialized unless you initialize them. They can't be assumed to have any particular value. Instance variables, static variables and global variables are initialized to 0, but not ordinary locals like this. You need to initialize it yourself before you intend to read its value

Chuck
  • 234,037
  • 30
  • 302
  • 389
0

Declaring a value on the stack does not initialize it. It simply allocates space for it. Whatever value happened to be in that memory is the value you will see printed. If you instead wrote:

-(void)printVal
{
    int val = 0;
    NSLog (@"%i", val);
}

then it should print 0 every time.

user1118321
  • 25,567
  • 4
  • 55
  • 86