469,889 Members | 1,234 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,889 developers. It's quick & easy.

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

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
May 26 '06 #1
14 1572
Jeffrey Baker wrote:
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


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

May 26 '06 #2
Jeffrey Baker wrote:
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;

}


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
May 26 '06 #3
On 2006-05-26 19:32, Jeffrey Baker wrote:
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.


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
May 26 '06 #4


--
It is far better to post code then to never have posted
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
Jeffrey Baker wrote:
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


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


I only need to add a contructor like strcpy(st.word, " ") and everything is
cleaned.

jb
May 26 '06 #5
Jeffrey Baker wrote:
--
It is far better to post code then to never have posted
"mlimber" <ml*****@gmail.com> wrote in message
news:11**********************@y43g2000cwc.googlegr oups.com...
Jeffrey Baker wrote:
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


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


I only need to add a contructor like strcpy(st.word, " ") and everything is
cleaned.

jb


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

May 26 '06 #6
mlimber wrote:
Jeffrey Baker wrote:
[..]

I only need to add a contructor like strcpy(st.word, " ") and
everything is cleaned.

jb
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;
}


Actually, a compliant compiler should also accept

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

for that. Not sure if VC++ 2003 is that good. Probably not.
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.


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
May 26 '06 #7
Victor Bazarov wrote:
mlimber wrote:
Repro::Repro()
{
wd[0] = 0;
st.word[0] = 0;
}


Actually, a compliant compiler should also accept

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

for that. Not sure if VC++ 2003 is that good. Probably not.


Good point.
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.


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


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

May 26 '06 #8
mlimber wrote:
[..] 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).


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".
May 26 '06 #9
Victor Bazarov wrote:
mlimber wrote:
[..] 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).
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.


Sure.
That's why I am not convinced that the sweeping
generalisation that it's "better still" to use 'std::string' here.


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

May 26 '06 #10
Victor Bazarov <v.********@comacast.net> wrote:
mlimber wrote:
Repro::Repro()
{
wd[0] = 0;
st.word[0] = 0;
}


Actually, a compliant compiler should also accept

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

for that. Not sure if VC++ 2003 is that good. Probably not.


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
May 26 '06 #11
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
May 26 '06 #12
"Default User" <de***********@yahoo.com> wrote in message
news:Iz*******@news.boeing.com...
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


Funny.

--
It is far better to post code then to never have posted
May 26 '06 #13
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:e5**********@news.datemas.de...
Jeffrey Baker wrote:
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;

}


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

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
May 27 '06 #14
Jeffrey Baker wrote:
"Default User" <de***********@yahoo.com> wrote in message
news:Iz*******@news.boeing.com...
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.
Funny.


??

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
May 27 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Harry Whitehouse | last post: by
2 posts views Thread by Harry Whitehouse | last post: by
2 posts views Thread by pmbcan | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.