?? Definite Assignment of Structs -- Language Spec Issue ??

T

Tom Baxter

Hi All,

I was hoping someone could clarify something I read in the C# Language Spec
regarding "Definite Assignment" of structs. This is a very subtle point.

I am referring to ECMA-334, section 12.3:

The definite assignment states of instance variable of a struct-type
variable are tracked individually as well as collectively. In addition to
the rules above, the following rules apply to struct-type variables and
their instance variables:

* An instance variable is considered definitely assigned if its
containing struct-type variable is considered definitely assigned.
* A struct-type variable is considered definitely assigned if each of
its instance variable is considered definitely assigned.

It seems like the two bullet points above can result in circular logic
depending on one's interpretation. If you interpret these points as:

* An instance variable is considered definitely assigned if and only if
its containing struct-type variable is considered definitely assigned.
* A struct-type variable is considered definitely assigned if and only
if each of its instance variable is considered definitely assigned.

then we have a deadlock situation because a struct's instance members are
definitely assigned exactly when the struct variable is definitely assigned.
But the struct variable is definitely assigned exactly when all its instance
variables are definitely assigned. It's the Chicken and the Egg Problem.

Now, I realize the meaning of "if" is much different from that of "if and
only if" but I'm wondering about the intent of the passages. Did the authors
really mean "if and only if"? I doubt it. I suspect the authors meant "if",
as it's written.

(I told you it was subtle). So, if the authors of the spec meant "if", as it
was written, then there must be some way for the instance variables of a
struct variable to be definitely assigned other than the struct variable
itself being definitely assigned. Similarly, there must be some way for the
struct variable to be definitely assigned other than having all its instance
variables definitely assigned.

So (whew!), assuming the authors of the spec meant to say "if" and not "if
and only if", my question is, how can the instance variables of a struct be
definitely assigned without the containing struct variable being definitely
assigned? And, how can a struct instance variable be definitely assigned
without all of its instance variables being definitely assigned?

If you tell me there is a mutual dependency between the struct instance
members and the struct variable itself, then you are telling me we have a
Chicken and Egg Problem.

Anyone still reading??? Thanks!
 
P

Peter Duniho

[...] So (whew!), assuming the authors of the spec meant to say "if"
and not "if and only if", my question is, how can the instance
variables of a struct be definitely assigned without the containing
struct variable being definitely assigned?

Simple:

struct A
{
public int x;
public int y;
}

void Method()
{
A a; // a, a.x, and a.y are not definitely assigned

a.x = 1; // a.x is definitely assigned, a, a.y are not
a.y = 2; // a.y is definitely assigned, as is a
}
And, how can a struct instance variable be definitely assigned without
all of its instance variables being definitely assigned?

It can't. Thus the first clause. If the struct itself is definitely
assigned, then all of the instance variables are definitely assigned as
well.

Pete
 
J

Jialiang Ge [MSFT]

Hello Tom,

As you see in the spec document,
* An instance variable is considered definitely assigned if its
containing struct-type variable is considered definitely assigned.
* A struct-type variable is considered definitely assigned if each of
its instance variable is considered definitely assigned.

The writer means that

An instance is definitely assigned <= its containing struct types are
definitely assigned
A struct type is definitely assigned <= its containing instances are
definitely assigned

If we put them into a chain, it will be:
An instance is definitely assigned <= its containing struct types are
definitely assigned <= its containing instances are definitely assigned <=
its containing struct types are definitely assigned ¡­

The writer does not mean 'if and only if' by 'if'. 'if and only if' stands
for <==>, rather than <= in the above logic.

Therefore, your current question is why An instance is definitely assigned
CANNOT => its containing struct types are definitely assigned. Am I right?

I am consulting the issue now and will get back to you as soon as possible.

Sincerely,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
T

Tom Baxter

Hello Jialiang,

I'm glad you put it in "logical notation". Here is what the spec says:

(a) The containing struct-type variable is definitely assigned => (b) All
instance variables are definitely assigned.
(b) All instance variables are definitely assigned => (a) The containing
struct-type variable is definitely assigned.

So we have a chain of the form ... => a => b => a => b => a => ...

In order for this "chain" to be true (and according to the spec, it is), at
least one of the following must be true:

"a" can be made true by some means other than "b" being true.
"b" can be made true by some means other than "a" being true.

So, the question is, *how* can "a" be made true independent of "b" being
true, or vice versa.

Peter Duniho's response to my question gives the answer. I'll repeat Pete's
response with some added comments:

struct MyStruct {
public int x;
public int y;
}

void Method() {
MyStruct a; // a, a.x, and a.y are not definitely assigned

a.x = 1; // a.x is definitely assigned, a, a.y are not
a.y = 2; // a, a.x, and a.y are ALL definitely assigned
}

Once a.x and a.y are assigned, then "a" is considered definitely assigned.
As an experiment, create a method that takes a MyStruct as a parameter. You
will not be able to pass "a" to that method until a.y has been assigned. At
that point, "a" is definitely assigned.

The above code shows, " All instance variables are definitely assigned =>
The containing struct-type variable is definitely assigned."

Now what about the other condition: "The containing struct-type variable is
definitely assigned => All instance variables are definitely assigned."?
Here's an example that:

void Method() {
MyStruct a = new MyStruct(); // a, a.x, and a.y are ALL definitely
assigned
}

So, the spec is correct (relief!). The problem was with my interpretation.
Thanks for your responses Jialiang and Pete.

Tom.
 

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

Top