内存分配释放... [英] memory allocation deallocation ...

查看:86
本文介绍了内存分配释放...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你是否看到使用构造函数3后跟一个

函数foo()有什么问题,正如我在下面定义的那样?


class A {

member_variable_1;

ptr_member_variable_2;


public:

//构造函数1

A():

member_variable_1(初始值),

ptr_member_variable_2(NULL){};


//构造函数2

A(m_var_1,m_ptr_val_2):

member_variable_1(m_var_1),

ptr_member_variable_2(m_ptr_val_2){};


//构造函数3

A(m_var_1)

{

//使用member_variable_1做一些事情

//并且不用担心ptr_member_variable_2


//由于没有为ptr_member_variable_2分配内存

// d-ctor不应该试着删除它吗?

//问题1。你觉得这种方法有什么问题吗?

};


~A()

{

if(!ptr_member_variable){

delete ptr_member_variable_2;

ptr_member_variable_2 = NULL;

}

};


// foo只使用member_variable_1,

//不关心ptr成员变量

foo() ;

};


提前致谢。

解决方案

< blockquote> pasa_1写道:


你看到使用构造函数3后跟一个

函数foo()有什么问题,正如我在下面定义的那样?



请参阅发布代码问题的指南:

http://parashift.com/c++-faq-lite/ho...t.html#faq-5.8


A类{

member_variable_1;

ptr_member_variable_2;



为了论证,我们给出这些类型:


int member_variable_1;

int * ptr_member_variable_2;


>

public:

//构造函数1

A():

member_variable_1(初始值),

ptr_member_variable_2(NULL){};


//构造函数2

A(m_var_1,m_ptr_val_2):

member_variable_1(m_var_1),

ptr_member_variable_2(m_ptr_val_2){};


//构造函数3

A(m_var_1)

{

//使用member_variable_1做某事

//并且不用担心ptr_member_variable_2


//因为没有为ptr_member_variable_2分配内存

// d- ctor不应该试图删除它吗?

//问题1。你认为这种方法有什么问题吗?

};



分号在函数定义后是无关紧要的。为什么不给你

将这三个(或至少前两个)合并为一个:


A(int v = 0,int * p = 0)

:member_variable_1(v)

,ptr_member_variable_2(p)

{}


第三个构造函数的问题是,如果你没有初始化

指针为0(或NULL),它可能有* any *值,所以

当你到达析构函数时它会尝试删除它,如果它是
非零,这是不好的。


如果你传入一个指针,该对象取得

的所有权(即,对象负责删除它),我建议在参数列表中使用

std :: auto_ptr沟通这个。然后你可以将
存储为auto_ptr中的成员,其他一些智能指针,甚至是/ b $ ba朴素指针,具体取决于你的需求。


~A()

{

if(!ptr_member_variable){

delete ptr_member_variable_2;

ptr_member_variable_2 = NULL;

}

};



在删除之前你不需要检查NULL(参见
http://parashift.com/c++-faq-lite/fr...html#faq-16.8)<< >。另外,

一般不需要在

析构函数中将成员指针设置为NULL。该对象正在被破坏,因此该变量变得无法访问。顺便说一句,如果你使用一个智能指针,这个析构函数将完全离开


// foo仅使用member_variable_1,

//不关心ptr成员变量

foo();

};



底线建议:给所有东西一个初始值。


干杯! --M


pasa_1写道:


你看到使用构造函数3有什么问题吗?然后是我在下面定义的

函数foo()?


A类{

member_variable_1;

ptr_member_variable_2;


public:

//构造函数1

A():

member_variable_1(初始值),

ptr_member_variable_2(NULL){};


//构造函数2

A(m_var_1 ,m_ptr_val_2):

member_variable_1(m_var_1),

ptr_member_variable_2(m_ptr_val_2){};


//构造函数3

A(m_var_1)

{

//使用member_variable_1做一些事情

//并且不要打扰ptr_member_variable_2


//由于没有为ptr_member_variable_2分配内存

// d-ctor不应该尝试删除它?

//问题1。你觉得这种方法有什么问题吗?

};


~A()

{

if(!ptr_member_variable){

delete ptr_member_variable_2;

ptr_member_variable_2 = NULL;

}

};


// foo只使用member_variable_1,

//不关心ptr成员变量

foo() ;

};


提前致谢。



是的,不编译:P


也在析构函数中你最好将ptr_member_variable与NULL进行比较。


在构造函数3中,我希望你这样做:

ptr_member_variable = NULL


pasa_1写道:


你看到使用构造函数3有什么问题吗?我在下面定义了一个

函数foo()?



是的。你的''foo''没有任何返回值类型。并查看我的

评论内联。


>

A级{

member_variable_1;

ptr_member_variable_2;



除非这些宏扩展为普通声明,否则

语法错误:缺少类型。


>

public:

//构造函数1

A():

member_variable_1(初始值),

ptr_member_variable_2(NULL){};



尾随分号是无关紧要的。 Drop''em。


>

//构造函数2

A(m_var_1,m_ptr_val_2):



参数缺少类型。


member_variable_1(m_var_1),

ptr_member_variable_2(m_ptr_val_2){};


//构造函数3

A(m_var_1)



类型丢失。


{

//使用member_variable_1做点什么

//并且不要打扰ptr_member_variable_2


//由于没有为ptr_member_variable_2分配内存

// d-ctor不应该尝试删除它?



问题是''​​ptr_member_variable_2''具有_undetermined_

值。对它做任何事都会有不确定的行为。


// Question1。你觉得这种方法有什么问题吗?

};


~A()

{

if(!ptr_member_variable){



''ptr_member_variable''似乎不存在。你有没有把它给b / b $ b''_2''?如果你这样做了,你的c-tor 3会忘记初始化那个

变量,所以坏事一定要发生。


delete ptr_member_variable_2 ;

ptr_member_variable_2 = NULL;

}

};


// foo仅使用member_variable_1 ,

//不关心ptr成员变量

foo();

};


先谢谢。



V

-

请在通过电子邮件回复时删除资金'A'

我没有回复最热门的回复,请不要问


Do you see anything wrong with using constructor 3 followed by a
function foo() as I have defined below?

class A {
member_variable_1;
ptr_member_variable_2;

public:
// Constructor 1
A():
member_variable_1(initial value),
ptr_member_variable_2(NULL) { };

// constructor 2
A(m_var_1, m_ptr_val_2):
member_variable_1(m_var_1),
ptr_member_variable_2(m_ptr_val_2) { };

// constructor 3
A(m_var_1)
{
// do something with member_variable_1
// and don''t bother about ptr_member_variable_2

// Since no memory was allocated for ptr_member_variable_2
// the d-ctor should not try to delete it?
// Question1. do you see anything wrong in this approach?
};

~A()
{
if (!ptr_member_variable) {
delete ptr_member_variable_2;
ptr_member_variable_2 = NULL;
}
};

// foo uses only member_variable_1,
// doesn''t care about the ptr member variable
foo();
};

Thanks in advance.

pasa_1 wrote:

Do you see anything wrong with using constructor 3 followed by a
function foo() as I have defined below?

Please see the guidelines on posting code questions:

http://parashift.com/c++-faq-lite/ho...t.html#faq-5.8

class A {
member_variable_1;
ptr_member_variable_2;

For the sake of argument, let''s give these types:

int member_variable_1;
int* ptr_member_variable_2;

>
public:
// Constructor 1
A():
member_variable_1(initial value),
ptr_member_variable_2(NULL) { };

// constructor 2
A(m_var_1, m_ptr_val_2):
member_variable_1(m_var_1),
ptr_member_variable_2(m_ptr_val_2) { };

// constructor 3
A(m_var_1)
{
// do something with member_variable_1
// and don''t bother about ptr_member_variable_2

// Since no memory was allocated for ptr_member_variable_2
// the d-ctor should not try to delete it?
// Question1. do you see anything wrong in this approach?
};

The semicolons are extraneous after function definitions. Why don''t you
combine these three (or at least the first two) into one:

A( int v = 0, int * p = 0 )
: member_variable_1(v)
, ptr_member_variable_2(p)
{}

The problem with the third constructor is that if you don''t initialize
the pointer to 0 (or NULL), it may have *any* value whatsoever, and so
when you get to the destructor it will try to delete it if it is
non-zero, which is bad.

If you are passing in a pointer that this object takes ownership of
(i.e., the object is responsible for deleting it), I''d suggest using
std::auto_ptr in the parameter list to communicate this. Then you can
store it as a member in an auto_ptr, some other smart pointer, or even
a plain pointer, depending on your needs.

~A()
{
if (!ptr_member_variable) {
delete ptr_member_variable_2;
ptr_member_variable_2 = NULL;
}
};

You needn''t check for NULL before deleting (see
http://parashift.com/c++-faq-lite/fr...html#faq-16.8). Also,
in general there is no need to set member pointers to NULL in the
destructor. The object is being destroyed, and so the variable becomes
inaccessible. BTW, if you use a smart pointer, this destructor will go
away altogether.

// foo uses only member_variable_1,
// doesn''t care about the ptr member variable
foo();
};

Bottom line advice: Give everything an initial value.

Cheers! --M


pasa_1 wrote:

Do you see anything wrong with using constructor 3 followed by a
function foo() as I have defined below?

class A {
member_variable_1;
ptr_member_variable_2;

public:
// Constructor 1
A():
member_variable_1(initial value),
ptr_member_variable_2(NULL) { };

// constructor 2
A(m_var_1, m_ptr_val_2):
member_variable_1(m_var_1),
ptr_member_variable_2(m_ptr_val_2) { };

// constructor 3
A(m_var_1)
{
// do something with member_variable_1
// and don''t bother about ptr_member_variable_2

// Since no memory was allocated for ptr_member_variable_2
// the d-ctor should not try to delete it?
// Question1. do you see anything wrong in this approach?
};

~A()
{
if (!ptr_member_variable) {
delete ptr_member_variable_2;
ptr_member_variable_2 = NULL;
}
};

// foo uses only member_variable_1,
// doesn''t care about the ptr member variable
foo();
};

Thanks in advance.


yes, does not compile :P

also in the destructor you better compare ptr_member_variable to NULL.

in the constructor3, i hope you are doing:
ptr_member_variable=NULL


pasa_1 wrote:

Do you see anything wrong with using constructor 3 followed by a
function foo() as I have defined below?

Yes. Your ''foo'' doesn''t have any return value type. And see my
comments inline.

>
class A {
member_variable_1;
ptr_member_variable_2;

Unless those are macros expanding into normal declarations, the
syntax is wrong: types are missing.

>
public:
// Constructor 1
A():
member_variable_1(initial value),
ptr_member_variable_2(NULL) { };

Trailing semicolons are extraneous. Drop''em.

>
// constructor 2
A(m_var_1, m_ptr_val_2):

Types are missing for the arguments.

member_variable_1(m_var_1),
ptr_member_variable_2(m_ptr_val_2) { };

// constructor 3
A(m_var_1)

Type is missing.

{
// do something with member_variable_1
// and don''t bother about ptr_member_variable_2

// Since no memory was allocated for ptr_member_variable_2
// the d-ctor should not try to delete it?

The problem is that ''ptr_member_variable_2'' has _undetermined_
value. Doing anything with it would have undefined behaviour.

// Question1. do you see anything wrong in this approach?
};

~A()
{
if (!ptr_member_variable) {

''ptr_member_variable'' doesn''t seem to exist. Did you fortet the
''_2''? And if you did, your c-tor 3 forgets to initialise that
variable, so bad things are bound to happen.

delete ptr_member_variable_2;
ptr_member_variable_2 = NULL;
}
};

// foo uses only member_variable_1,
// doesn''t care about the ptr member variable
foo();
};

Thanks in advance.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


这篇关于内存分配释放...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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