2

In my method, I declare some variables, including int blockCount;. I call this method more than once. Using the Xcode debugger, I found out that after the second time the method was called, the value of blockCount was set to 364265, while it was set to 2, just a few milliseconds earlier.

It's not a real problem, since I can just set it to 0 or any other number I'd like, but is it bad programming habit to have a certain variable declared over and over again? I'm quite new to programming, and I want to make sure I'm doing things the right way. :)

Tim Vermeulen
  • 12,352
  • 9
  • 44
  • 63

1 Answers1

7

If you declare a variable but don't provide a value for it, it is considered "uninitialized". An uninitialized variable in C has an "undefined" value -- it's usually garbage, containing whatever happened to be at that address the last time something was written there. Strictly speaking, though, "undefined" means that you should under no circumstances try to use that value. (If you do a search for "nasal demons" this will be explained in quite colorful, and also useful, terms.*)

This variable, being local, is recreated every time the method runs, and thus gets a new actual, though still technically undefined value each pass.

It's generally recommended to not leave variables uninitialized, because the "random" value can cause bugs that are hard to find, and occasionally summon the aforementioned nasal demons. You're not doing anything wrong, but if you're not setting the actual value within a line or two of the declaration, I'd suggest initializing it to 0 or some sensible default:

int blockCount = 0;

*See also: What happens to a declared, uninitialized variable in C? Does it have a value?

Community
  • 1
  • 1
jscs
  • 63,694
  • 13
  • 151
  • 195
  • I always thought variables were already initialized to 0 by convention, but apparently not. Thanks for explaining! – Tim Vermeulen Mar 15 '12 at 19:51
  • 3
    Only `static` variables, and, under ARC, object pointers, not primitives like `int`. Glad I could help! – jscs Mar 15 '12 at 19:51
  • 2
    @timjver I wanted to add as a note, since you say you're new to programming, one of the reasons why this is the way it is. Initializing to 0 takes a small amount of CPU time to do. By not initializing, it is a slight performance gain. After all, you very well may want to start with a different value than 0. Initializing to 0 is a step you can skip. – Jordan Mack Mar 15 '12 at 19:59
  • @JMack Sounds fair, but why wouldn't it be skipped with i.e. static variables? – Tim Vermeulen Mar 15 '12 at 20:17
  • 1
    @tim: For two reasons, both springing from the fact that static variables live for the life of the program. First, they are only initialized once, meaning the (already small) performance hit of setting to 0 happens only once, at start-up. Second, because you frequently use static variables to hold data that's expensive to create but which doesn't need to change. In order to be sure you only do that creation once, the variable needs to initially have a known value that you can check for. E.g., `void f(void){ static void * expensive_dat; if( expensive_dat == NULL ){ // initialize } // use dat` – jscs Mar 15 '12 at 20:34
  • @IuliusCæsar So that's what that was for, I always wondered that. Thanks for explaining :) – Tim Vermeulen Mar 16 '12 at 15:22