SyntaxC # ifndef to enable security in C ++

I am currently participating in the final exam for the CS course, and I have run into a minor (maybe big?) Issue regarding the C ++ #ifndef syntax.

I examined the syntax of C # infndef when using it as a #include protector, and most of the Internet seems to say:

#ifndef HEADER_H #define "header.h" ... #endif 

But my training course slides show examples like:

 #ifndef __HEADER_H__ #define "header.h" ... #endif 

I was wondering what (if any) is the difference between the two. The exam will most likely be asked by the #include guard, and I know that the usual wisdom is to just go with what the professor / teacher says, but if there is a difference in compilation time that I would like to know.

+7
source share
4 answers

A common practice is to not do this, and include the include header inside as it reduces repetition. eg:.

header.h

 #ifndef HEADER_H #define HEADER_H // Rest of header file contents go here #endif 

Exactly what you use as the macro name depends on your specific coding standard. However, there are various subtle rules in the C and C ++ standards that prevent you from using identifiers starting with underscores, 1 so you should avoid __HEADER_H__ to be safe.

It is also worth mentioning that you should choose something that is unlikely to run into anything else in your code base. For example, if you had a variable called HEADER_H elsewhere (unlikely, I understand), then you will get some crazy errors.


<sub> 1. See section 7.1.3 of C99.

+17
source

Names starting with double underscores are reserved for implementation, so I would suggest using __SOMETHING in your inclusion defenders. Also, try picking names that make conflicts unlikely. Thus, it seems that the textbooks of your class are mistaken in at least two points. See This Humorous Article , for example.

+5
source

It makes no difference if you do not use the underscore in variable names elsewhere, this is just a naming convention.

You just need to deliver something unique.

+1
source

The argument for including the included guards in a file that contains a header, and not in the header itself, is that if the file is already included, the compiler (in particular, the preprocessor) should not open and read the file again.

This is a weak argument. In practice, the time saved is trivial, and the probability of error is high.

In your example:

 #ifndef HEADER_H #include "header.h" ... #endif 

you are not showing us #define HEADER_H . Is it somewhere in header.h ? If so, how do you know that the author of header.h decided to use HEADER_H as the name of the included security macro? What if he changes to something else later?

If you decide to include the include security element in the include file, you must also define a macro:

 #ifndef HEADER_H #include "header.h" #define HEADER_H #endif 

But, as the other answers have already said, it is much better to put the guard in the header:

header.h:

 #ifndef HEADER_H #define HEADER_H /* contents of header.h */ #endif 

and then include just has:

 #include "header.h" 

and has less information to worry about.

0
source

All Articles