Variable Scope, Initialization, Linkage etc.

N

Neelesh Bodas

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.


-----------------------------------------------------------------------------------------
 
J

Jaspreet

Neelesh said:
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)
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
<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.
 
N

Neelesh Bodas

Jaspreet said:
<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.

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?
 
S

Shezan Baig

Neelesh said:
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?


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

-shez-
 
S

Shezan Baig

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


Correction: order of initialisation in different translation units :)

-shez-
 
L

Luke Meyers

"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
 
D

David Harmon

On 4 Jan 2006 05:34:51 -0800 in comp.lang.c++, "Luke Meyers"
Now, a really interesting (read: pathological and degenerate) thing
would be for the initialization of a static variable to invoke main().

"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."
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top