سؤال

The company I'm working for have development rules for C development on embedded target. One is :

It is recommended to not allocate any storage space in the header files.

I'm not sure what it means, the person who wrote it is not around and the other developers don't really care, so I am asking here.

What I understand is that I shouldn't declare variables in a header files, so something like that would be discouraged in a .h :

   int myVar;
   static char myOtherVar;

What I don't understand is what's wrong with that ? Why shouldn't I do it ?

هل كانت مفيدة؟

المحلول

What is wrong is that external variables get doubly defined, while static ones get defined for each module that includes the header, wasting space (unless they get optimized away).

نصائح أخرى

You should declare the variable in the C file, and use

extern int myVar;

in the header file, or better still, write an accessor function.

having static char myOtherVar; in the header makes no sense, since the static means it only accessible within the file where it is declared.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top