Armenian Knowledge Base  

Go Back   Armenian Knowledge Base > Technical sections > Languages, Compilers, Interpreters > C/C++
Register

Reply
 
LinkBack Thread Tools
Old 16.09.2004, 08:01   #1
The Reloaded
 
Aram Hambardzumyan's Avatar
 
Join Date: 01 2002
Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,387
Downloads: 4
Uploads: 0
Reputation: 146 | 4
Thumbs up .net compiler bug?

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:

PHP Code:
struct S
{
 
int i[10];
 
int*const& ri;
 
S(): ri(i) { }
 
 
void f(const Ss);
};
 
void S::f(const Ss)
{    
// still ok
}    // here the change appears
 
int
main
(int argccharargv[])
{
 
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
Reply With Quote
Old 17.09.2004, 06:07   #2
The Reloaded
 
Aram Hambardzumyan's Avatar
 
Join Date: 01 2002
Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,387
Downloads: 4
Uploads: 0
Reputation: 146 | 4
Default

Yesterday I was given following explanation by one of my colleagues:
PHP Code:
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 Ss);
}; 
Now I have fixed the problem by removing '&' from 'ri' definition.
Reply With Quote
Reply

Thread Tools


На правах рекламы:
реклама

All times are GMT. The time now is 18:44.


Powered by vBulletin® Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.