Connecting Tech Pros Worldwide Forums | Help | Site Map

Variable Scope, Initialization, Linkage etc.

Neelesh Bodas
Guest
 
Posts: n/a
#1: Jan 4 '06
Hello All,

I was just listing down various ways in which variables can be created
and destroyed in C++. (On the lines of 10.4.3 TC++PL Ed 3)

Putting the summary here for corrections, comments, criticism, advices,
improvements.

Abbreviation:
Created (C)
Destroyed (D)
Lifetime (L)
Visibility (V)
Location (Lo)
Linkage (Li)
----------------------------

1.Local data inside a function
C When control hits the definition
D At the end of scope
L For the scope in which they are declared
V Lexical scope
LO Stack
LI No linkage

2.Static data inside a function
C First time when control hits the definition
D When the program exits normally.
L From the first time control reaches upto end of program
V Lexical scope
LO Data setcion
LI Internal

3.Non-static Global or namespace declared data
C Before the program starts
D When the program exits normally
L Throughout the program's lifetime
V Throughout the program (wherever namespace is made
available)
LO Data
LI External

4.Static global or namespace declared data
C Before the program starts
D When the program exits normally
L Throughout the program's lifetime
V In the module in which it is declared
LO Data
LI Internal

5.Temporaries
C While evaluting intermediate expressions
D When the full expression is evaluated
L Same as lifetime of the expression for which it is created
V Lexical scope of the expression
LO Stack
LI No linkage

6.Static member of a class
C When the class is instantiated for the first time.
D When the program exits normally
L From the time of creation till the end of program
V Class scope
LO Data
LI Internal linkage

7.Non static member of a class
C Every time class is instantiated
D Every time instance is destroyed
L Same as the lifetime of the instance
V Class scope
LO Same as location of instance
LI Same as linkage of the instance

8.Heap allocated data
C On calling 'new'
D On calling delete
L From explicit creation upto explict deletion or end of program
V Same as the visibility of the pointer pointing to the heap memory
LO Heap
LI Same as the linkage of the pointer pointing to heap memory.


-----------------------------------------------------------------------------------------


Jaspreet
Guest
 
Posts: n/a
#2: Jan 4 '06

re: Variable Scope, Initialization, Linkage etc.



Neelesh Bodas wrote:[color=blue]
> Hello All,
>
> I was just listing down various ways in which variables can be created
> and destroyed in C++. (On the lines of 10.4.3 TC++PL Ed 3)
>
> Putting the summary here for corrections, comments, criticism, advices,
> improvements.
>
> Abbreviation:
> Created (C)
> Destroyed (D)
> Lifetime (L)
> Visibility (V)
> Location (Lo)
> Linkage (Li)
> ----------------------------
>[/color]
<snip>[color=blue]
>
> 6.Static member of a class
> C When the class is instantiated for the first time.
> D When the program exits normally
> L From the time of creation till the end of program
> V Class scope
> LO Data
> LI Internal linkage
>[/color]
<snip>

I have doubt on the Creation part of a static member of a class. I do
not think you need an instance of the class for the static member to be
created.

Experts your 2 cents please.

A nice way to list out important points though.

Neelesh Bodas
Guest
 
Posts: n/a
#3: Jan 4 '06

re: Variable Scope, Initialization, Linkage etc.



Jaspreet wrote:[color=blue][color=green]
> >
> > 6.Static member of a class
> > C When the class is instantiated for the first time.
> > D When the program exits normally
> > L From the time of creation till the end of program
> > V Class scope
> > LO Data
> > LI Internal linkage
> >[/color]
> <snip>
>
> I have doubt on the Creation part of a static member of a class. I do
> not think you need an instance of the class for the static member to be
> created.
>[/color]

Yeah, that needs to be corrected. But not sure what is appropriate -
static member will be created when it will be accessed for the first
time, or at the start of program?

Shezan Baig
Guest
 
Posts: n/a
#4: Jan 4 '06

re: Variable Scope, Initialization, Linkage etc.


Neelesh Bodas wrote:[color=blue]
> Yeah, that needs to be corrected. But not sure what is appropriate -
> static member will be created when it will be accessed for the first
> time, or at the start of program?[/color]


At the start of the program, but order of initialisation is not
specified.

-shez-

Shezan Baig
Guest
 
Posts: n/a
#5: Jan 4 '06

re: Variable Scope, Initialization, Linkage etc.



Shezan Baig wrote:[color=blue]
> Neelesh Bodas wrote:[color=green]
> > Yeah, that needs to be corrected. But not sure what is appropriate -
> > static member will be created when it will be accessed for the first
> > time, or at the start of program?[/color]
>
>
> At the start of the program, but order of initialisation is not
> specified.[/color]


Correction: order of initialisation in different translation units :)

-shez-

Luke Meyers
Guest
 
Posts: n/a
#6: Jan 4 '06

re: Variable Scope, Initialization, Linkage etc.


"Sometime before main()" is my favorite characterization.

Now, a really interesting (read: pathological and degenerate) thing
would be for the initialization of a static variable to invoke main().
I guess that would mean that it's really "sometime before the automatic
invocation of main(), but not necessarily before some weirdo calls it
manually."

I love this programming language, I really do.

Luke

David Harmon
Guest
 
Posts: n/a
#7: Jan 4 '06

re: Variable Scope, Initialization, Linkage etc.


On 4 Jan 2006 05:34:51 -0800 in comp.lang.c++, "Luke Meyers"
<n.luke.meyers@gmail.com> wrote,[color=blue]
>Now, a really interesting (read: pathological and degenerate) thing
>would be for the initialization of a static variable to invoke main().[/color]

"The function main shall not be called from within a program. The
linkage of main is implementation-defined. A program that takes the
address of main, or declares it inline or static is ill-formed."

Closed Thread