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

J

Jeffrey Baker

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
 
M

mlimber

Jeffrey said:
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

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
 
V

Victor Bazarov

Jeffrey said:
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
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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
 
J

Jeffrey Baker

--
It is far better to post code then to never have posted
mlimber said:
Jeffrey said:
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

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
 
M

mlimber

Jeffrey said:
--
It is far better to post code then to never have posted
mlimber said:
Jeffrey said:
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

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-lite/containers.html#faq-34.1), and
then that class' constructor will initialize it for you automatically.

Cheers! --M
 
V

Victor Bazarov

mlimber said:
Jeffrey said:
[..]

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-lite/containers.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
 
M

mlimber

Victor said:
mlimber said:
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.
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
 
V

Victor Bazarov

mlimber said:
[..] 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".
 
M

mlimber

Victor said:
mlimber said:
[..] 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
 
M

Marcus Kwok

Victor Bazarov said:
mlimber said:
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...5401ad4b39a/dab44a8b786b6a79#dab44a8b786b6a79

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/micr...guages.vc/browse_frm/thread/b5d0dbefaa1c4286/
 
D

Default User

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
 
J

Jeffrey Baker

Default User said:
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.
 
J

Jeffrey Baker

Victor Bazarov said:
Jeffrey said:
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
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
 
D

Default User


??

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top