473,586 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1790
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.goo glegroups.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.goo glegroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1530
by: Boris Dynin | last post by:
Hi, I have some projects (native - not managed code) in VC++ 2003. I'm trying to debug that stuff on Win98 system that has VC6.0 installed. Is it possible to use VC6.0 for debugging code built with VC++ 2003? I know that remote debugging with VS 2003 can be used in this scenario. However, it would be easier for me to use VC6.0. Thanks...
2
1495
by: zorro | last post by:
Hi, I use the Visual C++ .NET 2003 Standard Edition which doesn't include an optimizing compiler. But Microsoft released the VC++ 2003 Toolkit (http://msdn.microsoft.com/visualc/vctoolkit2003/) which provides an optimizing compiler. Is it possible to replace the original "Standard" compiler with this one ? How could I do this ?
5
1442
by: Michael | last post by:
i experience slower compile times with VC++ 2003 compared to VC+6.0. Anyone experiencing the same? Should that be expected? This ineed matters, when total compilation time is > 1h and you have to wait 10-50% longer...
2
1188
by: Harry Whitehouse | last post by:
I'm porting an application from Borland C++ to VS .NET 2003. The VC++ compiler doesn't want to deal with declarations like this in my code: LONG FAR PASCAL _export StdPVDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { }
2
1227
by: Harry Whitehouse | last post by:
I just wanted to confirm this for WIN32 projects in VC++ 2003. It looks like you need to reference functions in 3rd party DLL's (C-based) by building a LIB file as described here: http://support.microsoft.com/default.aspx?scid=KB;EN-US;q131313&id=KB;EN-US;q131313 Am I wrong in my assumption? TIA
6
1887
by: Felix I. Wyss | last post by:
It appears that VC++2003 has a code generator bug related to template parameters that are a pointer-to-member type: If the actual template argument is a virtual method, VC generates code that always calls the method at the first vtable slot (index 0). To reproduce this bug, consider the following code: template<class CLASS_T, void...
27
2752
by: Jason Doucette | last post by:
I'm getting an assertion fire from a list iterator being checked against NULL. This did not occur in VC++ 2003 (v7.1). Are there changes that have been made to the STL between these versions that I should know about? Any resources I should check into? thanks, Jason
0
1443
by: JohnIdol | last post by:
VC++6 to VC++2003 - linking troubles -------------------------------------------------------------------------------- Hi All, I successfully ported an application from VC++6 to VS2003. Solved compiling issues but now I am sticking with serious Linking troubles. I got a bunch of:
2
2225
by: pmbcan | last post by:
I have a project written in VC++ 2003.NET which uses MFC classes. I have ONLY VC++Express2005. I can open the VCProject file sucessfully. The conversion takes place without any error. BUT the project is not Building using VC++Express2005. The error is as follows: 1>Compiling... 1>StdAfx.cpp 1>\stdafx.h(35) : fatal error C1083: Cannot...
0
7839
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8200
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8215
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5710
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3836
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1448
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1179
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.