PDA

View Full Version : .net compiler bug?


Aram Hambardzumyan
Sep 16, 2004, 07:01
This seems like another bug in compiler but I would like to hear your opinions as well. In the code below the class defines two members: an array and a reference to that array (with scalar type of first member, everything works well). when the function S::f(const S& s) is invoked, the value for s.ri changes. I'd like to mention that function f isn't necessarily to be the member of the same S as it's argument, I did this way just in order not to introduce another struct/class.

As comments describe, value of s.ri coincides with s.i just before entering function body, and differs right after it:

struct S
{
int i[10];
int*const& ri;
S(): ri(i) { }

void f(const S& s);
};

void S::f(const S& s)
{ // still ok
} // here the change appears

int
main(int argc, char* argv[])
{
S s;
s.f(s);

return 0;
}


P. S. This happened both with first .net compiler as well as with .net 2003, but not with msvc6

Aram Hambardzumyan
Sep 17, 2004, 05:07
Yesterday I was given following explanation by one of my colleagues:
struct S
{
int i[10];
int*const& ri;

S(): ri(i) { }
// in this piece of code ri is initialized with
// a temporary pointer (allocated on the stack) pointing to &i[0]
// Thus ri refers to a location on the stack which is unrelated to the object
// of class s and the value aliased by ri changes when bits of that location on the
// stack are overwritten

void f(const S& s);
};

Now I have fixed the problem by removing '&' from 'ri' definition.