xorl %eax, %eax

Boolean Type in C

leave a comment »

Today I was reading some code of a network protocol and I came across something which I’ve seen countless times even on applications that SHOULD be efficient. Anyone who has read ISO/IEC 9899:1999 knows that C provides a Boolean type. Every C99 compatible compiler must support this data type. Here is an example using GCC:

#include <stdio.h>

int
main(void)
{
    _Bool ret = 0;
    printf("Bool size: %u byte(s).\n", sizeof ret);
    return ret;
}

And on runtime using -std option to validate it against C99:

sh-3.1$ gcc bool.c -std=c99 -pedantic -o bool
sh-3.1$ ./bool
Bool size: 1 byte(s).
sh-3.1$

As you can see it’s just one byte long. Nevertheless, a few developers choose char type to store flags, I’ve seen numerous applications using integers to perform this or similar tasks which is awful! You waste resources for no reason! And this might not be an issue on Mozilla Firefox which has already memory leaks that can reach a few hundreds mega-bytes on a couple of hours of runtime, but when you see this inside kernel code or device drivers it sucks!!! Oh.. I forgot to mention… It was also static…. !^#%!&#^$!$!*(^$

Written by xorl

January 15, 2009 at 17:22

Posted in C programming

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s