devxlogo

Reducing A Class’s Size

Reducing A Class’s Size

Many programmers still use the non-standard, platform dependent BOOL typedef instead of using bool. There are many good reasons why you shouldn’t use BOOL; one of them has to do with the size of this type. Unlike bool, which occupies one byte on most platforms, BOOL occupies four bytes. Thus, BOOL causes am unnecessary increase in the size of your classes and structs. Consider the following example:

 class Bloated{ BOOL a; BOOL b; BOOL c; BOOL d;};class Slim{ bool a; bool b; bool c; bool d;};

Class Bloated occupies 16 bytes in memory. Slim, on the other hand, occupies only four bytes.

devx-admin

Share the Post: