开发者

Effect of including a header file in C

What is the effect of including a header file, none of the functions declared in which, are used in source file ? Does it affect stack size e开发者_Go百科tc ?


It will have no effect, but it will increase compilation time and make the code harder to understand and maintain. You should only include the headers you actually need, and remove those which become redundant.


That depends on whether there are definitions in the header file or just declarations.

It also depends entirely on the implementation since the ISO C standard has nothing to say about how things are done at that level. It only covers how things appear to behave at a "C virtual machine" level. But I'll cover the most likely scenario here.

Definitions such as int xyzzy; (or, worse, char big_honkin_thing[9999999];) may take up space in the object file and, unless you have a particularly clever linker, the executable as well. I say "may" since this is dependent on the implementation.

Initialising the value is more likely to ensure it's stored in the object rather than created at runtime. But you're likely to find an effect regardless of that, either larger object/executable files if it's created at compile time or (mildly) slower startup times as more memory has to be zero-initialised.

For example, adding char big[99999] = {'x'}; to a header file results in the size of the executable going from 18K to 118K.

Simple declaration stuff like typedef and extern will not allocate space in the object in and of themselves.

In addition, even without definitions, the compile time will be increased since the compiler has to process that header file. But that will not have any impact on runtime (either speed or storage) itself.


The stack size is determined by your linker. Presumably you actually mean whether or not the emitted code is larger or not.

Including a header file whose declarations are never referenced in that translation unit will not affect the size of the generated objects. Of course, it will slow down compilation.


It will increase compile times, but AFAIK there shouldn't be any other changes.


Directly accessing the source file compare to accessing source file with header file will take less time if in the case there is nothing in header which can effect the source file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜