Jeffrey Baker wrote:[color=blue]
> --
> It is far better to post code then to never have posted
> "mlimber" <mlimber@gmail.com> wrote in message
> news:1148665190.163816.208910@y43g2000cwc.googlegr oups.com...[color=green]
> > Jeffrey Baker wrote:[color=darkred]
> >> Hello,
> >>
> >> I wrote a program way back when VC++ 5.0 was around and when using this
> >> program in VC++ 2003 I get garbage. I was able to let the program run
> >> through code that would view the data. Since each object is new there
> >> should be no data to show. Here is some code that would run clean in VC++
> >> 5.0 and in VC++ 2003 there is garbage.
> >>
> >> //Reproduce Garbage
> >>
> >> #include <iostream>
> >>
> >> using std::cout;
> >>
> >> using std::endl;
> >>
> >> class Repro
> >>
> >> {
> >>
> >> private:
> >>
> >> char wd[10];
> >>
> >> struct
> >>
> >> {
> >>
> >> char word[10];
> >>
> >> }st;
> >>
> >> public:
> >>
> >> void View();
> >>
> >> };
> >>
> >> void Repro::View()
> >>
> >> {
> >>
> >> cout << "word " << st.word << endl;
> >>
> >> cout << "wd " << wd << endl;
> >>
> >> }
> >>
> >> int main()
> >>
> >> {
> >>
> >> Repro a;
> >>
> >> a.View();
> >>
> >> return 0;
> >>
> >> }
> >>
> >> Regards,
> >> Jeff
> >> --
> >> It is far better to post code then to never have posted[/color]
> >
> > No. Each array is uninitialized, so the behavior is undefined. It might
> > do something nice (e.g., nothing); it might do something bad. There's
> > really no telling.
> >
> > Cheers! --M
> >[/color]
>
> I only need to add a contructor like strcpy(st.word, " ") and everything is
> cleaned.
>
> jb[/color]
Well, first that's not "a constructor" at all (that term has a specific
meaning in C++ land). It's an initialization you could put in the
constructor body. But it would work, though you could improve upon it
in several ways, e.g., by using an empty string ("") or by doing
something something shorter like:
Repro::Repro()
{
wd[0] = 0;
st.word[0] = 0;
}
Or better still, don't use a raw array or the C-style string functions
at all. Use std::string (see
http://www.parashift.com/c++-faq-lit...html#faq-34.1), and
then that class' constructor will initialize it for you automatically.
Cheers! --M