Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old July 24th, 2004, 10:51 AM
Authorized User
 
Join Date: May 2004
Location: chengdu, , China.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default help!

1.h
#include<string>
using namespace std;
string str = "Not null!";
int inum = 123;

1.cpp
#include<iostream>
#include"1.h"
using namespace std;
extern string str; // #1
extern int inum; // #2

int main()
{
   cout<<"str is "<<str<<endl;
   cout<<"inum is "<<inum<<endl;
   return 0;
}

The compiler is vc6.0 and the result is:
str is
inum is 123

If delete the sentence of #1 £¬the result is :
str is Not null!
inum is 123

I know the sentences of #1 and #2 should be deleted,as 1.h is
a head file,not cpp file.But why the inum's value always is 123?
Does it has some matter with the compiler?




niuwei
__________________
niuwei
Reply With Quote
  #2 (permalink)  
Old August 9th, 2004, 07:49 AM
Authorized User
 
Join Date: May 2004
Location: chengdu, , China.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I compiled this program with Borland C++5.5,it's OK.So I think it does have something to do with the compiler.

niuwei
Reply With Quote
  #3 (permalink)  
Old August 11th, 2004, 01:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , .
Posts: 453
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Ankur_Verma Send a message via MSN to Ankur_Verma
Default

I'm not sure, but it seems to be a problem with string class and how it manages memory. str retains the value and its memory address until just before the main starts excecuting and loose them as soon as excecution enters main.

Try the same program with char* and you would see no such problem.

Ankur
Reply With Quote
  #4 (permalink)  
Old August 24th, 2004, 10:21 AM
Authorized User
 
Join Date: May 2004
Location: chengdu, , China.
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you,Ankur.Yes,char * is OK.
I think the program below can make us more clearly of how VC6.0 works.
class AA
{
public:

    AA() : v(0)
    {
        ++count;
        cout << "Default constructor" << endl;
    }
    AA(int d) : v(d)
    {
        ++count;
        cout << "constructor" << endl;
    }
    ~AA()
    {
        --count;
    }
    void Show()
    {
        cout << "v=" << v << "\tcount=" << count << endl;
    }

private:
    int v;
    static int count;
};

int AA::count = 0;


AA a(5);
extern AA a; // A new object a is generated
int main()
{
    {
        AA b(4);
        b.Show(); // Now three objects have been generated.
    }
    a.Show();
}

// The result
constructor
Default constructor
constructor
v=4 count=3
v=0 count=2
Press any key to continue


So we can see clearly that VC6 generated a new object when running to
the code of "extern string str" and the former object was covered.



niuwei
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off




All times are GMT -4. The time now is 05:45 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.