473,659 Members | 2,722 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 2149

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

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
2588
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 #define SAMPLE_H int a;
8
3367
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 actual program. To me "scope" of the name of a function or object are the general rules for the areas of a program that can through a declaration, have "visibility."
14
2407
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 be declared as auto or anything else?
23
19167
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
8338
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 will be a 'global' with file scope? (no flames for oxymoron, please), available for use inside my functions which are defined in the same file (but which are also declared in the .h)?
2
4704
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 declare the global variable...and it is having scope throughout the file then what is so different in extern variable???
112
5425
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 that may print some messages. foo(...) { if (!silent)
11
8310
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 device_t { const device_backend_t *backend; ... } device_t; typedef struct device_backend_t {
1
3755
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: - "function prototype scope" for structures and unions? - "extern" for internal linkage ?
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
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 we have to send another system
2
1737
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.