1

I am struggling with bug in my program. and finally i got the point. here integer shows value 1 at the time of declaration. I clean and build again. but it shows 1 value?

please any one explain me why this happen?

enter image description here

QueueOverFlow
  • 1,336
  • 5
  • 27
  • 48
  • 3
    Say: NSLog(@"display value: %i", numberOfRecords); right after the declaration, this will be the real value, it might be some trash from the memory. – John Smith Aug 11 '12 at 11:32

1 Answers1

7

When you declare a local variable without specifying a value, you need to assign it first before reading from it becomes valid. The 1 that you see in your integer variable could be any garbage value, it is unspecified. Reading this value is undefined behavior.

int numberOfRecords = 0;

This is different from instance variables, which are initialized by default.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • 4
    Wanted to add this for further clarification - If its local variable, there could be any garbage value. If its class instance variable, int will be initialized to 0 for you. – 0x8badf00d Aug 11 '12 at 11:36
  • 2
    @0x8badf00d Thanks for an excellent comment, I modified the answer to mention this. – Sergey Kalinichenko Aug 11 '12 at 11:41