TheWalkingCube
TheWalkingCube

Reputation: 2116

Memory allocation in header files

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 ?

Upvotes: 2

Views: 3043

Answers (2)

malaugh
malaugh

Reputation: 157

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.

Upvotes: 0

Fred Foo
Fred Foo

Reputation: 363567

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).

Upvotes: 4

Related Questions