Re: keyword extern
From: Emmanuel Delahaye (emdelYOURBRA_at_noos.fr)
Date: 06/26/04
- Next message: markus: "Usual way to hide jpegs. Extraction off application jpgs??"
- Previous message: Scott Moore: "Re: ascii VS binary files"
- In reply to: umbs.sairam_at_gmail.com: "Re: keyword extern"
- Next in thread: umbs.sairam_at_gmail.com: "Re: keyword extern"
- Reply: umbs.sairam_at_gmail.com: "Re: keyword extern"
- Reply: Minti: "Re: keyword extern"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 26 Jun 2004 09:29:44 GMT
In 'comp.lang.c', umbs.sairam@gmail.com wrote:
> One of the purposes of using extern in multiple files is restricting
> the visibility of the variable only to the file that needs to "see"
> it.
It certainely is a bad practice. What if the type changes (int to long, float
to double, etc.) ? Because the compiler has no way to check the consistency
of the code, if you miss a change the code becomes inconsitent. This practice
is obviously wrong.
If you are concerned with the visibility of the global variables, which
is a good thing, there are other options:
- don't use global at all. (Use ADT's)
- give the user no right to change the data (a const pointer to the data)
/* data.h */
extern int const *px;
/* data.c */
#include "data.h"
static int x;
int const *px = &x;
Of course, in real life, data are gathered in some structure and a single
'context' pointer is enough.
Note that this 'protection' is just an intention. There is no way to prevent
an sloopy programmer to write and compile a code that uses a typecast to
force the pointer to a read/write access to the data. But it will invoke an
undefined behaviour. The ADT solution is safer.
> In your question, in both the cases the variable "a" is visible in
> all the files. In such a situation, there is no benefit in using the
> keyword "extern. You might as well declare the variable as global.
Sorry, I failed to understand your point. 'extern' doesn't make global a
variable. It's the lack of 'static' that makes it.
-- -ed- get my email here: http://marreduspam.com/ad672570 The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html C-reference: http://www.dinkumware.com/manuals/reader.aspx?lib=c99 FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
- Next message: markus: "Usual way to hide jpegs. Extraction off application jpgs??"
- Previous message: Scott Moore: "Re: ascii VS binary files"
- In reply to: umbs.sairam_at_gmail.com: "Re: keyword extern"
- Next in thread: umbs.sairam_at_gmail.com: "Re: keyword extern"
- Reply: umbs.sairam_at_gmail.com: "Re: keyword extern"
- Reply: Minti: "Re: keyword extern"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|