这段代码有什么问题? [英] what are the problems with this piece of code?

查看:59
本文介绍了这段代码有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是一名大学生,我正在为今天的决赛做准备。

我正在阅读课程笔记,我发现了一段完全奇怪的代码。它让我笑了,我觉得老师需要为这个

课程做好准备。

所以我要求你的观点。

这里是代码:


客户端内存管理!!

//清单9.4。客户而不是服务器进行内存管理

对象

类名称{

public:

char * contents; // PUBLIC指向动态内存的指针

Name(char * name); //或Name(char name []);

void show_name();

~Name(); //析构函数消除内存泄漏

};

Name :: Name(char * name)//转换构造函数

....

名称::〜名称(){//析构函数

删除内容; //它删除了堆内存,而不是指针

}


void Client(){

名称n1(" Jones" ); //转换构造函数被调用

名称* p =(名称*)malloc(sizeof(Name)); //没有构造函数被调用

p-> contents = new char [strlen(" Smith")+ 1]; //分配动态内存

if(p-> contents == NULL){//''new''不成功

cout<< Out of memory\\\
;出口(1); //放弃

}

strcpy(p-> contents," Smith"); //''new''成功

n1.show_name();对 - > SHOW_NAME(); //使用对象

删除p->内容; //避免内存泄漏

free(p); //注意动作的顺序

}

// p被删除,对象n1的析构函数被称为


(c )AISHY AMER CONCORDIA UNIV,ECE

Hello, I''m a university student and I''m preparing for my final today.
I''m reading course notes, I found completely strange piece of code. It
makes me laugh, I think the teacher needs to prepare herself for this
course.
so I ask for your point of view.
here''s the piece of code:

Memory management by client!!
// Listing 9.4. Memory management by client rather than by server
object
class Name{
public:
char *contents; // PUBLIC pointer to dynamic memory
Name (char* name); // or Name (char name []);
void show_name();
~Name(); // destructor eliminates memory leak
};
Name::Name(char* name)// conversion constructor
....
Name::~Name(){ // destructor
delete contents; // it deletes heap memory, not the pointer
}

void Client(){
Name n1("Jones"); // conversion constructor is called
Name *p = (Name*) malloc(sizeof(Name)); // no constructor is called
p->contents = new char[strlen("Smith")+1]; // allocate dynamic memory
if (p->contents == NULL){ // ''new'' was not successful
cout << "Out of memory\n"; exit(1); // give up
}
strcpy(p->contents, "Smith"); // ''new'' was successful
n1.show_name(); p->show_name(); // use the objects
delete p->contents; // avoid memory leak
free (p); // notice the sequence of actions
}
// p is deleted, destructor for object n1 is called

(c) AISHY AMER CONCORDIA UNIV, ECE

推荐答案

__ PPS__写道:
__PPS__ wrote:
你好,我是一所大学学生,我正准备今天的决赛。
我正在读课程笔记,我发现了一段完全奇怪的代码。它让我开怀大笑,我认为老师需要为这个课程做好准备。


准备?你的意思是什么?

所以我要求你的观点。
这里是代码片段:

客户的内存管理!!
//清单9.4。内存管理由客户端而不是服务器对象
类名{
public:
char * contents; // PUBLIC指向动态内存的指针
Name(char * name); //或Name(char name []);
void show_name();
~Name(); //析构函数消除了内存泄漏


这里的第一个也是最重要的问题是没有复制副本和

复制赋值操作。这违反了三法则。


当然,公共数据不好。


可以改进到界面。 c-tor应该使用

指向const char的指针。 ''show_name''成员应该带一个参数

(要打印到的流),这个成员应该是''const''。

};
名称::名称(字符*名称)//转换构造函数
...

名称::〜名称(){//析构函数
删除内容; //它删除堆内存,而不是指针


我认为这是一个bug,因为它应该可能是


delete []内容;


(虽然不知道如何获得''内容''指针。

}

void Client() {
名称n1(琼斯); //转换构造函数被调用
名称* p =(名称*)malloc(sizeof(名称)); //没有构造函数被调用
p-> contents = new char [strlen(" Smith")+ 1]; //分配动态内存
if(p-> contents == NULL){//''new''不是成功


''new''或'new []''在失败时实际上不会返回NULL。它会抛出。

cout<< ;Out of memory\ n;退出(1); //放弃
}
strcpy(p-> contents," Smith"); //''new''成功了
n1.show_name(); p-> show_name(); //使用对象
删除p->内容; //避免内存文件ak


必须是


删除[] p->内容;

free(p); //注意动作的顺序
}
// p被删除,对象n1的析构函数被称为

(c)AISHY AMER CONCORDIA UNIV,ECE
Hello, I''m a university student and I''m preparing for my final today.
I''m reading course notes, I found completely strange piece of code. It
makes me laugh, I think the teacher needs to prepare herself for this
course.
Prepare? What do you mean?
so I ask for your point of view.
here''s the piece of code:

Memory management by client!!
// Listing 9.4. Memory management by client rather than by server
object
class Name{
public:
char *contents; // PUBLIC pointer to dynamic memory
Name (char* name); // or Name (char name []);
void show_name();
~Name(); // destructor eliminates memory leak
The first and foremost problem here is the absence of a copy-c-tor and
the copy assignment op. That''s a violation of "the Rule of Three".

Of course, public data is bad.

Improvements could be made to the interface. The c-tor should take
a pointer to const char. The ''show_name'' member should take an argument
(the stream to print out to), and this member should probably be ''const''.
};
Name::Name(char* name)// conversion constructor
...
Name::~Name(){ // destructor
delete contents; // it deletes heap memory, not the pointer
I think it''s a bug because it should likely be

delete[] contents;

(although it is unknown how the ''contents'' pointer is obtained.
}

void Client(){
Name n1("Jones"); // conversion constructor is called
Name *p = (Name*) malloc(sizeof(Name)); // no constructor is called
p->contents = new char[strlen("Smith")+1]; // allocate dynamic memory
if (p->contents == NULL){ // ''new'' was not successful
''new'' or ''new[]'' does not actually return NULL upon a failure. It throws.
cout << "Out of memory\n"; exit(1); // give up
}
strcpy(p->contents, "Smith"); // ''new'' was successful
n1.show_name(); p->show_name(); // use the objects
delete p->contents; // avoid memory leak
Must be

delete[] p->contents;
free (p); // notice the sequence of actions
}
// p is deleted, destructor for object n1 is called

(c) AISHY AMER CONCORDIA UNIV, ECE



^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

那是什么?这是你要去的学校吗?


总而言之,是的,这段代码让我想起了我在
中看到的东西
a经过一些C / / $
程序员编写的一些业余C ++代码后的糟糕梦想。而且我仍然需要维护与此类似的代码。

希望在我完成之后,无论谁来到下一个都不需要做很多的b $ b。我只能希望。


V


^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What''s that? Is that the school you''re attending?

All in all, yes, that piece of code reminds me of something I''ve seen in
a bad dream after going over some amateurish C++ code written by some C
programmers. And I still have to maintain code similar to that.
Hopefully, after I am done with it, whoever comes next won''t have to do
much. I can only hope.

V


2005年12月19日星期一12:25:29 -0500,Victor Bazarov

< v。******** @ comAcast.net>写道:
On Mon, 19 Dec 2005 12:25:29 -0500, Victor Bazarov
<v.********@comAcast.net> wrote:
__ PPS__写道:
__PPS__ wrote:
你好,我是一名大学生,我正在为今天的决赛做准备。
我'在阅读课程笔记时,我发现了一段完全奇怪的代码。它让我开怀大笑,我认为老师需要为这个课程做好准备。
Hello, I''m a university student and I''m preparing for my final today.
I''m reading course notes, I found completely strange piece of code. It
makes me laugh, I think the teacher needs to prepare herself for this
course.




[snip]


我认为这真的是作业......记下这段代码所有的问题。你刚刚为他做了一些功课

(或她)!


-

Bob Hairgrove
No**********@Home.com


Bob Hairgrove写道:
Bob Hairgrove wrote:
2005年12月19日星期一12:25:29 -0500,Victor Bazarov
< v。**** ****@comAcast.net>写道:

On Mon, 19 Dec 2005 12:25:29 -0500, Victor Bazarov
<v.********@comAcast.net> wrote:

__ PPS__写道:
__PPS__ wrote:
你好,我是一名大学生,我正准备参加我的决赛今天。
我正在阅读课程笔记,我发现了一段完全奇怪的代码。它让我开怀大笑,我认为老师需要为这个课程做好准备。
Hello, I''m a university student and I''m preparing for my final today.
I''m reading course notes, I found completely strange piece of code. It
makes me laugh, I think the teacher needs to prepare herself for this
course.



[snip]

我认为那真的是作业......写下这段代码的所有问题。你刚刚为他做了一些功课
(或她)!


[snip]

I think that was really the assignment ... to write down all the
problems this code has. You just did (some of) the homework for him
(or her)!




哦......我想我已经老了或者也许只是感恩慈善

今天。



Oh well... I guess I''m getting old or maybe just feeling charitable
today.


这篇关于这段代码有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆