473,503 Members | 2,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Variable Scope, Initialization, Linkage etc.

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

Jan 4 '06 #1
6 2138

Neelesh Bodas wrote:
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)
----------------------------
<snip>
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.

Jan 4 '06 #2

Jaspreet wrote:

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.


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?

Jan 4 '06 #3
Neelesh Bodas wrote:
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-

Jan 4 '06 #4

Shezan Baig wrote:
Neelesh Bodas wrote:
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.

Correction: order of initialisation in different translation units :)

-shez-

Jan 4 '06 #5
"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

Jan 4 '06 #6
On 4 Jan 2006 05:34:51 -0800 in comp.lang.c++, "Luke Meyers"
<n.***********@gmail.com> wrote,
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."

Jan 4 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2576
by: Method Man | last post by:
Can someone explain the scope/linkage differences between the following 4 global declarations and when one should be used (theoretically) over the rest? sample.h --------- #ifndef SAMPLE_H...
8
3358
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
14
2377
by: rahul8143 | last post by:
hello, To limit scope of a variable in a single file that is part of a large project that have several C files we use static variable right?then to limit any variable to function scope it should...
23
19133
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
6
8329
by: shaun | last post by:
If I put (define) const variables at the top of a .cpp file but do not declare them in the .h file, are they only visible within that .cpp file? e.g. const int a={1,2,3,4}; in a cpp file...
2
4691
by: Shraddha | last post by:
Can we declare extern variable as static? What will be the scope of the variable then? What if we change the value of the variable in some other function? Also can someone tell me that if we can...
112
5348
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
11
8292
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
1
3742
by: Giacomo Catenazzi | last post by:
Hello, To learn the details of C, I've build the following example, could you check if it is correct and if it miss some important cases? Are there some useful (real cases) examples of: -...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.