开发者

Optimizing memory usage via appropriate data types

All,

I've been a Java developer for 10 years...when it comes to managing numerical data, for the most part I've stuck with int and used double when I need some decimal precision. The reason is we pretty much have a large heap space to work with and exhausting it is pretty difficult for the most part...

I'm starting iPhone development (I do have some C Programming knowledge) and I'm wondering if I should start looking at using appropriate data types given that the device itself has less resources than your typical JavaEE app...

For instance, I have an object with some numeric attribute (we'll call this attribute 'state'). This attribute will only ever have values from maybe -10 to 10. Originally, I defined this attribute using an int, but now I've decided to use char. The reason is I don't need that many bits to represent the values from -10 to 10...so char is the smallest I need. Unfortunately, when someone sees a char they may think character, but I开发者_StackOverflow中文版'm treating it as a number. I'm concerned that I'll probably introduce some confusion if I stick with char...

So given this code fragment:

-(void)doSomething:(char)value {
    state = value;
}

then invoking it such as:

[myObject doSomething:1];

My question is should I even be worried about optimizing the data types in this manner? Do I need to start checking for overflow and throw errors? Or is this perfectly good programming?

Thanks!

EDIT: I think I shouldn't have used the name state in my question. I don't particularly have an enumeration here as I perform calculations on this value and update the state as appropriate. It just happens to have a fixed range.


While this is perfectly acceptable, (its typically how I do it) unless there are more than one chars in a row, alignment issues will force the next variable to waste 1-3 bytes anyway, which could otherwise have been used for free by an int. I encourage you to investigate alignment, its rather straightforward but a little to verbose to explain + consequences here. At any rate, in order to avoid confusion (although I doubt there will be very much, but basically to each their own) you may want to use int8_t or which is typedef-ed to signed char anyway.

EDIT: Something I forgot before: the malloc implementation on MacOS (also the basis of iOS) only returns chunks of memory 16 bytes or longer, rounding up. So If you're object takes, say 15 bytes, you're also wasting a byte, and if it takes 17 bytes, you're wasting 15.


In cases like this I usually use a typedef enum containing all valid values:

typdef enum {
    kMyStateOne,
    kMyStateTwo
} MyStates;

and write the method like this:

-(void)doSomething:(MyStates)value {
    state = value;
}

so users of your code know to use:

[myObject doSomething:kStateOne];

This way you'll have the compiler complaining if they pass you anything else.


Unless you are going to be creating an awful lot of copies of this numeric attribute, space is not really an issue, even on the iPhone. Much more important are learning about memory management and the foundation data types (NSString, NSArray, NSNumber, etc.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜