473,385 Members | 1,387 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

C directives

vib
Hi there,

I have seen code which use #ifdef-#else-#endif directive to make sure
not duplicate declaration of variables. From the code below, I could
understand the reason behind of using the
#ifndef __KERNEL_H_
#define __KERNEL_H_.

However, as for the #ifdef __KERNEL_DEF
#undef __KERNEL_DEF
TASK taskreg[2];
#else
extern TASK taskreg[2];
#endif
, isn't that unnecesssary? As the first, one can write it without it,
but with a little more planning. and secondly I don't see many using
this construct.

Perhaps I am ignorant about the benefit of that construct. Appreciate
your comments.

Thanks
vib

-----
#ifndef __KERNEL_H_
#define __KERNEL_H_

typedef struct task_reg_type
{
int stack;
int taskreq;
} TASK;
#ifdef __KERNEL_DEF
#undef __KERNEL_DEF
TASK taskreg[2];
#else
extern TASK taskreg[2];
#endif

#endif
------

Nov 15 '05 #1
3 1512
"vib" <vi*****@gmail.com> wrote:
I have seen code which use #ifdef-#else-#endif directive to make sure
not duplicate declaration of variables. From the code below, I could
understand the reason behind of using the
#ifndef __KERNEL_H_
#define __KERNEL_H_.

However, as for the #ifdef __KERNEL_DEF
#undef __KERNEL_DEF
TASK taskreg[2];
#else
extern TASK taskreg[2];
#endif
, isn't that unnecesssary?


It's worse: it's probably broken. It looks like an attempt to prevent
duplicate definition of taskreg, by turning subsequent definitions into
declarations instead, which would be legal. Unfortunately, this helps
only if the second definition is inside the same translation unit as the
first - it has no effect at all if this file is #included twice in the
same program, but in different source files. In the latter case, you
still have two definitions for the same object, and still cause
undefined behaviour by doing so.

BTW, identifiers starting with two underscores are reserved for the
implementation. If this file was provided by your implementation, that's
all right, but in your own headers you're not supposed to use names like
__KERNEL_H_, or in fact __anythingatall.

Richard
Nov 15 '05 #2
Perhaps the point is that you can have a file which handles all of your
defines, just by including the right files and doing

#define __KERNEL_DEF

and including the right files. This way when someone adds a variable
definition, they only have to add it into a single header file, rather
than also having to add it to a regular source file.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017
Nov 15 '05 #3
Jonathan Bartlett wrote:
Perhaps the point is that you can have a file which handles all of your
defines, just by including the right files and doing

#define __KERNEL_DEF

and including the right files. This way when someone adds a variable
definition, they only have to add it into a single header file, rather
than also having to add it to a regular source file.


But then you'd have to make sure to only include the header file in one
particular source file, so you could just as well define the variables
there.
Christian
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dave | last post by:
Hello all, In standard C++, is it required that preprocessing directives (#include, #if, etc...) start in column 0, or is that just a C thing? Thank, Dave
24
by: Nalla | last post by:
Hi, I want a program. It should be a command line one. you can input the path of a folder(preferably) or a file...it should count the no. of lines between the compiler directives, ifdef win32 and...
13
by: seemanta dutta | last post by:
Greetings C gurus, I have used preprocessor directives since a very long time. But whenever I see some professional piece of C code, the linux kernel for example, I get literally confused by the...
1
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many...
5
by: cody | last post by:
the following leads to an error (note that TEST is not defined): #if TEST string s = @" #"; // <-- the error is "preprocessing directive expected" #endif also, here we get the same error: ...
13
by: rincewind | last post by:
I remember reading an article (was it Herb Sutter's?) that recommended avoiding using directives. While I quite understand this recommendation for headers, what's wrong in using directive in .cpp...
1
by: bcr07548 | last post by:
I am writing a C++ wrapper class for a library that was available in C but am having some trouble because, at some point, one function was replaced by a similar one. I would like to have it so my...
21
by: Bogdan | last post by:
Can anyone recommend a program for indentation of C preprocessor directives. My file looks like this: #ifdef a #define b #else #define c #endif int main() {
76
by: jacob navia | last post by:
Since standard C doesn't provide any way for the programmer to direct the compiler as to how to layout structures, most compilers provide some way to do this, albeit in different forms. ...
14
by: lagman | last post by:
All, I'm looking for a tool that is able to take my code base (which is full of preprocessor directives) and spit out source code that is "clean" of any preprocessor directives based on the...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.