Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: C++
Expertise: All
Jun 2, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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.

Danny Kalev
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date