[Objective-c] Primitive type initialization.

January 9, 2016

Last week, I have met a very interested case, I don’t know that have you guys met this before, here is it:

#import <Foundation/Foundation.h>
int main() {
NSLog(@"aBOOL %hhd", aBOOL);

return 0;

Could you get the NSLog result? In this case, it will show 95.

It’s pretty weird result with some people. I was in that case. And after a while researching, that problem is not that simple. Further more, it drove me to a concept named Primitive type initialization – the one I’m gonna illustrate in this post with my little-stupid brain in coding (that’s true). T.T

As we all know, BOOL is a primitive type of Objective-C (for more further of BOOL, you also can take a look at this valuable link). But first, let’s take a bit look at Object intitialization.

Object initialization

Since Objetive-C always initial object with pointer, that mean you don’t need to worry about initial value for an object. Really? Yes! We are all doing like this, for example, with NSString:

`NSString *string = [[NSString alloc] init];`

What alloc did in the line above is allocate a chunk of memory to hold the object, then clear the memory, that means set it to zero or nil, this step will sure it will not be set by Garbage value. Thus, these values are guaranteed after the alloc method is called.

Question is: What is Garbage value?

The Garbage value is not assigned, rather the value is already there. When you allocate a variable you are reserving a piece of memory - until you overwrite it that memory will contain whatever “random” information was there before.

Next, init means you have an action to initialize precisely to avoid bugs in development, and finally return an object of type id – the same object is being initialized.

So that means, all the object is safely initialization. Apple just did a good job let’s developers not take much time care about object initialization. So Object will be ignored from this post. All we are talking about is just Primitive type.

Additional: You recommendly should not use [SomeObject new]; instead of [[SomeObject alloc] init];, because new doesn’t support custom initializers, like initWithString

Primitive type initialization

We have 3 cases here with global, static and local variables, see example:

int globalVar;
static int staticVar;

int main() {
int localVar;

printf("Global var : %i\n", globalVar);
Static var: %i\n", staticVar);
Local var :%i\n", localVar);

return 0;

And result will be:

Global var : 0
Static var: 0
Local var :1606422582

So we can easily realize that local variables without initialization seem like unpredictable. Explaination for this, static and global variables are initialized at compile-time, since their address is known and fixed. Initializing them to 0 as default will not incur a runtime cost. Other side, automatic variables are stored in stack mean they don’t have fixed locations, they can have different addresses for different calls and would have to be initialized at runtime each time the function is called, will incur a runtime cost. If you don’t initialize them, they will be set to Garbage values that I mentioned above.

Apply for this case: The localVar has value like 1606422582 because it has been taking a chunk of memory, which is maybe already store an value that has been there before, so localVar has that value. Clear enough!

So what are we supposed to do?

Easy! With Primitive type variables, we just only need to initialize them to avoid exceptions, especially for local ones. This will make your code is clearer and under-control.

So hope this post could help you more in understanding about Primitive type initializations not only for Objective-C but also for more languages in case you meet this problem. Please leave comments if you have unsure-thoughts or I wrote some mistakes.

Cheers and happy coding, and please share if you get this valuable.