Sunday, 15 May 2011

c - Linux Kernel: Static Const vs #Define -



c - Linux Kernel: Static Const vs #Define -

which more "appropriate" when writing linux kernel module: using static const define constant, or #define ?

i have kernel module related piece of hardware, , have typical constant that's number of buffers. rather hard-code "3" everywhere, want utilize constant. c style recommends taking static const, notice kernel chock total of #define's on place. there reason?

it used couldn't do:

const size_t buffer_size = 1024; unsigned char buffer[buffer_size];

in c, since buffer_size not "real" constant. hence see

#define buffer_size 1024 unsigned char buffer[buffer_size];

instead.

as of c99, can former, not in global scope. won't work outside of function (not if made static). since much code in kernel deals similiar constructs, might 1 reason using preprocessor instead.

note: don't forget sizeof, it's tool when comes not repeating size constant on place, regardless of how constant implemented.

c linux linux-kernel

No comments:

Post a Comment