473,385 Members | 1,359 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,385 software developers and data experts.

static field initialization

Does anyone know if the standard has anything to say about how static fields
that use other static fields in initialization behave? Pointers into the
standard would be most appreciated. Thanks.

public struct SomeStruct
{
public SomeStruct(int i)
{
I = i;
}

public int I;
}

public struct S1
{
public static SomeStruct S = new SomeStruct(1);
}

public struct S2
{
public static SomeStruct S = new SomeStruct(S1.I);
}

(p.s. Sorry if this doesn't compile, I don't have a compiler from my posting
location.)
Is S2.S.I guaranteed to be 1, 0, or undefined? Does the order in which the
structs are declared change the outcome? If SomeStruct is a class, does
that affect the outcome? If S1.S and S2.S are readonly, does that affect
the outcome? If S1 and S2 are in different namespace, assemblies, etc.?
I've seen instances where S2.S.I == 0 in MSVC 7.1 .NET 1.1 under the
debugger (and the debugger is not displaying 0 when the actual value is 1,
the value really is 0).

Thanks,
Steve
Nov 16 '05 #1
1 2165
Stephen Lamb <sl***@easystreet.com> wrote:
Does anyone know if the standard has anything to say about how static fields
that use other static fields in initialization behave? Pointers into the
standard would be most appreciated. Thanks.
Have a look at section 9.5 of the CLI spec partition 1.
public struct SomeStruct
{
public SomeStruct(int i)
{
I = i;
}

public int I;
}

public struct S1
{
public static SomeStruct S = new SomeStruct(1);
}

public struct S2
{
public static SomeStruct S = new SomeStruct(S1.I);
}

(p.s. Sorry if this doesn't compile, I don't have a compiler from my posting
location.)

Is S2.S.I guaranteed to be 1, 0, or undefined?
1, as you've written it there. It would be 0 if S1 referred to S2
before its declaration of S1 and S1 were referenced before S2 though.
Does the order in which the structs are declared change the outcome?
No.
If SomeStruct is a class, does that affect the outcome?
No.
If S1.S and S2.S are readonly, does that affect
the outcome? If S1 and S2 are in different namespace, assemblies, etc.?
No.
I've seen instances where S2.S.I == 0 in MSVC 7.1 .NET 1.1 under the
debugger (and the debugger is not displaying 0 when the actual value is 1,
the value really is 0).


I wonder whether the debugger is changing things about static
initialization - I wouldn't be surprised.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

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

Similar topics

1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
5
by: Luther Baker | last post by:
Hi, Is the order of initialization guaranteed for static members as it is for instance members? Namely, the order they appear the in the declaration? ie: foo.h:
4
by: hyd | last post by:
With C++/CLI - VS2005, is it possible to have static constructors that are automatically called when the owner assembly is loading ? Otherwise, how it is possible to call it without creating an...
10
by: John A Grandy | last post by:
Say I have Class1 which contains static Class2 var1 = new Class2(); Is Class2 constructor code only executed if var1 is referenced in the code-execution path ? Or is Class2 constructor code...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
8
by: Per Bull Holmen | last post by:
Hey Im new to c++, so bear with me. I'm used to other OO languages, where it is possible to have class-level initialization functions, that initialize the CLASS rather than an instance of it....
3
by: Steve Folly | last post by:
Hi, I had a problem in my code recently which turned out to be the 'the "static initialization order fiasco"' problem (<http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.12>) The FAQ...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
6
by: gs | last post by:
Hi, I want to know that when memory get allocated to static data member of a class. class A { static int i; }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.