Jump to content
  • Advertisement
Sign in to follow this  
draconar

my compiler got dumb (aka gnomes are running the computers)

This topic is 2999 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all, I'm working through Stroustrup's newest book and here and there I'm discovering some novelty. As an example, I learned that even "primitive" types have constructors in C++. Like an int is in reality an int obj and they have automatic initialization:
int i;
cout << i << endl;
the result on screen will be 0. Ok, in my notebook (VC++ 2008 express) this works alright. But not in my desktop! which is running VC++ 2008 express too. In my desktop, the value of i on screen is a random garbage. what is this?! the only difference I could spot was that the notebook got a "using namespace System" while the desktop don't. Actually, the "using namespace System" doesn't even works in the Desktop. The compiler says: 1>.\apagar.cpp(9) : error C2871: 'System' : a namespace with this name does not exist what is this? different versions of the compiler?!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by Badgerr
You are printing out an uninitialized variable, which has undefined results.

Try int i = 0;


nops. c++ has a default constructor for built-in types. When I write just "int i;", would be the same as writing

int i = int();

which initializes correctly to zero, because it called the default constructor explicitly.

somehow one compiler is doing it correctly (calling the default constructor) and the other don't..

Share this post


Link to post
Share on other sites
The line "Using namespace System" looks like you created a managed C++ project (note the managed). Things behave quite different there.

Plain vanilla C++ does NOT initialize primitives.

Share this post


Link to post
Share on other sites
Quote:
Original post by draconar
what is this? different versions of the compiler?!

Different languages. Bjarne writes about C++, a language standardized by the ISO. You are using a language called C++/CLR invented by Microsoft on your notebook. They are vaguely related.

You are incorrect in your reading of Stroustrup, by the way.
int i;
is not equivalent to
int i = int(0);
according to the language definition.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
Quote:
Original post by draconar
what is this? different versions of the compiler?!

Different languages. Bjarne writes about C++, a language standardized by the ISO. You are using a language called C++/CLR invented by Microsoft on your notebook. They are vaguely related.

You are incorrect in your reading of Stroustrup, by the way.
int i;
is not equivalent to
int i = int(0);
according to the language definition.


yeah. that makes sense.
So as for the ANSI C++ goes,

int i; <-- is unitialized variable
int i = int(); <-- explicity calls the default constructor, which initializes to zero.

actually in page 323 (from PPP) he says


For built-in types, such as int and double, the default constructor notation means 0, so int() is a complicated way of saying 0, and double() a long-winded way of saying 0.0


but I extrapolated his examples using string,

string s1; //default value: the empty string ""

which is not a built-in type.

BTW, the passage I quote refers to the (), the default constructor notation. Not to automatic initialization of built-in types. ouch. :(

Share this post


Link to post
Share on other sites
Quote:
Original post by Endurion
The line "Using namespace System" looks like you created a managed C++ project (note the managed). Things behave quite different there.

Plain vanilla C++ does NOT initialize primitives.


Endurion,
you nailed it.

I created a managed c++ project in VC++ 2008. How can I be sure to always create an ANSI C++ project?

Share this post


Link to post
Share on other sites
After selecting "New Project" from the file menu, you should end up with a tree view in a pane off to the left with a bunch of project types. One of the nodes should be Visual C++ and underneath that you should see several more project types (ATL, CLR, General, MFC...).

A CLR Project is a C++/CLI (Managed) Project. You want the project type "Win32".

Choose a "Win32 Console Application" (I think) and when the Application Wizard pops up, click "Next" and under "Additional options" choose "Empty Project"

Hope that helps.

Share this post


Link to post
Share on other sites
As far as i know Gcc or Msvc dosen't initialize pod types with any value;
int i; will probably not be 0;Don't trust the book at these minor detailes and manually write i=0 if you want i to be 0;
string q(); is a totally different story, it involes memory allocation so for safety it must be "" be default

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!