Igor Benko
Igor Benko

Reputation: 1276

CGFloat has weird default value

I just got confronted with very weird bug, which happened probably due to lack of my knowledge on Objective C data types. If I do this:

CGFloat newY;
NSLog(@"newY is %f", newY);

Log returns 0.0, although I would expect nil, but that's not the real problem. If I now export this app to iOS .ipa and install the app from there the newY gets automatically initialized to 185.000. Where does this value come from and why it is different when the app is installed from .ipa and not directly from XCode?

I would really appreciate any insight on what's happening, it doesn't make any sense to me.

Upvotes: 3

Views: 3245

Answers (2)

Oyashiro
Oyashiro

Reputation: 505

CGFloat is not a pointer, so it can't be assign nil.

This is an automatic variable, so it is given a place in memory, and it takes the value which is already stored as you don't initialize it. The value is undefined, so you should initialize it.

Upvotes: 1

Aderstedt
Aderstedt

Reputation: 6518

newY is not initialized to zero, it is whatever happens to be on the stack (which is where newY is stored) at the time. This may be different on different platforms but may also change between executions.

You are most likely getting a warning telling you that it is unsafe to use newY before initializing it yourself.

Upvotes: 4

Related Questions