GistTree.Com
Entertainment at it's peak. The news is by your side.

What is your favorite C programming trick?

0

As an instance, I not too long ago stumbled on this in the linux kernel:

/Force a compilation error if condition is precise */
#outline BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))

So, for your code, if you’re going to be wanting some building which might possibly merely restful be, inform a more than one among 8 bytes in size, perchance thanks to about a hardware constraints, chances are high you’ll build:

BUILD_BUG_ON((sizeof(struct mystruct) % 8) != 0);

and it would also merely not compile except the dimensions of struct mystruct is a more than one among 8, and if it is a more than one among 8, no runtime code is generated at all.

One more trick I know is from the book “Graphics Gemstones” which permits a single header file to every show and initialize variables in a single module while in other modules the voice of that module, merely show them as externs.

#ifdef DEFINE_MYHEADER_GLOBALS
#outline GLOBAL
#outline INIT(x, y) (x) = (y)
#else
#outline GLOBAL extern
#outline INIT(x, y)
#endif

GLOBAL int INIT(x, 0);
GLOBAL int somefunc(int a, int b);

With that, the code which defines x and somefunc does:

#outline DEFINE_MYHEADER_GLOBALS
#embody "the_above_header_file.h"

while code that’s merely the voice of x and somefunc() does:

#embody "the_above_header_file.h"

So that you just fetch one header file that declares every conditions of globals and plan prototypes the put they are mandatory, and the corresponding extern declarations.

So, what are your authorized C programming methods along these traces?

Read More

Leave A Reply

Your email address will not be published.