简体   繁体   中英

Is there a reason why someone would include stdlib.h twice?

I was studying the code for the Kilo text editor here:

https://github.com/antirez/kilo/blob/master/kilo.c

And I noticed that the includes defined stdlib.h twice (UPDATE: Comments are mine):

#include <termios.h>
#include <stdlib.h> // first time
#include <stdio.h> 
#include <errno.h>
#include <string.h>
#include <stdlib.h>  // second time
#include <ctype.h> 

Is this merely an error? Or is there something to it? I ask because the author of the code doesn't seem like someone who makes a lot of mistakes. I wouldn't want to suggest a change in ignorance.

As stdlib.h has an include guard, there is no point in including it twice. Probably the mistake was caused by merging two files, both dependant on stdlib.h.

There is no harm in including a standard header more than once, although it is totally unnecessary.

The C standard says the following about this:

Standard headers may be included in any order; each may be included more than once in a given scope, with no effect different from being included only once, except that the effect of including <assert.h> depends on the definition of NDEBUG .

There's no reason to include a particular header file twice. If the file has proper include guards, the second inclusion will have no effect. If it does not have include guards, you'll likely get a slew of errors for multiple definitions for typedefs, among others.

In the case of system headers, they almost always have include guards. The contents of stdlib.h might look something like this:

#ifndef _STDLIB_H
#define _STDLIB_H   1
...
// type definitions, #defines, declarations, etc.
...
#endif /* stdlib.h  */

The first time stdlib.h is included, the #ifndef will evaluate to true, _STDLIB_H is defined, and the remaining contents are inserted into the file being compiled. The second time stdlib.h is included, the #ifndef will evaluate to false since _STDLIB_H is defined and the contents of the file between the #ifndef and #endif will not be inserted again.

Most UNIX/Linux systems do this. In contrast, Microsoft is known for not managing its OS specific include files properly. If you included them in the wrong order you'll end up with lots of errors.

The only scenario it can make a difference is when one of the includes is undefining some symbols (including the include guards) from the previous includes. Consider 2 files:
1.h:

#define A 1

2.h :

#undef A

Now, the following sequence:

#include "1.h"
#include "2.h"

int B = A;

will produce an error, as A is undefined.

The following sequence will be just fine:

#include "1.h"
#include "2.h"
#include "1.h"

int B = A;

Now, if 1.h has the include guards:

1.h:

#ifndef GUARD_1
#define GUARD_1
#define A 1
#endif

The 2.h can do:

#undef GUARD_1
#undef A

and cause the same effect.

Now to stdlib.h . You can compose something like this in your xh header:

#undef _STDLIB_H   // Kill the include guard of stdlib.h
#undef NULL        // Undefine some important symbol from stdlib.h

Now, this sequence:

#include <stdlib.h>
#include "x.h"

will have NULL undefined

And this:

#include <stdlib.h>
#include "x.h"
#include <stdlib.h>

will have it defined.

Though not directly applicable to <stdlib.h> , a reason for including a user-defined header file twice: Testing if including the header file twice incurs a problem.

Example: Consider a pair of files foo.h and foo.c declaring and implementing a bunch of foo_ functions, defines, types, etc.

File foo.c

#include "foo.h"
#include "foo.h"

rest of foo.c code ...

A 2nd calling of an include file should not cause a problem and foo.c tests that.
OTOH, foo.c did not test if including a header file only once is OK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM