Re: global variables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 2002-11-24 at 05:50, sandeep dey wrote:

> just declare the global variables as extern in other files
> and use them.i.e declare them as     'extern int k;'
> (int k or anything you would like to declare)
> take care of double global varibles with same name
> and don't initialise an extern variable where it was  declared.i.e (don't
> use 'extern int k=0;')

This is a recipe for disaster in a large project (i.e. the kernel).

The best solution is to use externs, but do not just "declare the global
variables as extern in other files" as you will have no type checking.

Instead, in a header file associated with whatever you are doing,
declare the variable extern.  Use that in your other files where you
need to use the global.  Then (and this is the important part) also
include that same header where the variable is actually defined.  Then
you will get your type checking, since the extern declaration must match
the actual definition.

Look anywhere in the kernel for examples of this.

Note, perhaps even better, do not have the variables accessible
externally at all.  If needed, export functions to access them (i.e.
get_foo() and set_foo()).  The scheduler is a good example of where we
did this.

Whenever I see an extern plopped right above where it is used, I am
always suspicious.

	Robert Love

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux