473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Question about scope of static member variable

All,

When I create a class with a static member variable and reference that
class in a console applicaton it appears as though each instance of the
application has its own separate copy of the member variable.

Therefore, it would appear that the scope of a static class member is
limited to a specific instance of an application that happens to be
running on the machine.

Is this correct?

Thanks,

--Jonathan

May 12 '06 #1
10 2093
"ne************@hotmail.com" <ne************@hotmail.com> wrote:
When I create a class with a static member variable and reference that
class in a console applicaton it appears as though each instance of the
application has its own separate copy of the member variable.

Therefore, it would appear that the scope of a static class member is
limited to a specific instance of an application that happens to be
running on the machine.

Is this correct?


Every application runs in its own address space with its own variables,
unless you work hard (via shared memory or some other technique) to make
it otherwise.

-- Barry
May 12 '06 #2
Hello, Barry!

BK> Every application runs in its own address space with its own variables,
BK> unless you work hard (via shared memory or some other technique) to
BK> make it otherwise.

Scope of static var can be limited to the scope of the thread via ThreadStaticAttribute.
--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
May 12 '06 #3
"Vadym Stetsyak" <va*****@ukr.net> wrote:
Hello, Barry!

BK> Every application runs in its own address space with its own variables,
BK> unless you work hard (via shared memory or some other technique) to
BK> make it otherwise.

Scope of static var can be limited to the scope of the thread via ThreadStaticAttribute.


The OPs question relates to multiple instances of a console application
not sharing the value of a static variable.

-- Barry
May 12 '06 #4
Hello, Barry!

BK> The OPs question relates to multiple instances of a console application
BK> not sharing the value of a static variable.

I've metioned that static variable can have not only application scope, but also thread scope. This notion corrects your post that it is not necessary to "work hard" to "make it otherwise". :8-)

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
May 12 '06 #5
>Every application runs in its own address space with its own variables,
unless you work hard (via shared memory or some other technique) to make
it otherwise.


Barry,

Thanks for the response!

The MSDN documentation seems to suggest that the scope of a static
member extends to a "App Domain".
Does this mean that every instance of a console app running on a
machine lives in a different App Domain?

How does the scenario change if it's an ASP.Net application?
Does every user who accesses a web application get an individual copy
of a static variable as well (or do all users of the web app share the
same app domain)?

Thanks again,

-Jonathan

May 12 '06 #6
>How does the scenario change if it's an ASP.Net application?
Does every user who accesses a web application get an individual copy
of a static variable as well (or do all users of the web app share the
same app domain)?


I just answered my question with another experiment.
It appears that all users of the web application are now accessing the
same copy of the static variable.
I guess this means all instances of a web application are in the same
AppDomain.

Thanks,

Jonathan

May 12 '06 #7
"Vadym Stetsyak" <va*****@ukr.net> wrote:
Hello, Barry!

BK> The OPs question relates to multiple instances of a console application
BK> not sharing the value of a static variable.

I've metioned that static variable can have not only application scope, but
also thread scope. This notion corrects your post that it is not necessary to
"work hard" to "make it otherwise". :8-)


Well, you have to work hard to make the static variable share its
instance between applications. I'll be more clear next time.

-- Barry
May 12 '06 #8
"ne************@hotmail.com" <ne************@hotmail.com> wrote:
How does the scenario change if it's an ASP.Net application?
Does every user who accesses a web application get an individual copy
of a static variable as well (or do all users of the web app share the
same app domain)?


I just answered my question with another experiment.
It appears that all users of the web application are now accessing the
same copy of the static variable.
I guess this means all instances of a web application are in the same
AppDomain.


That is a fairly configurable aspect of ASP.NET on Win2K3 - the domains
get recycled, etc.

-- Barry
May 12 '06 #9
On Fri, 12 May 2006 19:59:51 +0100, Barry Kelly
<ba***********@gmail.com> wrote:
"ne************@hotmail.com" <ne************@hotmail.com> wrote:
>How does the scenario change if it's an ASP.Net application?
>Does every user who accesses a web application get an individual copy
>of a static variable as well (or do all users of the web app share the
>same app domain)?


I just answered my question with another experiment.
It appears that all users of the web application are now accessing the
same copy of the static variable.
I guess this means all instances of a web application are in the same
AppDomain.


That is a fairly configurable aspect of ASP.NET on Win2K3 - the domains
get recycled, etc.

-- Barry


You have to be careful if you rely upon a shared variable within asp.net
as the web server can still choose to load a new app domain if that is
easier for processing threads. It can also remove one to free memory if
it hasn't been used in a while.
May 12 '06 #10
>You have to be careful if you rely upon a shared variable within asp.net
as the web server can still choose to load a new app domain if that is
easier for processing threads. It can also remove one to free memory if
it hasn't been used in a while.


Thanks for the reply!
I want to share one instance of an object across an app domain (in an
ASP dot net application), and I'm trying to weigh the pros and cons of
the following two approaches:

a) Storing the object in ApplicationState
b) Storing the object in a static member variable (utilizing a
singleton design pattern)

Any recommendations on which method would be more suitable?
Are there any other approaches that might be better?

The object in question is a dot net class wrapping a COM component.
The class creates an SSL session to a proprietary device that manages
encryption and decryption.

Thanks,
Jonathan

May 15 '06 #11

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

Similar topics

7
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type ...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
5
by: Jeff | last post by:
Hey Below is a C# program I've made.... it's an app where I experiment with variable scope. In this code you see two variables named i (one is a local variable and the other is a member of the...
22
by: Luna Moon | last post by:
I am reading the book "C++ Annotations", and here is a quote from the book: Namespaces can be defined without a name. Such a namespace is anonymous and it restricts the visibility of the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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
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...

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.