在单件模式中写析构函数遇到的问题,是缓冲区有溢出吗,帮忙看看,多谢了.

021850524 2004-08-29 03:41:10
在单件模式中写析构函数遇到的问题,是缓冲区有溢出吗,帮忙看看,多谢了.

程序源码:

#include <iostream>
#include <string>
using namespace std;

class Word
{
private:
Word();
Word(Word&);
Word &operator=(Word &);

int count;

char *pWords[10];
public:
~Word();
static Word &GetInstance()
{
static Word w;
return w;
}
int lookup(char *);
};

Word::Word()
{
count = 0;
strcpy(pWords[0]=new char[10], "test");
strcpy(pWords[1]=new char[10], "test");
strcpy(pWords[2]=new char[10], "test");
strcpy(pWords[3]=new char[10], "test");
strcpy(pWords[4]=new char[10], "test");
strcpy(pWords[5]=new char[10], "test");
strcpy(pWords[6]=new char[10], "test");
strcpy(pWords[7]=new char[10], "test");
strcpy(pWords[8]=new char[10], "test");
strcpy(pWords[9]=new char[10], "test");
}

Word::~Word()
{
int i;
for(i=0; i<10; i++)
{
delete pWords[i];
pWords[i] = NULL;
cout<<"deleting......"<<i<<endl;
}
}

int Word::lookup(char *s)
{
int i;
for(i=0; i<10; i++)
{
if(!strcmp(pWords[i], s))
{
count++;
}
}
return count;
}

int main()
{
Word &word = Word::GetInstance();
cout<<word.lookup("test")<<endl;
return 0;
}

结果:
10
deleting......
而不是预想的
10
deleting......0
deleting......1
deleting......2
deleting......3
deleting......4
deleting......5
deleting......6
deleting......7
deleting......8
deleting......9

...全文
214 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
rorot 2004-08-30
  • 打赏
  • 举报
回复
OK,废人说的全对,俺写错了,汗阿~
Wolf0403 2004-08-30
  • 打赏
  • 举报
回复
另外,copy constructor 的参数应该是 const Word &
Wolf0403 2004-08-30
  • 打赏
  • 举报
回复
rorot:

析构函数中你写错了一处……

Word::~Word () // Destructor
{
//
std::cout << "destructor\n";
for ( int i=0; i<10; ++i )
{
delete str[i]; // should be delete[] str[i];
str[i] = NULL;
std::cout << "deleting ... " << i << "\n";
}
}

至于 static 对象的析构函数不会自动被系统调用,我今天还是第一次注意到。。。佩服
rorot 2004-08-30
  • 打赏
  • 举报
回复
1 单件模式你的这种写法有待商榷...( 和流的转换层可能有冲突 )
2 int Word::lookup(const char*)里有一个隐含的错误, count = 0应该明确指出,否则下次调用会出错.
3 参照你的效果,俺给出我的singleton:

//////////////////////////////////////////////
//
// USEAGE -: Singleton Pattern
//
//////////////////////////////////////////////
#include <memory>
#include <iostream>

class Word
{
public:
static Word* Instance ();
int lookup (const char*);
~Word ();

private:
Word ();
Word (Word&);
static Word* pInstance;
char* str[10];
};

// Implementation of class Singleton
////////////////////////////////////////

Word* Word::pInstance = NULL;

Word::Word () // Default constructor
{
//
std::cout << "constructor\n";
for (int i=0; i<10; ++i)
strcpy( (str[i] = new char[10]), "test" );
}

Word::Word (Word& w) // Constructor
{
//
}

Word::~Word () // Destructor
{
//
std::cout << "destructor\n";
for ( int i=0; i<10; ++i )
{
delete str[i];
str[i] = NULL;
std::cout << "deleting ... " << i << "\n";
}
}

Word* Word::Instance () // Get Instance
{
if (!pInstance)
pInstance = new Word;
return pInstance;
}

int Word::lookup (const char* s) // Compare string ( ... So jokey function! )
{
int count = 0;
for ( int i=0; i<10; ++i )
if ( !strcmp(s, str[i]) ) ++count;
return count;
}

int main()
{
std::auto_ptr <Word> pWord(Word::Instance());
std::cout << pWord->lookup( "test" ) << std::endl;
return 0;
}

Reference:
<moder c++ design> ->Components -> Implementing Singletons
cchuocp 2004-08-29
  • 打赏
  • 举报
回复
cout也是一个静态全局对象,你知道它和w那个先析构吗?如果你的回答是不知道,那就不要在word的析构函数中调用cout。上面的代码与编译器的相关性太大了,要避免使用。
guijianchou 2004-08-29
  • 打赏
  • 举报
回复
拷贝构造:改为
Cat::Cat(const Cat &cat)
{
itsage = new int;
*itsage = *cat.itsage;
}
021850524 2004-08-29
  • 打赏
  • 举报
回复
单件中的一种表现手法是SingleTon模式.你可以查阅一下相关的资料.
有本书(书名忘了)好像叫Pattern Design,设计模式,你找找看看吧.
另外,又有个程序我vc6过不了,但是gcc能过.真郁闷,难道真的要换编译器了吗.

#include <iostream>
using namespace std;

class Cat
{
public:
Cat();
Cat(const Cat&);
~Cat();
int getage() const { return *itsage; }
void setage(int age) { *itsage=age; }
protected:
int *itsage;
};

Cat::Cat()
{
itsage = NULL;
itsage = new int;
*itsage=5;
}

Cat::Cat(const Cat &cat)
{
if(itsage)
*itsage = *cat.itsage;
else
{
itsage = new int;
*itsage = *cat.itsage;
}
}

Cat::~Cat()
{
delete itsage;
itsage = NULL;
}

int main()
{
Cat frisky;
cout<<"frisky's age:"<<frisky.getage()<<endl; //5
cout<<"setting frisky to 6...\n";
frisky.setage(6);
cout<<"creating boots from frisky\n";
Cat boots(frisky);
cout<<"frisky's age:"<<frisky.getage()<<endl; //6
cout<<"boots'age:"<<boots.getage()<<endl; //6
cout<<"setting frisky to 7...\n";
frisky.setage(7);
cout<<"frisky's age:"<<frisky.getage()<<endl; //7
cout<<"boots'age:"<<boots.getage()<<endl; //6

return 0;

}
//请网友们再帮忙看看吧.程序挺简单的,但是总调不出来,看不出哪里错了.
steveyzhang 2004-08-29
  • 打赏
  • 举报
回复
请问楼上,什么是单件
021850524 2004-08-29
  • 打赏
  • 举报
回复
多谢,我到没看什么关于设计模式方面的东西.只不过写个练习程序过程中,觉得如果写成单件,会有一些好处.就改成单件了.然后就遇到了这样的问题.
renheihei 2004-08-29
  • 打赏
  • 举报
回复
另:
楼主看得什么书,上面讲了设计模式?
renheihei 2004-08-29
  • 打赏
  • 举报
回复
楼主:
我分别在vc和dev c++中测试了一下,是vc的问题,编译器问题!!!
dev c++输出是正确的!!

64,634

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧