extern

M

Mark A. Odell

My question is about extern variables.
I have a structure in a .h file and its made extern as follows,

extern STATS_STRUCT Status[];

Actually in the .c file, the size of the array is defined. how does it
work ?
When is the memory actually allocated ?

I'd say the C file reserves the memory and the linker gives it an offset
in the final output image. The host system that loads this image allocates
the memory. The extern in the .h just tells other modules that need access
to Status[] what its type is.
 
S

shalu

My question is about extern variables.
I have a structure in a .h file and its made extern as follows,

extern STATS_STRUCT Status[];

Actually in the .c file, the size of the array is defined. how does i
work ?
When is the memory actually allocated


-
shal
 
J

Joe Wright

shalu said:
My question is about extern variables.
I have a structure in a .h file and its made extern as follows,

extern STATS_STRUCT Status[];

Actually in the .c file, the size of the array is defined. how does it
work ?
When is the memory actually allocated ?

The extern qualifier in the .h header declares an object by type and
name and suggests that the object is not defined here (in this
translation unit which includes the header). This also infers that
there are two or more .c translation units including the header.

One and only one of the .c translation units provides a definition
of the object. The linker then puts all the .o files together and
causes all extern references to refer to the single object.
 
F

Fanie Smith

Since the "extern" precedes the definition, it simply declares the array so
that other functions know what to make of it during compilation.

The actual definition takes place when the "extern" is omitted and this may
be done anywhere in the code, and the size is only needed during linking
time.

Unlike common misconception, the difinition may take place in either the
header or implementation file, however, if in the header (depending on the
size and structure of your file) it may defined more than once if the header
is imported more that once which will result in a compile error.

I personally prefer to place the variable definitions of all global
variables in the header files, since this simplifies maintanance, but you
have to construct your headers carefully.
 
J

Joe Wright

Fanie said:
Since the "extern" precedes the definition, it simply declares the array so
that other functions know what to make of it during compilation.

The actual definition takes place when the "extern" is omitted and this may
be done anywhere in the code, and the size is only needed during linking
time.

Unlike common misconception, the difinition may take place in either the
header or implementation file, however, if in the header (depending on the
size and structure of your file) it may defined more than once if the header
is imported more that once which will result in a compile error.

I personally prefer to place the variable definitions of all global
variables in the header files, since this simplifies maintanance, but you
have to construct your headers carefully.

That's generally considered "A Bad Thing (c)". Headers should
declare things, objects and values, text substitutions, etc. The
header should never cause the creation of any object or function.

Personal preferences aside, headers allow translation units
(modules) to share certain information common to all of them.

If you take care not to define objects, you can use the same header
for all the modules.

As soon as the header defines something, it can only be used once.
That means each of your .c modules will require a different header.
If a .c module cannot share headers with its siblings, constructed
carefully or not, why have a header at all?

Please explain 'simplifies maintenance' in this context.
 
L

Lawrence Kirby

My question is about extern variables.
I have a structure in a .h file and its made extern as follows,

extern STATS_STRUCT Status[];

Actually in the .c file, the size of the array is defined. how does it
work ?

It is the definition in the .c file that causes the object to be created.
The declaration in the .h file simply indicates that such a thing exists
somewhere with the type specified. The process of linkage ensures that the
various references to Status access the same object.
When is the memory actually allocated ?

Status is an object with static storage duration and like any such object
it is created at program startup and exists for the entire lifetime of the
program.

Lawrence
 
M

Mark McIntyre

My question is about extern variables.
I have a structure in a .h file and its made extern as follows,

extern STATS_STRUCT Status[];

This is a declaration. No memory is allocated for it. After compilation,
this statement has disappeared, and there's nothing there any more.
Actually in the .c file, the size of the array is defined. how does it
work ?

In the c module, the compiler encounters a definition of the object, and
emits instructions to create it.
When is the memory actually allocated ?

Typically global variables exist for the entire life of your programme. I
believe that technically they must appear as if they were are created just
before they were first accessed, and destroyed just after they were last
accesed, in both cases irrespective of which module the access was from. A
clever optimising runtime environment might thus delay actual creation
until the object was first accessed but the 'as if' rule would apply.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top