Connecting Tech Pros Worldwide Help | Site Map

VC++ 5.0 runs this different then VC++2003

  #1  
Old May 26th, 2006, 06:35 PM
Jeffrey Baker
Guest
 
Posts: n/a
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


  #2  
Old May 26th, 2006, 06:45 PM
mlimber
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003


Jeffrey Baker wrote:[color=blue]
> 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

  #3  
Old May 26th, 2006, 06:55 PM
Victor Bazarov
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003


Jeffrey Baker wrote:[color=blue]
> 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;
>
> }[/color]

Your program leaves both 'wd' and 'st' members of the 'a' object
*uninitialised*. That means they contain garbage. If you want
them to contain something particular, you have to specifically say
so (in 'Repro's constructor). If you declare 'a' *static*, for
instance, the memory it occupies will be zeroed out, and that's
something meaningful. But as it stands, 'a' is *automatic* and its
memory has random contents.

The difference between compilers is immaterial.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #4  
Old May 26th, 2006, 06:55 PM
Erik Wikström
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003


On 2006-05-26 19:32, Jeffrey Baker wrote:[color=blue]
> 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.[/color]

Seems like it's working as it should to me. Since you have not
initialized the data it will be whatever was there when the memory was
allocated for the data. The reason that you didn't get any data when
using VC++5 was that the memory allocated was zeroed out, but it's not
something you can depend on.

Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
  #5  
Old May 26th, 2006, 07:05 PM
Jeffrey Baker
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003




--
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=blue]
> Jeffrey Baker wrote:[color=green]
>> 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


  #6  
Old May 26th, 2006, 07:25 PM
mlimber
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003


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

  #7  
Old May 26th, 2006, 07:35 PM
Victor Bazarov
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003


mlimber wrote:[color=blue]
> Jeffrey Baker wrote:[color=green]
>> [..]
>>
>> 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;
> }[/color]

Actually, a compliant compiler should also accept

Repro::Repro() : wd(), st() {}

for that. Not sure if VC++ 2003 is that good. Probably not.
[color=blue]
> 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.[/color]

A raw array is often faster. I would agree if the OP was using pointers
and allocating C strings himself, though...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


  #8  
Old May 26th, 2006, 07:45 PM
mlimber
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003


Victor Bazarov wrote:[color=blue]
> mlimber wrote:[color=green]
> > Repro::Repro()
> > {
> > wd[0] = 0;
> > st.word[0] = 0;
> > }[/color]
>
> Actually, a compliant compiler should also accept
>
> Repro::Repro() : wd(), st() {}
>
> for that. Not sure if VC++ 2003 is that good. Probably not.[/color]

Good point.
[color=blue][color=green]
> > 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.[/color]
>
> A raw array is often faster. I would agree if the OP was using pointers
> and allocating C strings himself, though...[/color]

True, but assembly is also often faster. As you well know, correctness
and maintainability should be primary concerns, while speed should be
secondary (cf. the old adage about making a fast program correct vs.
making a correct program fast).

Cheers! --M

  #9  
Old May 26th, 2006, 08:15 PM
Victor Bazarov
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003


mlimber wrote:[color=blue]
> [..] As you well know, correctness
> and maintainability should be primary concerns, while speed should be
> secondary (cf. the old adage about making a fast program correct vs.
> making a correct program fast).[/color]

Well, to know how to make a correct program fast one should be aware
of the differences between 'std::string' and how it manages its memory
and a plain array as a member, and what the implications are for using
one versus the other. That's why I am not convinced that the sweeping
generalisation that it's "better still" to use 'std::string' here.

Speaking from experience, mind you. So, I do "well know".


  #10  
Old May 26th, 2006, 08:35 PM
mlimber
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003


Victor Bazarov wrote:[color=blue]
> mlimber wrote:[color=green]
> > [..] As you well know, correctness
> > and maintainability should be primary concerns, while speed should be
> > secondary (cf. the old adage about making a fast program correct vs.
> > making a correct program fast).[/color]
>
> Well, to know how to make a correct program fast one should be aware
> of the differences between 'std::string' and how it manages its memory
> and a plain array as a member, and what the implications are for using
> one versus the other.[/color]

Sure.
[color=blue]
> That's why I am not convinced that the sweeping
> generalisation that it's "better still" to use 'std::string' here.[/color]

I'm just saying that the first choice for such usage should be
generally and sweepingly be std::string. When the speed of std::string
is proven by measurement to be an issue, then other options should be
considered.

Cheers! --M

  #11  
Old May 26th, 2006, 09:05 PM
Marcus Kwok
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003


Victor Bazarov <v.Abazarov@comacast.net> wrote:[color=blue]
> mlimber wrote:[color=green]
>> Repro::Repro()
>> {
>> wd[0] = 0;
>> st.word[0] = 0;
>> }[/color]
>
> Actually, a compliant compiler should also accept
>
> Repro::Repro() : wd(), st() {}
>
> for that. Not sure if VC++ 2003 is that good. Probably not.[/color]

It is not.

See:
http://groups.google.com/group/comp....b44a8b786b6a79

and Dietmar Kuehl's followup. They have fixed it in VC+ 2005. However,
you can work around this in a standards-compliant manner by wrapping the
class in another:
http://groups.google.com/group/micro...0dbefaa1c4286/

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
  #12  
Old May 26th, 2006, 10:05 PM
Default User
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003


Jeffrey Baker wrote:

Here's a technical note about your post. You properly posted your
comments after the quotes, but your newsreader slapped your .sig at the
very top. This put your entire post into your .sig. That's bad.

The reason this is happening it that you are using the braindead
Microsoft Outlook Express. There are few things you can do to correct
the problem:

1. Get a different newsreader. There are many good ones.

2. Don't use a .sig.

3. Find and install OE Quotefix.





Brian
  #13  
Old May 27th, 2006, 12:55 AM
Jeffrey Baker
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003


"Default User" <defaultuserbr@yahoo.com> wrote in message
news:Izw3ur.u8@news.boeing.com...[color=blue]
> Jeffrey Baker wrote:
>
> Here's a technical note about your post. You properly posted your
> comments after the quotes, but your newsreader slapped your .sig at the
> very top. This put your entire post into your .sig. That's bad.
>
> The reason this is happening it that you are using the braindead
> Microsoft Outlook Express. There are few things you can do to correct
> the problem:
>
> 1. Get a different newsreader. There are many good ones.
>
> 2. Don't use a .sig.
>
> 3. Find and install OE Quotefix.
>
>
>
>
>
> Brian[/color]

Funny.

--
It is far better to post code then to never have posted


  #14  
Old May 27th, 2006, 01:05 AM
Jeffrey Baker
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:e57f42$e6e$1@news.datemas.de...[color=blue]
> Jeffrey Baker wrote:[color=green]
>> 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;
>>
>> }[/color]
>
> Your program leaves both 'wd' and 'st' members of the 'a' object
> *uninitialised*. That means they contain garbage. If you want
> them to contain something particular, you have to specifically say
> so (in 'Repro's constructor). If you declare 'a' *static*, for
> instance, the memory it occupies will be zeroed out, and that's
> something meaningful. But as it stands, 'a' is *automatic* and its
> memory has random contents.
>
> The difference between compilers is immaterial.
>
> V
> --
> Please remove capital 'A's when replying by e-mail
> I do not respond to top-posted replies, please don't ask
>[/color]
Here is the program fixed -

//Reproduce Garbage lost due to Repro(){ strcpy(st.word," ");strcpy(wd,"
");}

#include <iostream>

using std::cout;

using std::endl;

#include <string>

class Repro

{

public:

Repro(){ strcpy(st.word," ");strcpy(wd," ");}

void View();

private:

char wd[10];

struct

{

char word[10];

}st;

};

void Repro::View()

{

cout << "word " << st.word << endl;

cout << "wd " << wd << endl;

}

int main()

{

Repro a;

a.View();

return 0;

}

I'll have to look at the "static" you mentioned.

Thanks,
Jeff
--
It is far better to post code then to never have posted


  #15  
Old May 27th, 2006, 06:35 AM
Default User
Guest
 
Posts: n/a

re: VC++ 5.0 runs this different then VC++2003


Jeffrey Baker wrote:
[color=blue]
> "Default User" <defaultuserbr@yahoo.com> wrote in message
> news:Izw3ur.u8@news.boeing.com...[color=green]
> > Jeffrey Baker wrote:
> >
> > Here's a technical note about your post. You properly posted your
> > comments after the quotes, but your newsreader slapped your .sig at
> > the very top. This put your entire post into your .sig. That's bad.[/color][/color]
[color=blue]
> Funny.[/color]

??

I wasn't attempting to be funny. I was quite serious. OE Quotefix was
designed to repair that defect in Lookout Express. Do you not see the
signature at the top of the message?


Brian
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
why migrate from VC++ win32 to C++.NET windows applications? John answers 14 June 12th, 2006 05:05 AM
why migrate from VC++ win32 to C++.NET windows applications? John answers 14 June 12th, 2006 05:05 AM
VC6 speed vs VS2003 YAZ answers 7 November 17th, 2005 05:26 PM
weird VC.NET 2003 compiler behaviour (for C++ windows DLL based project) Daniel Yelland answers 8 November 17th, 2005 03:30 PM